【问题标题】:Remove some of the axis labels in ggplot faceted plots删除 ggplot 多面图中的一些轴标签
【发布时间】:2014-10-27 16:56:29
【问题描述】:

我用ggplot2 包和facet_wrap 函数创建了一个类似the one here 的图,我想抑制一些x 轴文本以使其更清晰。

例如,这里如果 x 轴刻度只出现在框 D、F、H 和 J 上会更清晰。

我怎么能这样做?提前致谢!

EDIT:可重现的代码

library(ggplot2)
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)
d + facet_wrap(~ color, nrow = 1)

【问题讨论】:

  • 您的问题与stackoverflow.com/questions/5380417/…非常相似(如果不相同)
  • 我认为你的问题是关于修改比例,但在这里我不想修改比例(顺便说一下我已经手动修复了)。我想让它出现在某些方面,但不是全部(例如方面 D、F、H 和 J)。
  • 有趣的问题,但最好有一个可重现的代码。您能否添加它以便复制/粘贴/实验方便? (尤其是因为您的数据看起来像 diamonds

标签: r ggplot2 facet gtable


【解决方案1】:

如果您愿意在网格/grob 级别工作,那绝对是可行的。

首先,我们为您的多面图分配一个 ggplot 对象

my_plot <- d + facet_wrap(~ color, nrow = 1)

然后,我们加载 gtable,以便我们可以使用/操作较低级别的对象。

library(gtable)

## Loading required package: grid

现在,我们将 ggplot 对象提取到 TableGrob 中(对于冗长的输出表示歉意,但我认为它有助于显示构面图的底层结构):

plot_tab <- ggplotGrob(my_plot)
print(plot_tab)

## TableGrob (8 x 25) "layout": 33 grobs
##     z         cells       name                                    grob
## 1   0 ( 1- 8, 1-25) background          rect[plot.background.rect.263]
## 2   1 ( 4- 4, 4- 4)    panel-1                 gTree[panel-1.gTree.53]
## 3   2 ( 4- 4, 7- 7)    panel-2                 gTree[panel-2.gTree.68]
## 4   3 ( 4- 4,10-10)    panel-3                 gTree[panel-3.gTree.83]
## 5   4 ( 4- 4,13-13)    panel-4                 gTree[panel-4.gTree.98]
## 6   5 ( 4- 4,16-16)    panel-5                gTree[panel-5.gTree.113]
## 7   6 ( 4- 4,19-19)    panel-6                gTree[panel-6.gTree.128]
## 8   7 ( 4- 4,22-22)    panel-7                gTree[panel-7.gTree.143]
## 9   8 ( 3- 3, 4- 4)  strip_t-1    absoluteGrob[strip.absoluteGrob.211]
## 10  9 ( 3- 3, 7- 7)  strip_t-2    absoluteGrob[strip.absoluteGrob.217]
## 11 10 ( 3- 3,10-10)  strip_t-3    absoluteGrob[strip.absoluteGrob.223]
## 12 11 ( 3- 3,13-13)  strip_t-4    absoluteGrob[strip.absoluteGrob.229]
## 13 12 ( 3- 3,16-16)  strip_t-5    absoluteGrob[strip.absoluteGrob.235]
## 14 13 ( 3- 3,19-19)  strip_t-6    absoluteGrob[strip.absoluteGrob.241]
## 15 14 ( 3- 3,22-22)  strip_t-7    absoluteGrob[strip.absoluteGrob.247]
## 16 15 ( 4- 4, 3- 3)   axis_l-1 absoluteGrob[axis-l-1.absoluteGrob.199]
## 17 16 ( 4- 4, 6- 6)   axis_l-2         zeroGrob[axis-l-2.zeroGrob.200]
## 18 17 ( 4- 4, 9- 9)   axis_l-3         zeroGrob[axis-l-3.zeroGrob.201]
## 19 18 ( 4- 4,12-12)   axis_l-4         zeroGrob[axis-l-4.zeroGrob.202]
## 20 19 ( 4- 4,15-15)   axis_l-5         zeroGrob[axis-l-5.zeroGrob.203]
## 21 20 ( 4- 4,18-18)   axis_l-6         zeroGrob[axis-l-6.zeroGrob.204]
## 22 21 ( 4- 4,21-21)   axis_l-7         zeroGrob[axis-l-7.zeroGrob.205]
## 23 22 ( 5- 5, 4- 4)   axis_b-1 absoluteGrob[axis-b-1.absoluteGrob.150]
## 24 23 ( 5- 5, 7- 7)   axis_b-2 absoluteGrob[axis-b-2.absoluteGrob.157]
## 25 24 ( 5- 5,10-10)   axis_b-3 absoluteGrob[axis-b-3.absoluteGrob.164]
## 26 25 ( 5- 5,13-13)   axis_b-4 absoluteGrob[axis-b-4.absoluteGrob.171]
## 27 26 ( 5- 5,16-16)   axis_b-5 absoluteGrob[axis-b-5.absoluteGrob.178]
## 28 27 ( 5- 5,19-19)   axis_b-6 absoluteGrob[axis-b-6.absoluteGrob.185]
## 29 28 ( 5- 5,22-22)   axis_b-7 absoluteGrob[axis-b-7.absoluteGrob.192]
## 30 29 ( 7- 7, 4-22)       xlab             text[axis.title.x.text.249]
## 31 30 ( 4- 4, 2- 2)       ylab             text[axis.title.y.text.251]
## 32 31 ( 4- 4,24-24)  guide-box                       gtable[guide-box]
## 33 32 ( 2- 2, 4-22)      title               text[plot.title.text.261]

我的 negating-look-ahead-regex-fu 今天早上不工作了,所以如果有人对此有更短的正则表达式可以编辑它或发表评论,那就太棒了。基本上,我们会过滤掉您不想要的 x 轴元素(在本地再次打印以查看消失的内容)。

plot_filtered <- gtable_filter(plot_tab, 
                     "(background|panel|strip_t|axis_l|xlab|ylab|guide-box|title|axis_b-[1357])",
                     trim=FALSE)

现在我们进行实际的绘图:

grid.newpage()
grid.draw(plot_filtered)

【讨论】:

  • 我不认为你的 negating-look-ahead-regex-fu 可以使用 gtable_filter b/c 它不允许将 perl=TRUE 传递给底层的 grepl() 表达式,所以不能使用像(?!axis_b) 这样的表达式。也不可能通过negate=TRUE 来否定整个正则表达式。
【解决方案2】:

这个answergtable_filter_remove() 函数用于简单的负子集。根据p_tab$layout$name 的输出,我们必须删除axis-b-2-1axis-b-4-1axis-b-6-1 才能得到我们想要的。

library(ggplot2)

d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)

my_plot <- d + facet_wrap(~ color, nrow = 1)

library(gtable)
p_tab <- ggplotGrob(my_plot)
p_tab$layout$name

#>  [1] "background"  "panel-1-1"   "panel-2-1"   "panel-3-1"   "panel-4-1"  
#>  [6] "panel-5-1"   "panel-6-1"   "panel-7-1"   "axis-t-1-1"  "axis-t-2-1" 
#> [11] "axis-t-3-1"  "axis-t-4-1"  "axis-t-5-1"  "axis-t-6-1"  "axis-t-7-1" 
#> [16] "axis-b-1-1"  "axis-b-2-1"  "axis-b-3-1"  "axis-b-4-1"  "axis-b-5-1" 
#> [21] "axis-b-6-1"  "axis-b-7-1"  "axis-l-1-7"  "axis-l-1-6"  "axis-l-1-5" 
#> [26] "axis-l-1-4"  "axis-l-1-3"  "axis-l-1-2"  "axis-l-1-1"  "axis-r-1-7" 
#> [31] "axis-r-1-6"  "axis-r-1-5"  "axis-r-1-4"  "axis-r-1-3"  "axis-r-1-2" 
#> [36] "axis-r-1-1"  "strip-t-1-1" "strip-t-2-1" "strip-t-3-1" "strip-t-4-1"
#> [41] "strip-t-5-1" "strip-t-6-1" "strip-t-7-1" "xlab-t"      "xlab-b"     
#> [46] "ylab-l"      "ylab-r"      "guide-box"   "subtitle"    "title"      
#> [51] "caption"     "tag"

gtable_filter_remove <- function (x, name, trim = TRUE){
  matches <- !(x$layout$name %in% name)
  x$layout <- x$layout[matches, , drop = FALSE]
  x$grobs <- x$grobs[matches]
  if (trim) 
    x <- gtable_trim(x)
  x
}

p_filtered <- gtable_filter_remove(p_tab, name = paste0("axis-b-", c(2, 4, 6), "-1"),
                                   trim = FALSE)

library(grid)
grid.newpage()
grid.draw(p_filtered)

reprex package (v0.2.0.9000) 于 2018 年 8 月 26 日创建。

【讨论】:

    【解决方案3】:

    还有什么更大的原因,为什么你决定只显示一行? 只需设置facet_wrap()的参数nrow = 2,转动x轴文字: 将theme(aspect.ratio=1) 更改为theme(aspect.ratio = 1,axis.text.x = element_text(angle = 90, hjust = 0,vjust=.5))

    所以结果应该是这样的:

    library(ggplot2)
    d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
    xlim(0, 2) + stat_binhex(na.rm = TRUE) +
    theme(aspect.ratio = 1,axis.text.x = element_text(angle = 90, hjust = 0,vjust=.5))
    d + facet_wrap(~ color, nrow = 2)
    

    我知道这可能无法准确回答您的问题,但根据我的理解,我认为您可能期待着让情节不那么过载。

    这对你有用吗?

    【讨论】:

      猜你喜欢
      • 2016-05-07
      • 2012-03-20
      • 2014-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 2019-06-08
      • 2021-11-04
      相关资源
      最近更新 更多