简体   繁体   English

通过构面分隔y轴标签或删除图例但保留空间

[英]Separate y-axis labels by facet OR remove legend but keep the space

Ok, I'm stumped on a home-brew ggplot. 好吧,我被一个家​​庭酿造的ggplot难倒了。

What I would like to do is have a three row, one column faceted plot with a different y-axis label for each facet. 我想要做的是有一个三行,一列刻面图,每个面都有不同的y轴标签 The units of the y-axis are all the same. y轴的单位都是相同的。 This would be the most convenient, but googling tells me it may not be possible. 这将是最方便的,但谷歌搜索告诉我,这可能是不可能的。

Alternatively, I found this solution using grid.arrange, which seems like it will work. 或者,我使用grid.arrange找到了这个解决方案,看起来它会起作用。 However, I want to keep a legend only for one plot and remove it from the other two, but maintain the spacing as if it were still there so that everything lines up nice. 但是,我想仅为一个绘图保留一个图例并将其从其他两个中移除,但保持间距就像它仍然存在一样,以便所有内容都很好。 Someone had the same problem a few years ago, but the suggested solution is depreciated and I can't sort out how to make it work in modern ggplot. 几年前有人遇到了同样的问题 ,但建议的解决方案已经过折旧,我无法理解如何使其在现代ggplot中运行。

Any help is appreciated! 任何帮助表示赞赏! Using facets would be easiest! 使用facet是最简单的!

Edited to add copy of plot after using user20560's gridArrange solution below. 编辑后使用user20560的gridArrange解决方案添加绘图副本。 Very nearly there, just would like to get back the box around the top and bottom facet panels! 非常接近那里,只想回到顶部和底部面板周围的盒子!

在此输入图像描述

I have assumed (possibly wrongly) that you are wanting to add separate y-axis titles rather than axis labels. 我假设(可能错误地)你想要添加单独的y轴标题而不是轴标签。 [If it is the labels you want different you can use the scales argument in facet_grid ] [如果是你想要的标签不同,可以使用facet_gridscales参数]

There will be a ggplot way to do this but here are a couple of ways you could tweak the grobs yourself. 将有一个ggplot方法来做到这一点,但这里有几种方法可以自己调整凹凸。

So using mtcars dataset as example 所以以mtcars数据集为例

library(ggplot2)
library(grid)
library(gridExtra)

One way 单程

p <- ggplot(mtcars, aes(mpg, wt, col=factor(vs))) +   geom_point() + 
                                 facet_grid(gear ~ .)

# change the y axis labels manually
g <- ggplotGrob(p)
yax <- which(g$layout$name=="ylab")

# define y-axis labels
g[["grobs"]][[yax]]$label <- c("aa","bb", "cc")

# position of labels (ive just manually specified)
g[["grobs"]][[yax]]$y <- grid::unit(seq(0.15, 0.85, length=3),"npc")

grid::grid.draw(g)


在此输入图像描述
Or using grid.arrange 或者使用grid.arrange

# Create a plot for each level of grouping variable and y-axis label
p1 <- ggplot(mtcars[mtcars$gear==3, ], aes(mpg, wt, col=factor(vs))) + 
                                 geom_point() + labs(y="aa") + theme_bw()
p2 <- ggplot(mtcars[mtcars$gear==4, ], aes(mpg, wt, col=factor(vs))) +  
                                 geom_point() + labs(y="bb") + theme_bw()
p3 <- ggplot(mtcars[mtcars$gear==5, ], aes(mpg, wt, col=factor(vs))) +  
                                 geom_point() + labs(y="cc") + theme_bw()

# remove legends from two of the plots
g1 <- ggplotGrob(p1)
g1[["grobs"]][[which(g1$layout$name=="guide-box")]][["grobs"]] <- NULL

g3 <- ggplotGrob(p3)
g3[["grobs"]][[which(g3$layout$name=="guide-box")]][["grobs"]] <- NULL

gridExtra::grid.arrange(g1,p2,g3)

