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

时间:2019-03-04 18:09:12

标签: r ggplot2 facet-wrap

我的数据:

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包,但这也不起作用。有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分: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