我正在尝试使用grid.arrange来绘制多个图形。 具体来说,我希望情节在底部有一个共享的x轴标签和一个图例。 这里是我用于安排( 这里的工作示例 )的代码,sans通用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))图例是一个TableGrob对象; 通用的x轴应该是沿着线条的文字
bottom=textGrob("X Axis")但是,如果我将其添加到代码中,图例将移动到右侧。 如果我指出传说和标签都应该在底部,其中一个仍然会移到右侧。 因此,问题是,是否有一种将通用x轴标签与底部图例相结合的方法?
I am trying to plot a number of graphs next to each other using grid.arrange. Specifically, I would like the plots to have a shared x axis label and a legend at the bottom. Here is the code I am using for the arrangement (working example here), sans the universal x axis:
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))The legend is a TableGrob object; the universal x axis should be a textGrob along the lines of
bottom=textGrob("X Axis")However, if I add that to the code, the legend is moved to the right side. If I indicate that both legend and label should be at the bottom, one of them still moves to the right side. Thus the question, is there a way of combining a universal x axis label with a legend at the bottom?
最满意答案
有类似的问题,所以我想出了这样的事情。
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)Had a similar issue so I came up with something like this.
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)更多推荐
发布评论