【问题标题】:Adding subscripts and symbols to facet_wrap in ggplot2在 ggplot2 中为 facet_wrap 添加下标和符号
【发布时间】:2021-10-25 19:18:01
【问题描述】:

我正在尝试创建一个随时间变化的某些天气状况的 2x2 刻面图,但在为某些刻面标题添加度数符号和上标时遇到了麻烦。

weatherPLOT = data.frame(weather = rep(c("Soil Temperature (C)", 
                                        "Snow Depth (m)", 
                                        "Air Temperature (C)", 
                                        "Discharge (m3/sec)"), each = 366),
                           day = 1:366,
                           mean = 3, # Obvious place holders,
                           Lo95 = 2, # Are actual numbers in real code
                           Hi95 = 4)

ggplot(aes(y = mean, x = day), data = weatherPLOT) + 
  geom_ribbon(aes(ymin = Lo95, ymax = Hi95), alpha = 0.25) +
  geom_path(size = 1) + 
  theme(axis.title.y = element_blank()) + xlab("Julian Day") +
  facet_wrap( ~ weather, nrow = 2, ncol = 2, scales = "free")

我知道诀窍是在 facet_wrap 中使用 labeller,但我无法让它工作 - 我只是想在 (C) 之前添加一个度数符号并制作 3 in (m3/sec)上标。

【问题讨论】:

    标签: r ggplot2 facet-wrap


    【解决方案1】:

    最简单的方法是将文本值本身更改为适当的符号并使用 ggtext 包进行格式化。

    \u00b0 是度数符号的 Unicode 值。 <sup>3</sup> 是上标 3 的 ggtext Markdown 代码。您可以使用 ggtext::element_markdown() 指定主题文本应为 markdown。

    library(ggplot2)
    weatherPLOT = data.frame(weather = rep(c("Soil Temperature (\u00b0C)", 
                                             "Snow Depth (m)", 
                                             "Air Temperature (\u00b0C)", 
                                             "Discharge (m<sup>3</sup>/sec)"), each = 366),
                             day = 1:366,
                             mean = 3, # Obvious place holders,
                             Lo95 = 2, # Are actual numbers in real code
                             Hi95 = 4)
    
    ggplot(aes(y = mean, x = day), data = weatherPLOT) + 
      geom_ribbon(aes(ymin = Lo95, ymax = Hi95), alpha = 0.25) +
      geom_path(size = 1) + 
      labs(y = "", x = "Julian Day") +
      theme(strip.text = ggtext::element_markdown()) +
      facet_wrap( ~ weather, nrow = 2, ncol = 2, scales = "free")
    

    reprex package (v2.0.0) 于 2021 年 8 月 25 日创建

    【讨论】:

    • 这就是我最初尝试的方法,但无法使格式正常工作 - 感谢您的快速修复 :)
    【解决方案2】:

    另一种方法如下:

    weatherPLOT %>%
      mutate(weather = factor(weather,
                              labels = c(bquote('Air Temperature'*degree*C),
                                         "Discharge~(m^{3}/sec)",
                                         "Snow~Depth~(m)",
                                         bquote('Soil Temperature'*degree*C)))) %>% 
      ggplot(aes(y = mean, x = day)) + 
      geom_ribbon(aes(ymin = Lo95, ymax = Hi95), alpha = 0.25) +
      geom_path(size = 1) + 
      theme(axis.title.y = element_blank()) + xlab("Julian Day") +
      facet_wrap( ~ weather, nrow = 2, ncol = 2, scales = "free", labeller =  label_parsed)
    

    【讨论】:

      猜你喜欢
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      相关资源
      最近更新 更多