簡體   English   中英

在 1 頁上繪制 4 個圖,在 R 中有一個共同的圖例

[英]Plotting 4 plots on 1 page, with a common legend in R

我有以下情節:

dat <- data.frame(
  FunctionClass = factor(c("A", "B", "C", "D", "E", "F", "G", "H", "I",     "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z"), levels=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z")),
  legend = c("A: RNA processing and modification", "B: Chromatin structure and dynamics", "C: Energy production and conversion", "D: Cell cycle control, cell division, chromosome partitioning", "E: Amino acid transport and metabolism", "F: Nucleotide transport and metabolism", "G: Carbohydrate transport and metabolism", "H: Coenzyme transport and metabolism", "I: Lipid transport and metabolism", "J: Translation, ribosomal structure and biogenesis", "K: Transcription", "L: Replication, recombination and repair", "M: Cell wall/membrane/envelope biogenesis", "N: Cell motility", "O: Posttranslational modification, protein turnover, chaperones", "P: Inorganic ion transport and metabolism", "Q: Secondary metabolites biosynthesis, transport and catabolism", "R: General function prediction only", "S: Function unknown", "T: Signal transduction mechanisms", "U: Intracellular trafficking, secretion, and vesicular transport", "V: Defense mechanisms", "W: Extracellular structures", "Y: Nuclear structure", "Z: Cytoskeleton"),
  Differential_Abundance=c(2.1,2.2,3.4,3.3,2,1.1,0.1,0.1,-0.3,-0.9,3,2.1,-0.3,-0.9,-2,-1.2,-0.4,-0.5,-3,-2,-0.3,-2.1,-1.3,-2.2,-3),
  Differential_Abundance2=c(2.1,2.2,3.4,3.3,2,1.1,0.1,0.1,-0.3,-0.9,3,2.1,-0.3,-0.9,-2,-1.2,-0.4,-0.5,-3,-2,-0.3,-2.1,-1.3,-2.2,-3),
  Differential_Abundance3=c(2.1,2.2,3.4,3.3,2,1.1,0.1,0.1,-0.3,-0.9,3,2.1,-0.3,-0.9,-2,-1.2,-0.4,-0.5,-3,-2,-0.3,-2.1,-1.3,-2.2,-3),
  Differential_Abundance4=c(2.1,2.2,3.4,3.3,2,1.1,0.1,0.1,-0.3,-0.9,3,2.1,-0.3,-0.9,-2,-1.2,-0.4,-0.5,-3,-2,-0.3,-2.1,-1.3,-2.2,-3)
)
library(ggplot2)

p <- ggplot(data=dat, aes(x=FunctionClass, y=Differential_Abundance, fill=legend))+
  geom_bar(stat="identity", position=position_dodge(), colour="seashell")
p + guides (fill = guide_legend(ncol = 1))+
  coord_flip() +
  scale_x_discrete(limits = rev(levels(dat$FunctionClass))) +
  xlab("COG Class") +
  ylab("Differential Abundance (Treated/Untreated)")

現在,我想要在同一頁面上有 4 個類似的圖(但有 4 個“Differential_Abundance”值,但具有相同的 y 軸),有 4 個單獨的標簽(A、B、C 和 D)。 我想,我也必須將圖例移到底部,因為它會占用網站太多空間。

無論如何要這樣做?

謝謝!

分面是包含共享一個共同圖例的多個圖的好方法。

首先,我將擴充數據以包含label (我將使用隨機數據來顯示它在分面圖中的不同。)

set.seed(42)
dataug <- do.call(rbind.data.frame, lapply(c("A", "B", "C", "D"), function(lbl) {
  transform(dat, label = lbl,
            Differential_Abundance = Differential_Abundance + runif(nrow(dat), -2, 1))
}))
# or blindly and less-interestingly
dataug <- rbind(transform(data, label="A"), # first frame
                transform(data, label="B"), # second frame
                transform(data, label="C"), # ...
                transform(data, label="D"))

在您的情況下,您可能已經擁有它但作為單獨的框架,在這種情況下,您可能需要手動將label=添加到每個框架,然后使用rbind將它們組合成一個框架。 ggplot2真的更喜歡“長”格式的東西,試着模仿我對dataaug 。)

