您如何将趋势线添加到GGPLOT2中的一部分数据中?
我有数据和这样的图,
x = c(1,2,3,4,5,6,7,8,9,10,11,12)
y1 = x^2-5
y2 = -x^2+1
data <- data.frame(x,y1,y2)
data1 = data.frame(pivot_longer(data,2:3))
ggplot(data1, aes(x, y = value, color = name))+
geom_point()+
geom_smooth(method = 'lm',se = FALSE)
是否有一种方法可以使趋势线仅适用于x的值大于x一个数字,例如3?
I have data and a plot like this,
x = c(1,2,3,4,5,6,7,8,9,10,11,12)
y1 = x^2-5
y2 = -x^2+1
data <- data.frame(x,y1,y2)
data1 = data.frame(pivot_longer(data,2:3))
ggplot(data1, aes(x, y = value, color = name))+
geom_point()+
geom_smooth(method = 'lm',se = FALSE)
Is there a way to have the trendline only applying to values for x greater than a certain number, like 3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以做到这一点:
You can do this:
您只能将当前
AES
仅应用于geom_point
,并创建一个新列(即我的代码中的x2
)以映射到geom_smooth
。由 reprex软件包(v2.0.1)
You can apply the current
aes
togeom_point
only, and create a new column (i.e.x2
in my code) for mapping togeom_smooth
.Created on 2022-05-07 by the reprex package (v2.0.1)
与上述两者相似,仅使用
subset
:Similar to both above just using
subset
: