通用x轴标签和图例底部使用grid.arrange(Universal x axis label and legend at bottom using grid.arrange)

编程入门 行业动态 更新时间:2024-10-27 22:31:04
通用x轴标签和图例底部使用grid.arrange(Universal x axis label and legend at bottom using grid.arrange)

我正在尝试使用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)

enter image description here

更多推荐

本文发布于:2023-07-26 08:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1272886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图例   标签   arrange   grid   Universal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!