2016-03-03 57 views
1

我想手动将回归方程添加到包含正确数学符号(例如斜体,上标)的图中。 Annotate()看起来非常完美,但它似乎并不接受labels参数中的expression()函数。这是一个简单的例子。带有expression()的注释将不会显示。我究竟做错了什么?在R中添加方程ggplots

library(ggplot2) 
x <- 1:10 
y <- x^2 
df <- data.frame(x, y) 
sp <- ggplot(df, aes(x, y)) + geom_point() 
sp + annotate('text', label = expression('y = x^2'), x = 2.5, y = 50) + 
     annotate('text', label = 'y = x^2', x = 2.5, y = 75) 

回答

0

使用geom_text代替注释:

sp + geom_text(aes(2.5,75, label=(paste(expression("y = x "^-2*"")))),parse = TRUE)