r - ggplot2 多个绘图,具有共享图例、一种背景颜色、1 个主标题和 3 个副标题以及非标准布局
问题描述
我正在尝试创建一个将 ggplot2 中制作的 6 个图组合的图。条件是
- 一个主标题
- 三个字幕
- 常用背景色
- 不同大小的地块
- 底部有一个传说
我找到了一些零碎的东西,但我不知道如何把它放在一起。
要添加主标题,我使用了带有 ggplot2 的多图面板的放置标题
layout <- matrix(c(1, 1, 2, 3, 3, 4), nrow = 2, byrow = TRUE)
grid.arrange(A, B, C, D, top = "Title",
layout_matrix = layout)
我找到了函数 multiplot ( http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ ),它允许绘制几个不同大小的图表,但其他要求没有得到满足
plot_list <- list(A, B, C, D)
layout <- matrix(c(1, 1, 2, 3, 3, 4), nrow = 2, byrow = TRUE)
multiplot(plotlist = plot_list, layout = layout)
我还找到了如何创建通用图例,但图表大小相同(为组合 ggplots 添加一个通用图例)
grid_arrange_shared_legend <- function(...) {
plots <- list(...)
g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
lheight <- sum(legend$height)
grid.arrange(
do.call(arrangeGrob, lapply(plots, function(x)
x + theme(legend.position="none"))),
legend,
ncol = 1,
heights = unit.c(unit(1, "npc") - lheight, lheight))
}
grid_arrange_shared_legend(A, B, C, D)
样本数据
DF <- data.frame(ID = 1:10, Pop = (1:10)^2, gr = c("A", rep("B", 8), "A"))
DF_Pie <- DF %>%
group_by(gr) %>%
summarise(Years = n(),
Pop_Years = sum(Pop))
A <- ggplot(DF, aes(x = ID, col = gr, fill = gr)) +
geom_bar()+
theme(legend.position="none")
B <- ggplot(DF_Pie, aes(x = factor(1), y = Years, fill = gr))+
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0)+
theme(legend.position="none")
C <- ggplot(DF, aes(x = ID, y = Pop, col = gr, fill = gr)) +
geom_bar(stat="identity") +
theme(legend.position="bottom")
D <- ggplot(DF_Pie, aes(x = factor(1), y = Pop_Years, fill = gr))+
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) +
theme(legend.position="none")
编辑
感谢@hpesoj626,我知道如何制作所有东西,但条件 3 仍然适用 - 常见的背景颜色
编辑 2
解决方案
从链接的帖子中,有一个ggpubr
解决方案。我看了看包装,发现ggpubr::anotate_figure
它似乎可以做你想做的事。我对情节做了一些调整A, B, C, D
。
A <- ggplot(DF, aes(x = ID, col = gr, fill = gr)) +
geom_bar() +
xlab(NULL) + theme(legend.position = "none")
B <- ggplot(DF_Pie, aes(x = factor(1), y = Years, fill = gr))+
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) + labs(x = NULL, y = NULL) + theme(legend.position = "none")
C <- ggplot(DF, aes(x = ID, y = Pop, col = gr, fill = gr)) +
geom_bar(stat="identity")
D <- ggplot(DF_Pie, aes(x = factor(1), y = Pop_Years, fill = gr))+
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) + labs(x = NULL, y = NULL)
p1 <- ggarrange(A, B, ncol=2)
p2 <- ggarrange(C, D, ncol=2, common.legend = TRUE, legend = "bottom")
p1 <- annotate_figure(p1, top = text_grob("According to years"))
p2 <- annotate_figure(p2, top = text_grob("According to population"))
p <- ggarrange(p1, p2, nrow=2, common.legend = TRUE, legend="bottom", heights = c(3,3.75))
annotate_figure(p, top = text_grob("Main title", face = "bold", size = 16))
推荐阅读
- c - 求方阵的最大对角元素
- php - 使用多个过滤器和分页简化 Eloquent Laravel?
- python - 在Python中计算整数列表中唯一乘法和加法对的数量的有效方法是什么?
- ssl - SSL 配置阻止浏览器建立安全连接
- javascript - AMP 下拉错误:“onchange”可能不会出现在标签“选择”中
- c++ - 线程中的 C++ 方法。传递之间的区别:对象、对象的地址、对象的 std::ref
- linux - 在 For 循环 (Bash) 中使用变量名称
- r - 获取序列的长度
- java - 如何遍历Freemarker中某些类的HashMap和类列表
- android - 发现多个文件具有独立于操作系统的路径