【问题标题】:How can I format the axis tick labels for each ggplot panel separately?如何分别格式化每个 ggplot 面板的轴刻度标签?
【发布时间】:2021-10-11 16:08:51
【问题描述】:

我需要创建一个多面板图,其中 y 轴(因子)的刻度标签对于某些面板是斜体,而对于其他面板是纯文本。

这是一个可重复的示例,使用虚构的数据,我尝试过的一件事:

library(ggplot2)
## Example data
df <- data.frame(var = c("Grass cover", "River depth", "Canis familiaris", "Canis lupus", "Canis latrans"), 
                 coef = c(0.6, 0.4, 0.8, 0.1, 0.5),
                 se = c(0.3, 0.4, 0.4, 0.05, 0.2),
                 panel = c(rep("Environment", 2), rep("Canid abundance", 3)))
## multi-panel plot
ggplot(df, aes(y = var, x = coef, xmin = coef - se, xmax = coef + se)) + 
  geom_pointrange() +
  facet_wrap(~ panel, scales = "free_y", ncol = 1) +
  labs(y = NULL) +
  theme(axis.text.y = element_text(face = c(rep("plain", 2), rep("italic", 3))))

请注意,只有“Canis lupus”在顶部面板中以斜体显示。所需的数字将在第一个面板中的 所有 科学名称(y 轴标签)为斜体,但底部面板中的标签应该是普通的。在真实数据中,我有 4 个面板,其中 2 个需要斜体。

似乎element_text() 中的矢量化face 参数在面板之间被回收。我也尝试过axis.text.y = element_text(face = rep("italic", 3), c(rep("plain", 2))),这会导致所有 5 个 y 轴标签都变为斜体。

如果可能,我不希望使用多个 ggplot 对象(即grid_arrange()cowplot())。不过,我愿意接受任何允许我调整所有视觉格式元素的解决方案。


我认为使用expression() 一定有解决方案,但我一直无法解决。

【问题讨论】:

  • 如果你找不到 ggplot2 原生的东西,你可以考虑 ggtree (github) 包,它支持标签中的降价等。

标签: r ggplot2 formatting facet-wrap


【解决方案1】:

实现所需结果的一种选择是使用ggtext 包,它允许使用markdown 语法和/或HTML/CSS 来设置标签和主题元素的样式。

  1. 使用theme(axis.text.y = ggtext::element_markdown()) y 轴标签将被解析为降价代码。
  2. 将要以斜体显示的标签包裹在* ... * 中。
library(ggplot2)
library(ggtext)

df <- data.frame(var = c("Grass cover", "River depth", "Canis familiaris", "Canis lupus", "Canis latrans"), 
                 coef = c(0.6, 0.4, 0.8, 0.1, 0.5),
                 se = c(0.3, 0.4, 0.4, 0.05, 0.2),
                 panel = c(rep("Environment", 2), rep("Canid abundance", 3)))
df$var <- ifelse(df$panel %in% "Canid abundance", paste0("*", df$var, "*"), df$var)

## multi-panel plot
ggplot(df, aes(y = var, x = coef, xmin = coef - se, xmax = coef + se)) + 
  geom_pointrange() +
  facet_wrap(~ panel, scales = "free_y", ncol = 1) +
  labs(y = NULL) +
  theme(axis.text.y = ggtext::element_markdown())

【讨论】:

    【解决方案2】:

    这是另一种选择。

    library(tidyverse)
    
    df <- tibble(var = c("Grass cover", "River depth", "Canis familiaris", "Canis lupus", "Canis latrans"),
                 coef = c(0.6, 0.4, 0.8, 0.1, 0.5),
                 se = c(0.3, 0.4, 0.4, 0.05, 0.2),
                 panel = c(rep("Environment", 2), rep("Canid abundance", 3)))
    
    df %>%
      mutate(lab = map2_chr(var, panel, 
                            ~ifelse(.y == "Canid abundance", 
                                    paste0('italic("', .x,'")'), 
                                    paste0('"', .x,'"')))) %>%
      ggplot(aes(y = lab, x = coef, xmin = coef - se, xmax = coef + se)) + 
      geom_pointrange() +
      facet_wrap(~ panel, scales = "free_y", ncol = 1) +
      labs(y = NULL) +
      scale_y_discrete(label = function(x) parse(text = x))
    

    【讨论】:

      猜你喜欢
      • 2017-03-16
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多