代码之家  ›  专栏  ›  技术社区  ›  AAAA

ggplot2多个绘图区,具有共享的图例、一种背景色、一个主标题和三个子标题以及非标准布局

  •  3
  • AAAA  · 技术社区  · 6 年前

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

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

    它看起来应该是这样的:

    我找到了一些零碎的照片,但我不知道怎么穿。

    要添加主标题,我使用了 place title of multiplot panel with ggplot2

    layout<-matrix(c(1,1,2,3,3,4),nrow=2,byrow=true)
    网格。排列(a,b,c,d,top=“title”,
    布局矩阵=布局)
    

    我找到了函数multiplot(http://www.cookbook-r.com/graphs/multiple-graphs_on ou one ou page ou(ggplot2)/>)和it dose allow to plot some charts with different sizes,but the other re不符合要求

    plot_list<-list(A,B,C,D)
    布局矩阵(C(1,1,2,3,3,4),nrow=2,byrow=true)
    multiplot(plot list=plot_list,layout=layout)
    
    
    

    我还发现了如何创建公共图例,但图表大小相同(add a common-legend-for-combined-ggplots>

    网格排列共享图例<-函数(…){
    绘图<-列表(…)
    g<-ggplotgrob(plots[[1]]+theme(legend.position=“bottom”))$grobs
    图例<-g[[其中(SAPPLY(g,函数(x)x$name)==“导向框”)]
    lheight<-总和(图例$height)
    网格。排列(
    do.call(arrangegrob,lapply)(绘图,函数(x)
    x+主题(legend.position=“none”)),
    传奇,
    ncol=1,
    高度=单位C(单位(1,“npc”)-左八,左八)
    }
    
    网格\排列\共享\图例(A、B、C、D)
    
    
    

    样本数据

    df<-data.frame(id=1:10,pop=(1:10)^2,gr=c(“a”,rep(“b”,8),“a”))
    df饼<-df%>%
    分组依据(gr)%>%
    总结(年=n(),
    pop_years=总和(pop)
    
    A<-ggplot(df,aes(x=id,col=gr,fill=gr))。+
    土工格栅()。+
    主题(legend.position=“none”)
    
    b<-ggplot(df_pie,aes(x=因子(1),y=年,fill=gr))。+
    土工格栅(宽度=1,stat=“Identity”)。+
    坐标极坐标(“Y”,开始=0)+
    主题(legend.position=“none”)
    
    C<-ggplot(df,aes(x=id,y=pop,col=gr,fill=gr))。+
    几何条形图(stat=“identity”)。+
    主题(legend.position=“bottom”)
    
    D<-ggplot(df_pie,aes(x=因子(1),y=pop_years,fill=gr))。+
    土工格栅(宽度=1,stat=“Identity”)。+
    坐标极坐标(“Y”,开始=0)+
    主题(legend.position=“none”)
    

    编辑

    多亏了@hpesoj626,我知道如何制作所有东西,但条件3仍然适用-通用背景色

    编辑2

    我已经创建了以下图表but the space between plots and titles are hurge

  • 通用背景色
  • 不同地块大小
  • 底部有一个传说
  • 它看起来应该是这样的: Desired plot

    我找到了一些零碎的照片,但我不知道怎么穿。

    添加我使用的主标题Place title of multiplot panel with 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)
    

    Plot with 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) 
    

    Plot with multiplot

    我还发现了如何创建通用图例,但图表大小相同(Add a common Legend for combined 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)
    

    Plot with 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仍然适用-通用背景色 Chart after hpesoj626  changes

    编辑2

    我创建了以下图表end result但是情节和标题之间的空间是巨大的

    1 回复  |  直到 6 年前
        1
  •  1
  •   hpesoj626    6 年前
    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))
    

    enter image description here