簡體   English   中英

底部的通用x軸標簽和圖例使用grid.arrange

[英]Universal x axis label and legend at bottom using 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軸應該是一個textGrob沿線

bottom=textGrob("X Axis")

但是,如果我將其添加到代碼中,則圖例將移動到右側。 如果我指示圖例和標簽都位於底部,則其中一個仍然移動到右側。 因此,問題是,是否有一種方法可以將通用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)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM