R ggplot注释中的顺序上标
我希望我能得到一些帮助,在ggplot图上放置一个文本标签(使用R ggplot注释中的顺序上标,r,ggplot2,R,Ggplot2,我希望我能得到一些帮助,在ggplot图上放置一个文本标签(使用annotate),其中文本包含一个序号后缀作为上标(或任何合适的术语)和一个计算值。尽管我读了一些关于SO和SE的问题,但我还是很难让它起作用 这里是一个最小的例子 # my two attempts at creating the label my.var = 10 / 3 # just a toy example my.label.expr = paste(expression("20^th*"), " decil
annotate
),其中文本包含一个序号后缀作为上标(或任何合适的术语)和一个计算值。尽管我读了一些关于SO和SE的问题,但我还是很难让它起作用
这里是一个最小的例子
# my two attempts at creating the label
my.var = 10 / 3 # just a toy example
my.label.expr = paste(expression("20^th*"), " decile ", round(my.var, 4), sep = "")
my.label.bquote = bquote('Lift (' ~ 20^th* ~ "decile) : " ~ .
(round(my.var, 4), sep = ""))
test = data.frame(x = 1:10, y = 1:10)
ggplot(aes(x = x , y= y ), data = test) + geom_point() +
annotate("text", label = my.label.bquote,parse = TRUE, x = 5, y = 5,
colour = "red", size = 7)
使用label=my.label.expr
会产生错误:
# Error in parse(text = lab) : <text>:1:15: unexpected numeric constant
# 1: 20^th* decile 3.3333
#分析错误(text=lab)::1:15:意外的数字常量
#1:20十分位数3.3333
使用label=my.label.bquote
会导致解释器出现提示,提示中带有
我还尝试了使用parse=FALSE
,my.label.expr
打印,但只是按字面显示文本,没有上标。对于bquote解决方案,使用parse=FALSE
,它根本不打印任何内容
我觉得我对这些选项的语法感到困惑。是否可以显示纯字母上标?任何帮助都将不胜感激 这个怎么样
my.label.expr = paste("20^{th}*' decile '*", round(my.var, 4))
ggplot(aes(x = x , y= y ), data = test) + geom_point() +
annotate("text", label = my.label.expr, parse=TRUE,
x = 5, y = 5, colour = "red", size = 7)
在这里,我们使用适当的?plotmath
语法(将指数分组在括号中)将值构建为字符串,然后设置parse=TRUE
,以便对其进行评估