ggplot`facet_grid`标签被切断 [英] ggplot `facet_grid` label cut off

查看:81
本文介绍了ggplot`facet_grid`标签被切断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggplot中,当使用facet_grid(..., space = "free_y")并且组中的点数很少时,构面标题将被切断.

In ggplot, when using facet_grid(..., space = "free_y") and the number of points within a group is small, the facet title gets cut off.

例如...

library(tidyverse)

d <- tibble(
  x = factor(1:40),
  y = rnorm(40),
  g = c(rep("GROUP 1", 39), "GROUP 2")
)

ggplot(d) +
  aes(x = x, y = y) +
  geom_col() +
  facet_grid(g ~ ., scales = "free_y", space = "free_y") +
  coord_flip() +
  theme(
    strip.text.y = element_text(angle = 0, size = rel(4))
  )

有什么方法可以使GROUP 2文本在多面网格边距处不被截断?我知道我可以拉伸剧情,但这并不是真正令人满意的解决方案-毕竟,构面之间存在着所有的空白空间!有没有办法让标签渗入其中?

Is there any way to get the GROUP 2 text to not be cut-off at the facet grid margins? I'm aware that I can just stretch the plot, but that's not really a satisfying solution -- after all, there's all that margin space between facets! Is there a way to get the label to just bleed into those?

推荐答案

如果将绘图转换为grob,则可以关闭裁剪:

You can turn off the clipping if you convert the plot to a grob:

pg <- ggplotGrob(pp)

for(i in which(grepl("strip-r", pg$layout$name))){
  pg$grobs[[i]]$layout$clip <- "off"
}

grid::grid.draw(pg)

上面的操作关闭了右侧所有切面标签的剪切,这对于使用不同的数据集进行绘图很灵活.

The above turns off clipping for all facet strip labels on the right, which is flexible for plotting using different datasets.

或者,如果您确切知道要关闭的电源,则还可以检查grob&手动指定i的值:

Alternatively, if you know exactly which ones you want to turn off, you can also examine the grob & manually specify the value(s) for i:

> pg
TableGrob (12 x 8) "layout": 18 grobs
    z         cells       name                                   grob
1   0 ( 1-12, 1- 8) background        rect[plot.background..rect.908]
2   1 ( 6- 6, 4- 4)  panel-1-1               gTree[panel-1.gTree.839]
3   1 ( 8- 8, 4- 4)  panel-1-2               gTree[panel-2.gTree.852]
4   3 ( 5- 5, 4- 4)   axis-t-1                         zeroGrob[NULL]
5   3 ( 9- 9, 4- 4)   axis-b-1    absoluteGrob[GRID.absoluteGrob.865]
6   3 ( 6- 6, 3- 3)   axis-l-1    absoluteGrob[GRID.absoluteGrob.872]
7   3 ( 8- 8, 3- 3)   axis-l-2    absoluteGrob[GRID.absoluteGrob.879]
8   3 ( 6- 6, 6- 6)   axis-r-1                         zeroGrob[NULL]
9   3 ( 8- 8, 6- 6)   axis-r-2                         zeroGrob[NULL]
10  2 ( 6- 6, 5- 5)  strip-r-1                          gtable[strip]
11  2 ( 8- 8, 5- 5)  strip-r-2                          gtable[strip]
12  4 ( 4- 4, 4- 4)     xlab-t                         zeroGrob[NULL]
13  5 (10-10, 4- 4)     xlab-b titleGrob[axis.title.x..titleGrob.858]
14  6 ( 6- 8, 2- 2)     ylab-l titleGrob[axis.title.y..titleGrob.855]
15  7 ( 6- 8, 7- 7)     ylab-r                         zeroGrob[NULL]
16  8 ( 3- 3, 4- 4)   subtitle  zeroGrob[plot.subtitle..zeroGrob.905]
17  9 ( 2- 2, 4- 4)      title     zeroGrob[plot.title..zeroGrob.904]
18 10 (11-11, 4- 4)    caption   zeroGrob[plot.caption..zeroGrob.906]

# note that in this case, we actually only need to turn off clipping for
# strip-r-2, the 11th grob.

pg$grobs[[11]]$layout$clip <- "off"

这篇关于ggplot`facet_grid`标签被切断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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