facet_wrap()中的数学符号标签不起作用

问题描述 投票:1回答:1

我的数据:

plot_df <- 
structure(list(model = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Full", 
"Point Differential"), class = "factor"), measure = structure(c(1L, 
1L, 2L, 2L, 3L, 3L), .Label = c("AIC", "R^2", "Number of Predictors"
), class = "factor"), value = c(266.666394197942, 303.321585856894, 
0.851698145309359, 0.713883491646847, 10, 1)), class = "data.frame", row.names = c(NA, 
-6L))

我使用数据制作了以下图:

ggplot(plot_df, aes(y=value, x=model, fill=model)) +
  geom_bar(position="dodge", stat="identity") +
  facet_wrap(~measure, scales='free') +
  labs(title='Model Comparison', x='', y='Value', fill='Model') +
  geom_text(aes(label=round(value,2)),vjust=1.25) +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

enter image description here

第二个facet_wrap()窗格的标题为R^2,但它不是数学符号。我尝试过使用labeller=label_parsed(),但它不起作用。此外,我尝试使用latex2exp包,但这也不起作用。有谁知道如何解决这一问题?

r ggplot2 facet-wrap
1个回答
3
投票

这是labeller=label_parsed,而不是labeller=label_parsed()

但另外,你必须用~替换白色空格:

levels(plot_df$measure)[3] <- "Number~of~Predictors"
ggplot(plot_df, aes(y=value, x=model, fill=model)) +
  geom_bar(position="dodge", stat="identity") +
  facet_wrap(~measure, scales='free', labeller = label_parsed) +
  ......

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.