nee
nee

Reputation: 125

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

Upvotes: 0

Views: 4909

Answers (1)

ilir
ilir

Reputation: 3224

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)

Upvotes: 4

Related Questions