rggplot2linear-regressionscattertrendline

How do you add trendline to part of data in ggplot2?


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)

Output

Is there a way to have the trendline only applying to values for x greater than a certain number, like 3?


Solution

  • Similar to both above just using subset:

    ggplot(data1, aes(x, y = value, color = name))+ 
      geom_point()+
      geom_smooth(data=subset(data1, x > 3), method = 'lm',se = FALSE)
    

    enter image description here