【问题标题】:Using facet tags and strip labels together in ggplot2在 ggplot2 中同时使用构面标签和条形标签
【发布时间】:2019-05-09 16:43:56
【问题描述】:

我想使用ggplot2facet_grid 创建一个图形,如下所示:

# Load ggplot2 library for plotting
library(ggplot2)

# Plot dummy data
p <- ggplot(mtcars, aes(mpg, wt)) 
p <- p + geom_point() 
p <- p + facet_grid(gear ~ cyl)
print(p)

这很好,但由于它是在期刊文章中,因此每个面板还需要用 a、b、c 等标记。egg 包有一个很棒的功能,称为 tag_facet,用于如下:

# Load egg library for tagging
library(egg)
#> Warning: package 'egg' was built under R version 3.5.3
#> Loading required package: gridExtra

# Same plot but with tags for each facet
p <- ggplot(mtcars, aes(mpg, wt)) 
p <- p + geom_point() 
p <- p + facet_grid(gear ~ cyl)
tag_facet(p)

reprex package (v0.2.1) 于 2019 年 5 月 9 日创建

根据需要,我现在如何在每个面板上标注字母。但是,如您所见,我的条形标签消失了!

我的问题:如何在保留条形标签的同时添加标签?

【问题讨论】:

  • 我没用过,但是包stickylabeller可能会做你想做的事。
  • @aosmith 谢谢,我去看看。我很惊讶 tidyverse 没有一个原生函数可以做到这一点,因为它一定是一个相当普遍的问题......
  • 这可能是另一种方法:stackoverflow.com/a/46499601/13997761

标签: r ggplot2 facet facet-grid


【解决方案1】:

您可以查看tag_facet here 的代码。如您所见,该函数显式且有意地删除了刻面带(另请参见documentation 中的“值”)。您可以通过创建自己的函数来解决这个问题,只需从原始代码中删除 theme 调用:

tag_facet2 <- function(p, open = "(", close = ")", tag_pool = letters, x = -Inf, y = Inf, 
    hjust = -0.5, vjust = 1.5, fontface = 2, family = "", ...) {

    gb <- ggplot_build(p)
    lay <- gb$layout$layout
    tags <- cbind(lay, label = paste0(open, tag_pool[lay$PANEL], close), x = x, y = y)
    p + geom_text(data = tags, aes_string(x = "x", y = "y", label = "label"), ..., hjust = hjust, 
        vjust = vjust, fontface = fontface, family = family, inherit.aes = FALSE)
}

【讨论】:

  • 感谢您的回答 (+1),@iod。我在询问后在代码中注意到了这一点。我认为一个更简单的解决方案就是设置strip.text = element_text()在添加标签后重新引入条形标签。
  • 不知道为什么这更简单,但请随心所欲。我只是注意到您还需要重置strip.background,以获取条带的矩形。在两个答案中将您的输出与我的输出进行比较。
  • 也许更简单不是正确的词,但它肯定比重新定义标记功能更简洁。
  • 这取决于你需要多久做一次,我猜。将函数保存为文件并在需要时获取它,而不是调用egg。但当然是 YMMV。
【解决方案2】:

使用 {tagger} 包,这可以变得更简单一些。您可以使用devtools::install_github("eliocamp/tagger") 安装tagger。安装好后,我们来加载它。

library(tagger)
library(ggplot2)

# Plot dummy data
p <- ggplot(mtcars, aes(mpg, wt)) 
p <- p + geom_point() 
p + facet_grid(gear ~ cyl) + tag_facets()

【讨论】:

    【解决方案3】:

    当然,我询问后立即找到解决方案。问题似乎是tag_facet 将条形标签设置为element_blank,这可以通过调用theme 来解决调用tag_facet

    # Load libraries
    library(ggplot2)
    library(egg)
    #> Warning: package 'egg' was built under R version 3.5.3
    #> Loading required package: gridExtra
    
    # Create plot
    p <- ggplot(mtcars, aes(mpg, wt)) 
    p <- p + geom_point() 
    p <- p + facet_grid(gear ~ cyl)
    p <- tag_facet(p)
    p <- p + theme(strip.text = element_text())
    print(p)
    

    reprex package (v0.2.1) 于 2019 年 5 月 9 日创建

    【讨论】:

    • 如果你想要背景,也可以strip.background = element_rect()
    【解决方案4】:

    这只能使用geom_text() 来完成:

    data_text <- data.frame(
      cyl   = c(4, 6, 8, 4, 6, 8, 4, 6, 8),
      gear  = c(3, 3, 3, 4, 4, 4, 5, 5, 5)
      label = c('(a)', '(b)', '(c)', '(d)', '(e)', '(f)', '(g)', '(h)', '(i)')
    )
    
    ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    facet_grid(gear ~ cyl) +
    geom_text(data=data_text, aes(x=12, y=5, label=label), fontface='bold', size=4)
    

    【讨论】:

    • 您可以将check_overlap = TRUE 添加到geom_text 以使它们看起来更好
    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多