從這里開始,只需facet_wrapfacet_grid

p <- ggplot(data=dataug, aes(x=FunctionClass, y=Differential_Abundance, fill=legend))+
  geom_bar(stat="identity", position=position_dodge(), colour="seashell")
p + guides (fill = guide_legend(ncol = 1))+
  coord_flip() +
  scale_x_discrete(limits = rev(levels(dat$FunctionClass))) +
  xlab("COG Class") +
  ylab("Differential Abundance (Treated/Untreated)") +
  facet_wrap(~ label)                                               # the only addition

分面圖

一些注意事項:

  • facet_*(..., scales=)允許 x 軸和/或 y 軸是“自由的”,否則假定所有軸在 facet 之間都相同。 (如果您有大量不同的數據,您會想要使用它,否則通常最好不要更改其默認值。
  • facet_wrap(..., nrow=, ncol=)允許您定義寬度/高度(單元格數)
  • facet_grid(xfacet ~ yfacet, ...)允許您通過兩個變量進行分面,每個分面軸上有一個。 可能不是玩家給出的數據和解釋。

編輯:給定您的數據格式,最好將“寬”格式轉換為“長”格式。 StackOverflow 上有很多關於這個主題的資源,但簡而言之,我推薦tidyr::gather來完成這個任務:

dataug <- tidyr::gather(dat, label, Differential_Abundance, -FunctionClass, -legend)

這將適用於我上面的增強繪圖代碼。

您可以使用“ggpubr”包中的“ggarrange”將多個子圖連接到同一圖中。

library(ggplot2)
library(ggpubr)
dat <- data.frame(
  FunctionClass = factor(c("A", "B", "C", "D", "E", "F", "G", "H", "I",     "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z"), levels=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z")),
  legend = c("A: RNA processing and modification", "B: Chromatin structure and dynamics", "C: Energy production and conversion", "D: Cell cycle control, cell division, chromosome partitioning", "E: Amino acid transport and metabolism", "F: Nucleotide transport and metabolism", "G: Carbohydrate transport and metabolism", "H: Coenzyme transport and metabolism", "I: Lipid transport and metabolism", "J: Translation, ribosomal structure and biogenesis", "K: Transcription", "L: Replication, recombination and repair", "M: Cell wall/membrane/envelope biogenesis", "N: Cell motility", "O: Posttranslational modification, protein turnover, chaperones", "P: Inorganic ion transport and metabolism", "Q: Secondary metabolites biosynthesis, transport and catabolism", "R: General function prediction only", "S: Function unknown", "T: Signal transduction mechanisms", "U: Intracellular trafficking, secretion, and vesicular transport", "V: Defense mechanisms", "W: Extracellular structures", "Y: Nuclear structure", "Z: Cytoskeleton"),
  Differential_Abundance=c(2.1,2.2,3.4,3.3,2,1.1,0.1,0.1,-0.3,-0.9,3,2.1,-0.3,-0.9,-2,-1.2,-0.4,-0.5,-3,-2,-0.3,-2.1,-1.3,-2.2,-3)
)



p <- ggplot(data=dat, aes(x=FunctionClass, y=Differential_Abundance, fill=legend))+
  geom_bar(stat="identity", position=position_dodge(), colour="seashell")

p <- p + 
  coord_flip() +
  scale_x_discrete(limits = rev(levels(dat$FunctionClass))) +
  xlab("COG Class") +
  ylab("Differential Abundance (Treated/Untreated)") + labs(color = '')

p1 <- p
p2 <- p
p3 <- p
p4 <- p

ggarrange(p1,p2,p3,p4, nrow = 1, common.legend = T, legend.position = 'top')

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM