首页 > 解决方案 > ggplot2 多个绘图,具有共享图例、一种背景颜色、1 个主标题和 3 个副标题以及非标准布局

问题描述

我正在尝试创建一个将 ggplot2 中制作的 6 个图组合的图。条件是

  1. 一个主标题
  2. 三个字幕
  3. 常用背景色
  4. 不同大小的地块
  5. 底部有一个传说

它应该看起来像这样: 所需的情节

我找到了一些零碎的东西,但我不知道如何把它放在一起。

要添加主标题,我使用了带有 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)

用 grid.arrange 绘图

我找到了函数 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)

使用 grid_arrange_shared_legend 绘图

样本数据

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 仍然适用 - 常见的背景颜色 hpsoj626 更改后的图表

编辑 2

我创建了以下图表最终结果,但情节和标题之间的空间很大

标签: rplotggplot2

解决方案


从链接的帖子中,有一个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))

在此处输入图像描述


推荐阅读