If it is the axis titles you want to add I should ask why you want a different titles - can the facet strip text not do? 如果它是你想要添加的轴标题,我应该问你为什么要一个不同的标题 - 小平面文本不能这样做吗?

Following the comments by Axeman and aosmith (thank you), here's a way to do this using the facet labels using ggplot2 version 2.2.0 根据Axeman和aosmith的评论(谢谢),这里有一种使用ggplot2版本2.2.0的facet标签来做到这一点的方法

library(ggplot2) # From sessionInfo(): ggplot2_2.2.0

ggplot(mtcars, aes(mpg, wt, col=factor(vs))) + geom_point() + 
  facet_grid(gear ~ ., switch = 'y') +
  theme( axis.title.y = element_blank(), # remove the default y-axis title, "wt"
         strip.background = element_rect(fill = 'transparent'), # replace the strip backgrounds with transparent
         strip.placement = 'outside', # put the facet strips on the outside
         strip.text.y = element_text(angle=180)) # rotate the y-axis text (optional)
# (see ?ggplot2::theme for a list of theme elements (args to theme()))

I know this is an old post, but after finding it, I could not get @user20560's response to work. 我知道这是一个旧帖子,但在找到它之后,我无法得到@ user20560对工作的回应。

I've edited @user20560's grid.extra approach as follows: 我编辑了@ user20560的grid.extra方法,如下所示:

library(ggplot2)
library(gridExtra)
library(grid)

# Create a plot for each level of grouping variable and y-axis label
p1 <- ggplot(mtcars[mtcars$gear==3, ], aes(mpg, wt, col=factor(vs))) + 
  geom_point() + labs(y="aa") + theme_bw()
p2 <- ggplot(mtcars[mtcars$gear==4, ], aes(mpg, wt, col=factor(vs))) +  
  geom_point() + labs(y="bb") + theme_bw()
p3 <- ggplot(mtcars[mtcars$gear==5, ], aes(mpg, wt, col=factor(vs))) +  
  geom_point() + labs(y="cc") + theme_bw()

# get the legend as a grob
legend <- ggplotGrob(p1)
legend <- legend$grobs[[which(legend$layout$name=="guide-box")]]
lheight <- sum(legend$height)
lwidth <- sum(legend$width)

# remove the legend from all the plots
p1 <- p1 + theme(legend.position = 'none')
p2 <- p2 + theme(legend.position = 'none')
p3 <- p3 + theme(legend.position = 'none')

# force the layout to the right side
layoutMat <- matrix(c(1,2,3,4,4,4),ncol = 2)

grid.arrange(p1,p2,p3,legend, layout_matrix = layoutMat, ncol = 2, 
             widths = grid::unit.c(unit(1,'npc') - lwidth, lwidth))

This example is somewhat specific to this particular layout. 此示例有点特定于此特定布局。 There is a more general approach on the ggplot2 wiki . ggplot2 wiki上有一种更通用的方法。

I too had trouble getting the first approach in the answer of user20560 (above) to work. 我也无法在user20560 (上面) 的答案中获得第一种方法。 This is probably because the internals of ggplot2 have evolved, and there is no guarantee that these internals should stay the same. 这可能是因为ggplot2的内部已经发展,并且不能保证这些内部结构应该保持不变。 In any case, here is a version that currently works: 无论如何,这是一个当前有效的版本:

library(ggplot2) # From sessionInfo(): ggplot2_2.1.0
library(grid)

p <- ggplot(mtcars, aes(mpg, wt, col=factor(vs))) + geom_point() + facet_grid(gear ~ .)
g <- ggplotGrob(p)
yax <- which(g$layout$name == "ylab")
g[["grobs"]][[yax]]$children[[1]]$label <- c('fo','bar','foobar')
g[["grobs"]][[yax]]$children[[1]]$y <- grid::unit(seq(0.15, 0.85, length=3), "npc")
grid.draw(g)

Note that this is the approach that keeps the facets and does not repeat the x-axes. 请注意,这是保持构面并且不重复x轴的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM