Cep*_*irk 1 expression r ggplot2 plotmath
我想在我的 ggplot 上用两行、下标和上标以及对对象的引用进行注释。
我发现annotate()函数调用geom_text()which whenparse = TRUE可以使用来自plotmath.
如果这是我的标签:
q10 = 1.9
a = 3.9
b = -0.05
lab1 = substitute(atop(paste(Q[10], '=', q10), paste(M[O[2]], '=', a, e^(b*T))), list(q10 = q10 = 1.9, a = 3.9, b = -0.05))
Run Code Online (Sandbox Code Playgroud)
然后它将与基本图一起使用:
plot(1, 1, main = lab1)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用它时ggplot()会引发错误:
ggplot(diamonds, aes(carat, price, color = cut)) +
geom_point() +
annotate(geom = 'text', x = 4, y = 5000, label = lab1, parse = TRUE, color = 'blue')
Run Code Online (Sandbox Code Playgroud)
Error: Aesthetics must be either length 1 or the same as the data (1): label, colour
我在ggplot中发现了与多行注释相关的问题:R ggplot antop annotated with atop usingthree values and bgoup
与 ggplot 中的表达式相关:ggplot2 annotation with superscripts
但我无法弄清楚如何结合适当的答案来制作工作注释。那里的ggplot2大师有什么帮助吗?
要将 plotmath 与 ggplot 一起使用,请将其作为字符串传入—parse = TRUE指的是解析字符串。因此:
library(ggplot2)
ggplot(diamonds, aes(carat, price, color = cut)) +
geom_point() +
annotate(geom = 'text', x = 4, y = 5000,
label = "atop(Q[10] == 1.9,M[O[2]] == 3.9*e^(-0.05*T))",
parse = TRUE, color = 'blue')
Run Code Online (Sandbox Code Playgroud)

如果您需要替换到字符串中,请使用paste或glue::glue。
| 归档时间: |
|
| 查看次数: |
4310 次 |
| 最近记录: |