我已经搜索了这个主题并尝试了很多不同的东西,但仍然无法以正确的方式对我的标签进行注释。
在我的标签上,我想显示" R ^ 2 = 0.81,p值= 0.04,n = 50"。到目前为止,我已经尝试过:
df <- data.frame(x = rnorm(10), y = rnorm(10))
stat <- paste("R^2= 0.81", "p-value= 0.04", "n= 50")
ggplot(df, aes(x = x, y = y)) +
geom_point() +
annotate("text", x = mean(df$x), y = mean(df$y), parse = T, label=stat)
这给了我以下错误:Error in parse(text = as.character(lab)) : <text>:1:13: unexpected symbol
1: R^2= 0.81 p
&#39;
我尝试使用expression
代替paste
,例如stat <- expression("R^2= 0.81", "p-value= 0.04", "n= 50")
,但这会产生不同的错误(Aesthetics must be either length 1 or the same as the data (1): label
)。
我也注意到有些人使用撇号来解决逗号问题,所以我试过stat <- paste(
&#34; R ^ 2 = 0.81&#34; {{ 1}}&#34; p值= 0.04&#34; ,
&#34; n = 50&#34; ,
这也会产生错误。如何才能正确注释我的标签?
答案 0 :(得分:1)
df <- data.frame(x = rnorm(10), y = rnorm(10))
stat <- paste("R^2= 0.81", "p-value= 0.04", "n= 50")
ggplot(df, aes(x = x, y = y)) + geom_point() +
annotate("text", x = mean(df$x), y = mean(df$y), label=stat)
答案 1 :(得分:0)
我刚注意到,如果我拿出parse命令,我就不会正确描述R2中的下标。为了避免这种情况,我最终使用了以下命令:
df <- data.frame(x = rnorm(10), y = rnorm(10))
ggplot(df, aes(x = x, y = y)) + geom_point() +
annotate("text", x = mean(df$x), y = mean(df$y), parse = TRUE, label = as.character(expression(R^{2}*" = 0.81; "*"p = 0.04; "*"n = 50")))