sim*_*bus 4 plot r ggplot2 gridextra grob
我试图使用grid.arrange绘制彼此相邻的多个图形.具体来说,我希望这些图有一个共享的x轴标签和底部的图例.这是我用于排列的代码(这里的工作示例),没有通用x轴:
plot.c <- grid.arrange(arrangeGrob(plot.3 +
theme(legend.position="none"),
plot.2 + theme(legend.position="none"),
plot.4 + theme(legend.position="none"),
nrow=1),
my.legend,
main="Title goes here",
left=textGrob("Y Axis", rot = 90, vjust = 1),
nrow=2,heights=c(10, 1))
Run Code Online (Sandbox Code Playgroud)
图例是TableGrob对象; 通用x轴应该是一个textGrob沿线
bottom=textGrob("X Axis")
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其添加到代码中,则图例将移动到右侧.如果我指示图例和标签都位于底部,则其中一个仍然移动到右侧.因此,问题是,是否有一种方法可以将通用x轴标签与底部的图例相结合?
有类似的问题,所以我想出了类似的东西.
library(ggplot2)
library(gtable)
library(gridExtra)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(price, carat, data=dsamp, colour=clarity) + theme(legend.position="none") + xlab("")
p2 <- qplot(price, cut, data=dsamp, colour=clarity) + theme(legend.position="none") + xlab("")
p3 <- qplot(price, color, data=dsamp, colour=clarity) + theme(legend.position="none") + xlab("")
p4 <- qplot(price, depth, data=dsamp, colour=clarity) + theme(legend.position="none") + xlab("")
legend <- gtable_filter(ggplot_gtable(ggplot_build(p1 + theme(legend.position="bottom"))), "guide-box")
lheight <- sum(legend$height)
p <- arrangeGrob(p1, p2, p3, p4, ncol=2)
theight <- unit(12, "points")
p <- arrangeGrob(p, textGrob("price", gp=gpar(fontsize=12)), heights=unit.c(unit(1, "npc") - theight, theight))
p <- arrangeGrob(p, legend, heights=unit.c(unit(1, "npc") - lheight, lheight), ncol=1)
print(p)
Run Code Online (Sandbox Code Playgroud)
