# Plot 2D scatter plots comparing gene expression of two genes

**URL:** <https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304>\
**Category:** Code Cookbook\
**Created:** [July 17, 2022, 7:13pm UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304 "2022-07-17T19:13:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmccoy](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@mmccoy](https://help.c-moor.org/u/mmccoy)\
**Post date:** [July 17, 2022, 7:13pm UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/1 "2022-07-17T19:13:04Z")

</div>

Sometimes you want to know how the expression of two genes relate to each other. Here is some code to plot a 2D scatter plot:

```auto
# Create a variable with your favorite gene
my_favorite_genes <- c("ENSMUSG00000038192","ENSMUSG00000047139")
my_favorite_celltype <- "Def. endoderm"

# Plot
logcounts(sce)[my_favorite_genes,sce$celltype %in% my_favorite_celltype] %>%
  t() %>%
  as.matrix() %>%
  as_tibble() %>%
  rename(gene1 = 1, gene2 = 2) %>%
  ggplot(aes(x = gene1, y = gene2)) +
  geom_point() +
  theme_bw() +
  xlab(paste(my_favorite_genes[1], "log(counts)")) +
  ylab(paste(my_favorite_genes[2], "log(counts)")) +
  ggtitle(my_favorite_celltype)

```

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/c_moor/original/1X/d777545daf46c5bf21ee996c06b2136cf0458b01.png)

To plot a linear regression on top, you can add one with geom\_smooth(method = “lm”):

```auto
# Plot with linear regression line
logcounts(sce)[my_favorite_genes,sce$celltype %in% my_favorite_celltype] %>%
  t() %>%
  as.matrix() %>%
  as_tibble() %>%
  rename(gene1 = 1, gene2 = 2) %>%
  ggplot(aes(x = gene1, y = gene2)) +
  geom_point() +
  geom_smooth(method = "lm", col = "firebrick") +
  theme_bw() +
  xlab(paste(my_favorite_genes[1], "log(counts)")) +
  ylab(paste(my_favorite_genes[2], "log(counts)")) +
  ggtitle(my_favorite_celltype)

```

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/c_moor/original/1X/7f80a75bcb9ad39995cc8660b763b48bd315314b.png)

