R 按面分隔y轴标签或删除图例,但保留空间
好吧,我被一个自制的游戏情节难住了 我想做的是有一个三行一列的刻面图,每个刻面有一个不同的y轴标签。y轴的单位都是相同的。这将是最方便的,但谷歌告诉我这可能是不可能的 或者,我使用grid.arrange找到了解决方案,它似乎可以工作。然而,我只想为一个情节保留一个图例,并将其从另外两个情节中删除,但要保持间距,就好像它仍然存在一样,以便一切都排列得很好。几年前有人提出了这个方案,但建议的解决方案被低估了,我无法理清如何在现代ggplot中使用它 感谢您的帮助!使用方面将是最简单的 在使用下面user20560的gridArrange解决方案后,编辑以添加绘图副本。非常接近那里,只是想回到盒子周围的顶部和底部镶嵌面板 我假设(可能是错误的)您希望添加单独的y轴标题,而不是轴标签。[如果需要不同的标签,可以使用R 按面分隔y轴标签或删除图例,但保留空间,r,ggplot2,R,Ggplot2,好吧,我被一个自制的游戏情节难住了 我想做的是有一个三行一列的刻面图,每个刻面有一个不同的y轴标签。y轴的单位都是相同的。这将是最方便的,但谷歌告诉我这可能是不可能的 或者,我使用grid.arrange找到了解决方案,它似乎可以工作。然而,我只想为一个情节保留一个图例,并将其从另外两个情节中删除,但要保持间距,就好像它仍然存在一样,以便一切都排列得很好。几年前有人提出了这个方案,但建议的解决方案被低估了,我无法理清如何在现代ggplot中使用它 感谢您的帮助!使用方面将是最简单的 在使用下面u
facet\u grid
中的scales
参数]
将有一个ggplot方法来实现这一点,但这里有一些方法可以让你自己调整grobs
因此,使用mtcars数据集作为示例
library(ggplot2)
library(grid)
library(gridExtra)
单程
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)
如果您要添加的是axis标题,我应该问您为什么需要不同的标题-刻面条文本不能吗?我知道这是一篇旧文章,但找到它后,我无法得到@user20560对工作的响应 我编辑了@user20560的
网格。额外的方法如下:
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))
库(ggplot2)
图书馆(gridExtra)
图书馆(网格)
#为每个级别的分组变量和y轴标签创建一个绘图
p1我也很难让(上面)的第一种方法起作用。这可能是因为ggplot2的内部已经进化,不能保证这些内部应该保持不变。在任何情况下,以下是当前有效的版本:
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)
library(ggplot2)#来自sessionInfo():ggplot2_2.1.0
图书馆(网格)
p根据Axeman和aosmith的评论(谢谢),下面是一种使用ggplot2 2.2.0版的刻面标签来实现这一点的方法
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()))
这确实是我想要的。我没有想到仅仅使用刻面条文本的(明显的)答案,但我认为通过轴标签的标题更清晰、更标准,因为它们实际上描述的是单位(所有三个图的单位都是mm,但它们是不同事物的mm)。上述使用网格排列的解决方案运行良好,只是它还删除了每个镶嵌面面板周围的框。我不是很了解grob,所以我不知道该怎么办。我会尝试在这里的某个地方添加一份情节副本…@IcebergSlim;您可以更改面板.边框的厚度。例如,使用上面的绘图p3:p3+theme\u bw()+theme(panel.border=element\u rect(fill=NA,color=“black”,size=2))
,尽管可能只要theme\u bw()
就足够了。您可能还发现更改主题plot.margin
以控制排列的绘图之间的空间也很有用。嗯……面板边框对我没有多大作用。边界显示正确,直到我进入并操纵grob。我不确定为什么更改导向框会影响打印边界。也许我正在进入虫子的领地。谢谢你的建议@冰山;抱歉,回答代码中有错误-我缺少一个[[“grobs”]]
-已更正-似乎没有问题now@user3420448 ; 由于ggplot2的几次重要重写改变了内部绘图对象结构,这似乎不再有效。事实证明,这些答案相当不稳定。其他一些答案可能有助于/提供alt方法。这些答案有不同的x轴。我相信OP想要三个共享x轴的绘图,但y轴不同。根据OP上次的编辑,图片显示了不同的x轴。如果你想共享x轴,那么你可以使用facet\u grid
,如下面@Richard DiSalvo的回答所示。在当前ggplot2
版本中实现这一点的一个好方法是在facet\u grid
中使用switch='y'
,然后使用主题设置删除y轴标签和条形背景。不需要grid
操作。听起来您希望ggplot2\U 2.2.0使用switch.placement=“outside”
。有关示例,请参见答案。
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()))