首页 > 解决方案 > 多个绘图的自定义布局和标签 (GGPLOT2)

问题描述

我使用以下代码创建了一个绘图列表:

plots <- list()
for(i in 1:(k*k)){
  plots[[i]] <- ggplot(subset(mean_conf, Names == Names[i]), aes(x=nob)) + 
    geom_line (aes(y = Mean), color = "black") + 
    geom_line (aes(y = Left_Interval), color="black", linetype="twodash") +
    geom_line (aes(y = Right_Interval), color="black", linetype="twodash") + 
    theme(axis.title.x=element_blank(),axis.title.y=element_blank()) + expand_limits(y = 0)
}

使用 grid.arrange 函数(do.call(grid.arrange,plots))带来下图

在此处输入图像描述

我想在我的情节上的特定区域添加文本。像这样的东西: 在此处输入图像描述

我检查了不同的功能,但找不到解决问题的方法。我将不胜感激任何建议、建议和帮助。

标签: rggplot2textlayoutcustomization

解决方案


gridExtra 和 grid 可能是您所追求的布局的朋友。

这是10的首发。

library(ggplot2)
library(gridExtra)
library(grid)


p <- ggplot(mtcars, aes(disp, wt)) + geom_line()

ver <- textGrob("Plots verion 1", gp = gpar(fontsize = 15))
A_top <- textGrob("A", gp = gpar(fontsize = 15))
A_left <- textGrob("A", gp = gpar(fontsize = 15), x = unit(45, "mm"), y = unit(15, "mm") )

B_top <- textGrob("B", gp = gpar(fontsize = 15))
B_left <- textGrob("B", gp = gpar(fontsize = 15), x = unit(45, "mm"), y = unit(15, "mm"))

grid.arrange(ver, A_top, B_top, A_left, p, p, B_left, p, p, 
                  top = textGrob("Title", gp=gpar(fontsize = 20), x = unit(125, "mm")),
                  bottom = textGrob("Subtitle", gp=gpar(fontsize = 10), x = unit(65, "mm")), 
                  ncol = 3, 
                  widths = unit(c(50, 75, 75), "mm"), 
                  heights = unit(c(10, 75, 75), "mm"))


这给了你:

在此处输入图像描述

可以探索以下链接以优化布局

https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html#title-andor-annotations


推荐阅读