0
$\begingroup$

I have this data frame

head(d)

# A tibble: 6 x 3
  gene  variable       value
1 CCT5  Res_immune     10.6 
2 CCT5  Non_res_immune 10.5 
3 CDK1  Res_immune      9.25
4 CXCL5 Non_res_immune  9.82
5 GBP1  Res_immune      9.04
6 GBP1  Non_res_immune  9.43
> 

And this code

library(ggplot2)
library(ggrepel)
ggplot(d, aes(x = variable, y = value, fill = variable, label = gene))+
  geom_boxplot(alpha = 0.2)+
  geom_point(position = position_jitter(width = 0.2, seed = 2), show.legend = FALSE)+
  geom_text_repel(position = position_jitter(0.2, seed = 2), show.legend = FALSE)

The genes in first column are involved in cytokine signaling pathway in two groups of patients

This is my plot enter image description here

I have another data frame for genes in IL17 signaling in the sample patients like

head(d)

# A tibble: 6 x 3
  gene  variable       value
  <chr> <chr>          <dbl>
1 CDK6  Res_immune     17.6 
2 IL6  Non_res_immune 14.5 
3 CXCL8  Res_immune      1.25

How I can put plots from both data in a grade like below where the name of pathways shown instead of DEL, INS, SNP and total like below picture?

$\endgroup$
1
  • $\begingroup$ you should add a reproducible example of both dataframe (Cytokine and IL-17 signaling) by providing the output of dput(head(d)). It will make things easier for people trying to help you. $\endgroup$
    – dc37
    Feb 10, 2020 at 22:05

1 Answer 1

4
$\begingroup$

What you are looking for is facetting. Using this key word on any search engines you will find dozen of answers describing its use: https://ggplot2.tidyverse.org/reference/facet_grid.html and https://ggplot2.tidyverse.org/reference/facet_wrap.html

To prepare the faceting in ggplot2, a possible way is to bind both dataframes together by first specifying which group they belong such as:

cytokine$Group = "Cytokine"
il17$Group = "IL17"
DF <- rbind(cytokine,il17)
DF

    gene       variable value    Group
1:  CCT5     Res_immune 10.60 Cytokine
2:  CCT5 Non_res_immune 10.50 Cytokine
3:  CDK1     Res_immune  9.25 Cytokine
4: CXCL5 Non_res_immune  9.82 Cytokine
5:  GBP1     Res_immune  9.04 Cytokine
6:  GBP1 Non_res_immune  9.43 Cytokine
7:  CDK6     Res_immune 17.60     IL17
8:   IL6 Non_res_immune 14.50     IL17
9: CXCL8     Res_immune  1.25     IL17

Then, use facet_grid function of ggplot2:

library(ggplot2)
library(ggrepel)
ggplot(DF, aes(x = variable, y = value, fill = variable, label = gene))+
  geom_boxplot(alpha = 0.2)+
  geom_point(position = position_jitter(width = 0.2, seed = 2), show.legend = FALSE)+
  geom_text_repel(position = position_jitter(0.2, seed = 2), show.legend = FALSE)+
  facet_grid(Group~., scales = "free")

enter image description here

Alternatively, you can use facet_grid (or facet_wrap) to have your plot in a single row:

library(ggplot2)
library(ggrepel)
ggplot(DF, aes(x = variable, y = value, fill = variable, label = gene))+
  geom_boxplot(alpha = 0.2)+
  geom_point(position = position_jitter(width = 0.2, seed = 2), show.legend = FALSE)+
  geom_text_repel(position = position_jitter(0.2, seed = 2), show.legend = FALSE)+
  facet_wrap(.~Group, scales = "free")

enter image description here

Reproducible data example

structure(list(gene = c("CCT5", "CCT5", "CDK1", "CXCL5", "GBP1", 
"GBP1"), variable = c("Res_immune", "Non_res_immune", "Res_immune", 
"Non_res_immune", "Res_immune", "Non_res_immune"), value = c(10.6, 
10.5, 9.25, 9.82, 9.04, 9.43), Group = c("Cytokine", "Cytokine", 
"Cytokine", "Cytokine", "Cytokine", "Cytokine")), row.names = c(NA, 
-6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x55bfc0db9350>)

and

structure(list(gene = c("CDK6", "IL6", "CXCL8"), variable = c("Res_immune", 
"Non_res_immune", "Res_immune"), value = c(17.6, 14.5, 1.25), 
    Group = c("IL17", "IL17", "IL17")), row.names = c(NA, -3L
), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x55bfc0db9350>)
$\endgroup$
1
  • $\begingroup$ Sorry how I can place responders and non responders for each group next to each other? I mean for example for OBP responders and non responders being placed next to each other $\endgroup$
    – Exhausted
    Feb 11, 2020 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.