本文介绍了在R中的ggplots中添加方程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想手动将回归方程式添加到包含适当数学符号(例如,斜体,上标)的绘图中.Annotate()似乎很完美,但是似乎不接受labels参数中的expression()函数.这是一个简单的例子.带有expression()的注释将不会显示.我在做什么错了?
I'd like to manually add a regression equation to a plot that includes the proper mathematical notation (e.g., italics, superscripts). Annotate() seems perfect for this, but it doesn't seem to accept the expression() function in the labels argument. Here is a simple example. The annotation with expression() in it won't display. What am I doing wrong?
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)
推荐答案
使用geom_text代替注释:
Use geom_text instead of annotate:
sp + geom_text(aes(2.5,75, label=(paste(expression("y = x "^-2*"")))),parse = TRUE)
这篇关于在R中的ggplots中添加方程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!