如何在 ggplot facet_wrap 文本中添加数学符号?
How to add mathematical symbols in ggplot facet_wrap text?
This question 解释了如何在图中添加数学符号。但是,当文本应该存储在数据框本身中时,这不起作用。 facet_wrap 子图的小方框中出现的文本就是这种情况。
这是一个可重现的例子。比方说,我在 T 和 m³ 中有这些数据,我想绘制这样的图。
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))
正确显示单位。我怎样才能在侧面显示类似的立方米?
将 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)
This question 解释了如何在图中添加数学符号。但是,当文本应该存储在数据框本身中时,这不起作用。 facet_wrap 子图的小方框中出现的文本就是这种情况。
这是一个可重现的例子。比方说,我在 T 和 m³ 中有这些数据,我想绘制这样的图。
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))
正确显示单位。我怎样才能在侧面显示类似的立方米?
将 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)