我尝试使用上标生成标签,因此使用parse。我似乎不能使用空格,这有效:
GTVol <- 1:10
measuredVol <- 1:10
dat <- data.frame(GTVol, measuredVol)
ggplot(dat, aes(GTVol, measuredVol)) +
geom_point() +
geom_abline(slope=1) +
xlab(parse(text='HarvestedVolume(m^3)')) +
ylab("QSM Volume (m^3)")
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
GTVol <- 1:10
measuredVol <- 1:10
dat <- data.frame(GTVol, measuredVol)
ggplot(dat, aes(GTVol, measuredVol)) +
geom_point() +
geom_abline(slope=1) +
xlab(parse(text='Harvested Volume (m^3)')) +
ylab("QSM Volume (m^3)")
Run Code Online (Sandbox Code Playgroud)
给我以下错误:
Error in parse(text = "Harvested Volume (m^3)") :
<text>:1:11: unexpected symbol
1: Harvested Volume
Run Code Online (Sandbox Code Playgroud)
通过添加更改空格字符 ~
library(ggplot2)
ggplot(dat, aes(GTVol, measuredVol)) +
geom_point() +
geom_abline(slope=1) +
xlab(parse(text='Harvested~Volume~(m^3)')) +
ylab("QSM Volume (m^3)")
Run Code Online (Sandbox Code Playgroud)
-输出
或者gsub用于动态替换
...
xlab(parse(text=gsub("\\s+", "~", 'Harvested Volume (m^3)'))) +
...
Run Code Online (Sandbox Code Playgroud)
plotmath不需要parse,正确的方法是expression(下面)或bquote。
library(ggplot2)
ggplot(dat, aes(GTVol, measuredVol)) +
geom_point() +
geom_abline(slope = 1) +
xlab(expression(Harvested ~ Volume ~ (m^3))) +
ylab(expression(QSM ~ Volume ~ (m^3)))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31 次 |
| 最近记录: |