ggplot2:匹配绘图区域高度的组合图

时间:2017-12-28 13:30:07

标签: r ggplot2

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")

plot1 plot2

我有两个图,我想用相同的宽度,共同的标题和图例组合在一起。我想的不是拔出标题和传说中的凹凸,我会将它留在顶部的情节并隐藏在第二个情节中。这样,我就不必在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)

plot3

但是,这里的情节区域高度非常不同。所以我手动将所有绘图的高度设置为第一个。

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)

plot4

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

2 个答案:

答案 0 :(得分:3)

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

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

enter image description here Explantion:

  • 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")

答案 1 :(得分:1)

如果您可以重新构建并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")