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

查看:756
本文介绍了通用x轴标签和图例底部使用grid.arrange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

一个TableGrob对象;通用x轴应该是沿着

  bottom = textGrob(X轴)

但是,如果我将其添加到代码中,图例将移动到右侧。如果我指出传说和标签都应该在底部,其中一个仍然会移到右侧。因此,问题是,是否有一种将通用x轴标签与底部图例相结合的方式?解析方案

问题,所以我想出了这样的事情。

  library(ggplot2)
library(gtable)
库(gridExtra)

dsamp< - diamonds [sample(nrow(diamonds),1000),]
p1 < - qplot(price,carat,data = dsamp,color = clarity )+ theme(legend.position =none)+ xlab()
p2 <-qplot(price,cut,data = dsamp,color = clarity)+ theme(legend.position =none )+ xlab()
p3 < - qplot(price,color,data = dsamp,color = clarity)+ theme(legend.position =none)+ xlab()
p4 <-qplot(price,depth,data = dsamp,color = 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)


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?

解决方案

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)

这篇关于通用x轴标签和图例底部使用grid.arrange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