【问题标题】:ggplot2: Match plot area height of combined plotsggplot2:匹配组合图的绘图区域高度
【发布时间】:2017-12-28 13:30:06
【问题描述】:
library(ggplot2)
library(gridExtra)

df1 <- data.frame(x=c("A1","A2","A3","A4"),something=c(10,18,24,32),col=rep(c("A","B"),2))
df2 <- data.frame(x=c("C1","C2","C3","C4"),somethingelse=c(10543,182334,242334,32255),col=rep(c("A","B"),2))


p1 <- ggplot(df1,aes(x,something,fill=col))+
  ggtitle("Plot")+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")
p2 <- ggplot(df2,aes(x,somethingelse,fill=col))+
  ggtitle("Plot")+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")

我有这两个图,我想将它们与相等的宽度、共同的标题和图例组合在一起。我认为与其拉出标题和传说 grobs,不如将其留在顶部情节并将其隐藏在第二个情节中。这样,我就不必在grid.arrange() 中手动设置高度了。

#using loops to generalise to n plots
plist <- list()
plist[[1]] <- p1
plist[[2]] <- p2
grobs <- list()

for (i in 1:length(plist)){
  if(i!=1) plist[[i]] <- plist[[i]]+theme(legend.position="none",plot.title=element_blank())
  grobs[[i]] <- ggplotGrob(plist[[i]])
  widths[[i]] <- grobs[[i]]$widths[2:5]
}

# fix widths
maxwidth <- do.call(grid::unit.pmax, widths)
for (i in 1:length(grobs)){
  grobs[[i]]$widths[2:5] <- as.list(maxwidth)
}

#plot
pgrob = do.call("arrangeGrob",grobs)
grid.arrange(pgrob)

但是,这里的地块高度是非常不同的。所以我手动将所有地块的高度设置为第一个。

for (i in 1:length(grobs)){
  grobs[[i]]$heights[2:5] <- grobs[[1]]$heights[2:5]
}

#plot
pgrob = do.call("arrangeGrob",grobs)
grid.arrange(pgrob)

现在,我在第二个情节上方得到了这个巨大的空白区域。我该如何摆脱它?

【问题讨论】:

  • 我真的建议坚持使用ggpubr。非常适合自定义出版质量图(对齐图、图例等)

标签: r ggplot2


【解决方案1】:

您可以使用 ggpubr 包为两个图设置相同的图例(无需提取 grobs 和调整高度)。

library(ggpubr)
figure <- ggarrange(p1, p2, nrow = 2, align = "v", common.legend = TRUE)
annotate_figure(figure, fig.lab = "Plot")

解释:

  • align = "v" 垂直对齐绘图
  • common.legend = TRUE 为两个图设置相同的图例
  • annotate_figure 添加共享标签

使用过的数据:

df1 <- data.frame(x=c("A1","A2","A3","A4"),something=c(10,18,24,32),col=rep(c("A","B"),2))
df2 <- data.frame(x=c("C1","C2","C3","C4"),somethingelse=c(10543,182334,242334,32255),col=rep(c("A","B"),2))

library(ggplot2)
p1 <- ggplot(df1,aes(x,something,fill=col))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")
p2 <- ggplot(df2,aes(x,somethingelse,fill=col))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")

【讨论】:

    【解决方案2】:

    如果您可以重组并rbind 您的数据框,您可以使用facet_wrap(..., scale = "free") 来获得您想要的。

    library(ggplot2)
    library(gridExtra)
    
    df1 <- data.frame(x=c("A1","A2","A3","A4"),y=c(10,18,24,32),col=rep(c("A","B"),2), type = "something")
    df2 <- data.frame(x=c("C1","C2","C3","C4"),y=c(10543,182334,242334,32255),col=rep(c("A","B"),2), type = "somethingelse")
    
    
    df <- rbind(df1, df2)
    
    ggplot(df,aes(x,y,fill=col))+
        ggtitle("Plot")+
        geom_bar(stat="identity")+
        facet_wrap(~type, ncol = 1, scales = "free") +
        theme(legend.position="top",
              legend.justification="right",
              legend.direction="horizontal")
    

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      相关资源
      最近更新 更多