如何ggplot facet_wrap文本添加数学符号?(How to add mathematic

2019-09-29 17:59发布

这个问题介绍了如何添加一个情节里面的数学符号。 但是,当是应该的文本存储在数据帧本身这不起作用。 这是出现在facet_wrap子地块小盒子文本的情况。

这里是一个重复的例子。 比方说,例如,我在T和在立方米,而且我想提请这样一个情节有这样的数据。

library(ggplot2)
dtf <- data.frame(year = rep(1961:2010,2),
                  consumption = cumsum(rnorm(100)),
                  item = rep(c("Tea bags","Coffee beans"),each=50),
                  unit = rep(c("T","m^3"),each=50))
ggplot(data=dtf)+
    geom_line(aes(x=year, y=consumption),size=1) +
    ylab(expression(paste("Consumption in T or ",m^3))) +
    scale_x_continuous(breaks = seq(1960,2010,10)) +
    theme_bw() + facet_wrap(item~unit, scales = "free_y")

ylab(expression(m^3))正确显示单元。 我怎么能显示在面类似立方米?

Answer 1:

添加labeller = label_parsed到您facet_wrap功能,格式化单元作为m^3 ,并在标签与替换空格~

library(ggplot2)
dtf <- data.frame(year = rep(1961:2010,2),
                  consumption = cumsum(rnorm(100)),
                  item = rep(c("Tea~bags","Coffee~beans"),each=50),
                  unit = rep(c("T","m^3"),each=50))
ggplot(data=dtf)+
  geom_line(aes(x=year, y=consumption),size=1) +
  ylab(expression(paste("Consumption in T or ",m^3))) +
  scale_x_continuous(breaks = seq(1960,2010,10)) +
  theme_bw() + facet_wrap(item~unit, scales = "free_y",labeller = label_parsed)



文章来源: How to add mathematical symbols in ggplot facet_wrap text?