简体   繁体   中英

slope, intercept, ggplot2, R

I plotted a time series data on ggplot with Year on the x axis and rain on the y axis. I would like to overlay a trend line on this plot ( my equation for this trend line is rain = 2.6*Year + 23 ). My slope was computed using the theil sen method How can I overlay this on my plot

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

Any ideas would be grateful

You can use geom_abline to specify your linear equation

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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