I would like to make an annotation on my ggplot with two lines, sub- and superscripts, and references to objects.
我想在我的ggplot上做一个注释,包括两行,子和上标,以及对象的引用。
I've figured out that the annotate()
function calls geom_text()
which when parse = TRUE
can use expressions from plotmath
.
我已经发现annotate()函数调用geom_text(),当parse = TRUE时,它可以使用plotmath中的表达式。
If this is my label:
如果这是我的标签:
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))
Then it will work with base plot:
然后它将与基础图一起使用:
plot(1, 1, main = lab1)
But when I try to use it with ggplot()
it throws an error:
但是当我尝试将它与ggplot()一起使用时,它会抛出一个错误:
ggplot(diamonds, aes(carat, price, color = cut)) +
geom_point() +
annotate(geom = 'text', x = 4, y = 5000, label = lab1, parse = TRUE, color = 'blue')
Error: Aesthetics must be either length 1 or the same as the data (1): label, colour
错误:美学必须是长度1或与数据(1)相同:标签,颜色
I found questions related to multi-line annotations in ggplot: R ggplot annotated with atop using three values and bgoup
我在ggplot中找到了与多行注释相关的问题:使用三个值和bgoup用atop注释的R ggplot
and related to expressions in ggplot: ggplot2 annotation with superscripts
并与ggplot中的表达式相关:带上标的ggplot2注释
But I can't figure out how to combine the appropriate answers to make a working annotation. Any help from the ggplot2
gurus out there?
但我无法弄清楚如何结合适当的答案来制作工作注释。那里有ggplot2专家的帮助吗?
1 个解决方案
#1
2
To use plotmath with ggplot, you pass it in as a string—parse = TRUE
refers to parsing the string. Thus:
要将plotmath与ggplot一起使用,请将其作为string-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')
If you need to substitute into the string, either use paste
or glue::glue
.
如果你需要替换成字符串,可以使用paste或glue :: glue。
#1
2
To use plotmath with ggplot, you pass it in as a string—parse = TRUE
refers to parsing the string. Thus:
要将plotmath与ggplot一起使用,请将其作为string-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')
If you need to substitute into the string, either use paste
or glue::glue
.
如果你需要替换成字符串,可以使用paste或glue :: glue。