【问题标题】:how can I use special characters, superscripts or subscripts in a single label of faceted plots in ggplot2?如何在 ggplot2 的多面图的单个标签中使用特殊字符、上标或下标?
【发布时间】:2021-10-20 20:32:39
【问题描述】:

我想在像这样的多面绘图布局中仅将上标和/或下标添加到条形文本标签的子集:

conc <- runif(nrow(iris), min = 5, max = 10)
df <- iris %>% mutate(mass_area = conc/Petal.Length*Sepal.Length)

melted <- reshape2::melt(df)
bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) + 
  geom_boxplot() +
  theme_ipsum() +
  scale_fill_brewer(palette = "Greens") +
  theme(
    legend.position = "bottom",
    plot.title = element_text(size = 10)) +
  theme(axis.text.x = element_blank(),
        strip.text = element_text(size = 12)) +
  ggtitle(mytitle) +
  xlab("") +
  ylab("") +
  facet_wrap(~variable, scale = "free")
bp1

但是mass_area应该有lab = expression("Chl concentration" ~ (mu ~ g ~ " " ~ cm^{-2}))形式的标签

This 响应很有用,但根据相同的模式标记所有方面。我只需要标记一个。

【问题讨论】:

  • 只是一个用于缩短代码的小技巧,您可以使用labs(x = NULL, y = NULL) 而不是+ xlab("") + ylab("") 。这更短,更容易阅读,更重要的是,使用 NULL 你不会绘制一个空的占位符,你可以使用 ""

标签: r ggplot2 facet-wrap


【解决方案1】:

在您链接的帖子之后,一个选项是使用ifelse 有条件地设置标签,如下所示:

library(dplyr)
library(ggplot2)
library(hrbrthemes)

set.seed(42)

conc <- runif(nrow(iris), min = 5, max = 10)
df <- iris %>% 
  mutate(mass_area = conc/Petal.Length*Sepal.Length)

melted <- reshape2::melt(df) %>% 
  mutate(variable = ifelse(variable == "mass_area",
                           paste0("Chl~concentration ~ (mu ~ g ~ cm^{-2})"), 
                           paste0(variable)))

#> Using Species as id variables

bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) + 
  geom_boxplot() +
  theme_ipsum() +
  scale_fill_brewer(palette = "Greens") +
  theme(
    legend.position = "bottom",
    plot.title = element_text(size = 10)) +
  theme(axis.text.x = element_blank(),
        strip.text = element_text(size = 12)) +
  ggtitle("mytitle") +
  xlab("") +
  ylab("") +
  facet_wrap(~variable, scale = "free", labeller = label_parsed)
bp1

【讨论】:

    【解决方案2】:

    这是一种使用 tidyverse 包的方法。我使用了pivot_longer() 而不是melt()case_when() 而不是 ifelse() 只是为了给你第二个解决方案,但最后它也是一样的,因为它是一个矢量化的 ifelse。 这为您提供与 stefans 解决方案相同的结果。

    附带说明:我已更正了表达式,因此微克中不再有空格。

    library(dplyr)
    library(tidyr)
    library(ggplot2)
     
     conc <- runif(nrow(iris), min = 5, max = 10)
     df <- iris %>% mutate(mass_area = conc/Petal.Length*Sepal.Length)
     
     melted <- df %>% pivot_longer(cols = -Species,
                                   names_to = "variable") %>% 
        mutate(variable = case_when(variable == "mass_area" ~ paste0("Chl~concentration ~ (mu*g ~ cm^{-2})"),
                                    TRUE ~ as.character(variable))
        )
     
     bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) + 
        geom_boxplot() +
        scale_fill_brewer(palette = "Greens") +
        theme(
           legend.position = "bottom",
           plot.title = element_text(size = 10)) +
        theme(axis.text.x = element_blank(),
              strip.text = element_text(size = 12)) +
        xlab("") +
        ylab("") +
        facet_wrap(~variable, scale = "free", label = "label_parsed")
     bp1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 2013-03-22
      相关资源
      最近更新 更多