ggplot2和gridExtra:完全删除facet_grid中的strip - 不仅不可见 [英] ggplot2 and gridExtra: completely remove strip in facet_grid - not just invisible

查看:515
本文介绍了ggplot2和gridExtra:完全删除facet_grid中的strip - 不仅不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个图表,我把它们放在另一个上面,如下所示:

  library(ggplot2) 
library(gridExtra)
p1 < - ggplot(mtcars,aes(mpg,wt))+ geom_point()
p2 < - ggplot(mtcars,aes(mpg,wt)) + geom_point()
p2 < - p2 + facet_grid(cyl〜。)
grid.arrange(p1,p2,ncol = 1)

为此,我需要顶部和底部图形的x轴排列,但是由于左侧的条带,小面图形比顶部图形更窄。我可以使条带隐形:

 主题(strip.text.y = element_blank())
主题( strip.background = element_blank())

然而,这并没有消除带子所带的空间向上。所以我要么需要一种方法来完全摆脱这个条带,要么有办法将我的分面图分割成单独的图形,然而不知怎的,它们之间共享相同的y轴标签。在我的图中,我有两个不是很高的多面板,并且没有足够的空间让它们各自具有相当大的y轴。



任何建议?

解决方案

我的解决方案是查找条的宽度,然后将这两个图的边距设置为零,但缩小没有带的宽度要稍微小一点(带的宽度),以便它们显示为相同的大小。通过试验和错误,它看起来是大约0.5线宽(但我想你可以通过编程来解决这个问题)。因此,只要确保在没有条形文字的情况下右边的绘图边距比具有不可见条形的文本大0.5行:

 #在左侧添加一条宽度为0.5的线,但将所有其他边距设置为零
p1 < - p1 + theme(plot.margin = unit(c(0,0.5,0 ,0),units =lines))
#将所有边距设置为零,小条将占用不可见空间的幻影量
p2 < - p2 +主题(strip.text.y = element_blank(),
strip.background = element_blank(),
plot.margin = unit(c(0,0,0,0),units =lines))

grid.arrange(p1,p2,ncol = 1)

显然,您可以调整边距(例如,将 plot.margin 中的每个数值向量中的第一个位置加1,以获得沿着每个图的顶部的一条线的边界),只要您在第二个图的右边框中保留了0.5线多余的边距,它们看起来是一样的。


I have two graphs that I'm placing one above the other, in the following way:

library(ggplot2)
library(gridExtra)
p1 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p2 <- p2 + facet_grid(cyl ~ .)
grid.arrange(p1, p2, ncol=1)

For this I need the x axes of the top and bottom graphs to line up, however because of the strip to the left, the faceted graph is narrower than the top graph. I can make the strip invisible using:

theme(strip.text.y = element_blank())
theme(strip.background = element_blank())

However this does not get rid of the space that the strip takes up. So I either need a way to get rid of the strip entirely, or have a way to split my faceted graph into separate graphs, yet somehow sharing the same y-axis label across them. In my graph I have two faceted panels that are not very tall, and there isn't enough space for them to each have a decent-sized y-axis.

Any suggestions?

解决方案

My solution would be to find the width of the strip and then set the margins of both plots to be zero, but shrink the one without the strip to be slightly smaller (the width of the strip) so they appear to be the same size. By trial and error it seems the strip is about 0.5 lines wide (but I guess you could figure this out programatically). Therefore just make sure the right plot margin in the plot without the strip text is 0.5 lines greater than the one with the invisible strip:

#  Add a line of width 0.5 on the left but set all other margins to zero
p1 <- p1 + theme( plot.margin = unit( c(0,0.5,0,0) , units = "lines" ) )
#  Set all margins to zero, the strip will take up a phantom amount of invisible space
p2 <- p2 + theme(strip.text.y = element_blank() , 
  strip.background = element_blank(),
  plot.margin = unit( c(0,0,0,0) , units = "lines" ) )

grid.arrange(p1, p2, ncol=1)

Obviously you can adjust the margins as you wish (e.g. add 1 to the first position in each numeric vector in plot.margin to get a border of one line along the top of each plot), as long as you keep 0.5 lines more margin in the right border of the second plot they will look the same.

这篇关于ggplot2和gridExtra:完全删除facet_grid中的strip - 不仅不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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