在 ggplot2 中使用 facet_wrap 上的公式

Using formulae on facet_wrap in ggplot2

我正在尝试用表达式替换 ggplot 条形图上的 facet_wrap 标题,但我没有运气。我试过 and here 但似乎都不适合我。

整个数据集很大,所以这里有一些虚拟数据来说明问题。

library(tidyr)
library(ggplot2)

    data<-data.frame(species = rep(c("oak", "elm", "ash"), each = 5),
resp_1 = (runif(15, 1,100)),
resp_2 = (runif(15, 1,100)),
resp_3 = (runif(15, 1,100)),
resp_4 = (runif(15, 1,100)),
resp_5 = (runif(15, 1,100)))

### transform to longform with tidyr

data_2 <- gather(data, response, result, resp_1:resp_5, factor_key=TRUE)

### plot with ggplot2

ggplot(data_2, aes(x = species, y = result, fill = species))+
  geom_bar(stat = 'sum')+
  facet_wrap(~ response)

### here are the labels I'd like to see on the facets

oxygen <-expression ("Oxygen production (kg/yr)")
runoff <-expression("Avoided runoff " ~ (m ^{3} /yr))
co <- expression("CO removal (g/yr)")
o3 <- expression("O"[3]~" removal (g/yr)")
no2 <- expression("NO"[2]~" removal (g/yr)")

labels <- c(oxygen, runoff, co, o3, no2)

### this doesn't work 

ggplot(data_2, aes(x = species, y = result, fill = species))+
  geom_bar(stat = 'sum')+
  facet_wrap(~ response, labeller = labeller(response = labels))

### close, but doesn't work

levels(data_2$response)<-labels


ggplot(data_2, aes(x = species, y = result, fill = species))+
  geom_bar(stat = 'sum')+
  facet_wrap(~ response, labeller = labeller(response = labels))

### produces an error

ggplot(data_2, aes(x = species, y = result, fill = species))+
  geom_bar(stat = 'sum')+
  facet_wrap(~ response, labeller = label_parsed)

我还想删除标题为“n”的灰色图例中的第二个。

现在您的表达式名称与用作构面的值不匹配。所以我建议将标签存储在表达式中

labels <- expression(
  resp_1 = "Oxygen production (kg/yr)",
  resp_2 = "Avoided runoff " ~ (m ^{3} /yr),
  resp_3 = "CO removal (g/yr)",
  resp_4 = "O"[3]~" removal (g/yr)",
  resp_5 = "NO"[2]~" removal (g/yr)"
)

然后你可以编写自己的标签函数来提取正确的值

ggplot(data_2, aes(x = species, y = result, fill = species))+
  geom_bar(stat = 'sum', show.legend = c(size=FALSE))+
  facet_wrap(~ response, labeller = function(x) {
    list(as.list(labels)[x$response])
  })

我们还使用了 show.legend = c(size=FALSE)

使用 as_labellerlabel_parsedRef

library(tidyr)
library(ggplot2)

data <- data.frame(species = rep(c("oak", "elm", "ash"), each = 5),
                   resp_1 = (runif(15, 1, 100)),
                   resp_2 = (runif(15, 1, 100)),
                   resp_3 = (runif(15, 1, 100)),
                   resp_4 = (runif(15, 1, 100)),
                   resp_5 = (runif(15, 1, 100)))
data_2 <- gather(data, response, result, resp_1:resp_5, factor_key = TRUE)

# setup the labels
reponse_names <- c(
  `resp_1` = "Oxygen~production~(kg*yr^{-1})",
  `resp_2` = "Avoided~runoff~(m^{3}*yr^{-1})",
  `resp_3` = "CO~removal~(g*yr^{-1})",
  `resp_4` = "O[3]~removal~(g*yr^{-1})",
  `resp_5` = "NO[2]~removal~(g*yr^{-1})"
)

# plot
ggplot(data_2, aes(x = species, y = result, fill = species))+
  geom_bar(stat = 'sum')+
  facet_wrap(
    ~ response,
    labeller = labeller(response  = as_labeller(reponse_names,  label_parsed))
  ) +
  guides(size = "none")

reprex package (v2.0.0)

于 2021-04-30 创建