Together with the correlatePairs() function, which I posted about recently ([Finding coexpressed genes in specific cell types](https://help.c-moor.org/t/finding-coexpressed-genes-in-specific-cell-types/303)), you can use the p value and FDR to get a sense of whether your genes are significantly correlated, and then the rho value to see if they are positively or negatively correlated.

We can see from the above plots that these two genes are positively correlated. The output from correlatePairs() confirms this:

```auto
> cor.pairs <- correlatePairs(x = sce_subset, subset.row = of.interest)
> cor.pairs %>% as_tibble() %>% filter(gene1 %in% my_favorite_genes, gene2 %in% my_favorite_genes)
# A tibble: 1 × 5
  gene1 gene2 rho p.value FDR
  <chr> <chr> <dbl> <dbl> <dbl>
1 ENSMUSG00000038192 ENSMUSG00000047139 0.860 7.97e-30 1.59e-25

```

@yanitrevino

---

<div class="post-metadata">

**Author:** ![yanitrevino](https://avatars.discourse-cdn.com/v4/letter/y/8baadc/32.png) [@yanitrevino](https://help.c-moor.org/u/yanitrevino)\
**Post date:** [July 18, 2022, 8:08pm UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/2 "2022-07-18T20:08:04Z")

</div>

Hey @mmccoy so I tried running it to find the rho for Wnt7B(ENSMUSG00000022382) and c-jun(ENSMUSG00000052684), however the two genes I looked for didn’t pop up. What should I do?

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/c_moor/original/1X/31cc2536f2020c9d56be8fabb3944895e86d70b3.jpeg)

---

<div class="post-metadata">

**Author:** ![yanethp](https://avatars.discourse-cdn.com/v4/letter/y/bbce88/32.png) [@yanethp](https://help.c-moor.org/u/yanethp)\
**Post date:** [July 19, 2022, 3:19am UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/3 "2022-07-19T03:19:16Z")

</div>

![Screen Shot 2022-07-18 at 8.17.44 PM](https://canada1.discourse-cdn.com/flex027/uploads/c_moor/original/1X/64427619917a1395a1dc5b46cfbac25bf4c44c9b.jpeg)  
I’m having the same issue as Yani ^.

---

<div class="post-metadata">

**Author:** ![mmccoy](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@mmccoy](https://help.c-moor.org/u/mmccoy)\
**Post date:** [July 19, 2022, 3:44am UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/4 "2022-07-19T03:44:13Z")

</div>

Hi @yanethp and @yanitrevino , your genes are in the cor.pairs object you made, you just have to search for them. I edited the code above to show you how to filter the cor.pairs object to only show your two genes of interest:

```auto
cor.pairs %>% as_tibble() %>% filter(gene1 %in% my_favorite_genes, gene2 %in% my_favorite_genes)

```

---

<div class="post-metadata">

**Author:** ![yanitrevino](https://avatars.discourse-cdn.com/v4/letter/y/8baadc/32.png) [@yanitrevino](https://help.c-moor.org/u/yanitrevino)\
**Post date:** [July 19, 2022, 6:08am UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/5 "2022-07-19T06:08:42Z")

</div>

![image](https://canada1.discourse-cdn.com/flex027/uploads/c_moor/original/1X/3bbbad3cdca441c73bb68c96ff991f0e06f05077.png)  
Okay thank you I got it working!  
@yanethp this is how I got it to work:  
cor.pairs ← correlatePairs(x = sce\_subset, subset.row = of.interest)  
cor.pairs %\>% as\_tibble() %\>% filter(gene1 %in% my\_favorite\_gene, gene2 %in% my\_favorite\_gene)

# A tibble: 1 × 5

---

<div class="post-metadata">

**Author:** ![ShamounDivanyan](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@ShamounDivanyan](https://help.c-moor.org/u/ShamounDivanyan)\
**Post date:** [July 19, 2022, 6:47am UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/6 "2022-07-19T06:47:26Z")

</div>

![Screen Shot 2022-07-18 at 11.44.19 PM](https://canada1.discourse-cdn.com/flex027/uploads/c_moor/original/1X/2cbf53360acaa6efe5089635fa07ba32b97894b8.png)

How should I alter this code to show a violin plot of gene expression relative the embryonic stages, for all cell types, would this even be possible ?

---

<div class="post-metadata">

**Author:** ![LindsayHayes](https://yyz1.discourse-cdn.com/flex027/user_avatar/help.c-moor.org/lindsayhayes/32/14_2.png) [@LindsayHayes](https://help.c-moor.org/u/LindsayHayes)\
**Post date:** [July 19, 2022, 2:57pm UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/7 "2022-07-19T14:57:54Z")

</div>

Plotting the gastrulation across modalities using facet\_wrap()

```auto
gene_name <- "Sox17"
gene_id <- rowData(sce)[which(rowData(sce)[,2] %in% gene_name),1]

# favorite plot to combine both stage and cell type
plotExpression(sce, features = gene_id, colour_by = "celltype", x = "celltype") + theme(axis.text.x = element_text(angle = 90), legend.position = "none") + facet_wrap( ~colData(sce)$stage ) + labs(subtitle = gene_name)

```

---

<div class="post-metadata">

**Author:** ![ShamounDivanyan](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@ShamounDivanyan](https://help.c-moor.org/u/ShamounDivanyan)\
**Post date:** [July 20, 2022, 5:22am UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/8 "2022-07-20T05:22:03Z")

</div>

![Screen Shot 2022-07-19 at 10.19.36 PM](https://canada1.discourse-cdn.com/flex027/uploads/c_moor/original/1X/3299f4dc8230a74357c0f60ffc9a8a189ae6b1d7.jpeg)

Whenever I run this code with multiple gene names, it would only provide the plots for the very first gene, How could I tweak this so that it would show every single gene with their own individual plots, or would I have to run the code 9 times with each gene

---

<div class="post-metadata">

**Author:** ![LindsayHayes](https://yyz1.discourse-cdn.com/flex027/user_avatar/help.c-moor.org/lindsayhayes/32/14_2.png) [@LindsayHayes](https://help.c-moor.org/u/LindsayHayes)\
**Post date:** [July 20, 2022, 12:47pm UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/9 "2022-07-20T12:47:54Z")

</div>

You have to plot each one individually.

---

<div class="post-metadata">

**Author:** ![yanethp](https://avatars.discourse-cdn.com/v4/letter/y/bbce88/32.png) [@yanethp](https://help.c-moor.org/u/yanethp)\
**Post date:** [July 20, 2022, 3:35pm UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/10 "2022-07-20T15:35:27Z")

</div>

> [@mmccoy](#):
>
> `cor.pairs %>% as_tibble() %>% filter(gene1 %in% my_favorite_genes, gene2 %in% my_favorite_genes)`

 ![Screen Shot 2022-07-20 at 8.34.50 AM](https://canada1.discourse-cdn.com/flex027/uploads/c_moor/original/1X/f7f28bbcb671cb9ce14844d1e3d18aed6a114c4d.jpeg)  
I’m still ruining into errors.

---

<div class="post-metadata">

**Author:** ![mmccoy](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@mmccoy](https://help.c-moor.org/u/mmccoy)\
**Post date:** [July 20, 2022, 8:25pm UTC](https://help.c-moor.org/t/plot-2d-scatter-plots-comparing-gene-expression-of-two-genes/304/11 "2022-07-20T20:25:07Z")

</div>

Hi @yanethp , your variable is named “my\_favorite\_gene”, not “my\_favorite\_genes”. They need to match.
