Griding ggplot object in r 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
&gt; 


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


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
  &lt;chr&gt; &lt;chr&gt;          &lt;dbl&gt;
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?
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 &lt;- 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")




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")




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 = &lt;pointer: 0x55bfc0db9350&gt;)


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 = &lt;pointer: 0x55bfc0db9350&gt;)