r - 在一页上绘制 4 个图,并使用 R 中的常见图例

标签 r ggplot2 plot

我有以下情节:

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)。我想,我也必须将图例移到底部,因为它会占用网站太多空间。

无论如何都要这样做吗?

谢谢!

最佳答案

分面是包含共享共同图例的多个图的好方法。

首先,我将扩充数据以包含标签。 (我将使用随机数据来显示它在多面图中的不同。)

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

faceted plot

一些注意事项:

  • facet_*(..., scales=) 允许 x 轴和/或 y 轴“自由”,否则假定构面之间的所有轴都相同。 (如果您有大量不同的数据,则需要使用它,否则通常最好不要更改其默认值。
  • facet_wrap(..., nrow=, ncol=) 允许您定义宽度/高度(单元格数量)
  • facet_grid(xfacet ~ yfacet, ...) 允许您通过两个变量进行分面,每个分面轴上一个。根据您的数据和解释,很可能不是玩家。

编辑:根据您的数据格式,最好从“宽”格式转换为“长”格式。 StackOverflow 上有大量关于此主题的资源,但简而言之,我建议使用 tidyr::gather 来完成此任务:

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

这将与我上面的增强绘图代码一起使用。

关于r - 在一页上绘制 4 个图,并使用 R 中的常见图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59971885/

相关文章:

用密文替换字母

r - 如何平滑ggplot中的曲线线图?

r - 关于如何使用 ggplot2 绘制 mixEM 类型数据的任何建议

r - 使用 facet_grid 将 "title"添加到我的因子

r - 确定使用哪些包

r - 我无法使用 font_import 导入字体

plot - 多曲面图包括 Plotly 中的布局?

python - 我的 PyQt 图的 Y 轴是颠倒的(甚至是文本)?

r - ggplot stat_bin2d 图中的梯度中断

r - 如何使用引导值注释 ggtree 对象?