简体   繁体   English

斜率,截距,ggplot2,R

[英]slope, intercept, ggplot2, R

I plotted a time series data on ggplot with Year on the x axis and rain on the y axis. 我在ggplot上绘制了一个时间序列数据,其中x轴为Year,y轴为rain。 I would like to overlay a trend line on this plot ( my equation for this trend line is rain = 2.6*Year + 23 ). 我想在该图上叠加一条趋势线(我的趋势线公式是rain = 2.6*Year + 23 )。 My slope was computed using the theil sen method How can I overlay this on my plot 我的斜率是使用theil sen方法计算的。如何将其叠加在我的绘图上

My code thus far is 到目前为止,我的代码是

ggplot(data = Datarain, aes(x = year, y = rain)) + 
  geom_smooth(color="red", formula = y ~ x) + 
  geom_smooth(method = "lm", se=FALSE color="blue", formula = y ~ x) +   
  geom_line() + scale_x_continuous("Year") 

I am not sure how to add my own equation on my plot or how to add a thiel sen line in ggplot 我不确定如何在我的情节上添加自己的方程式或如何在ggplot添加蒂尔森线

Any ideas would be grateful 任何想法将不胜感激

You can use geom_abline to specify your linear equation 您可以使用geom_abline指定线性方程式

ggplot(data = Datarain, aes(x = year, y = rain)) + 
  geom_smooth(color="red", formula = y ~ x) + 
  geom_smooth(method = "lm", se=FALSE color="blue", formula = y ~ x) +   
  geom_line() + scale_x_continuous("Year") + 
  geom_abline(intercept = 23, slope = 2.6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM