If the two ranges of x that you want to are independent and you want to generate 4 separate regression lines (as is my understanding of your question) then you can specify the data to use in 2 calls to geom_smooth()
.
Here, head()
and tail()
are indicating which values of x you want to regress on, assuming the points are ordered in df. If they are not ordered, you will need to do that first (e.g. using a call to arrange()
by values on the x-axis).
library(tidyverse)
# some test data with 3 variables: a random response (y), a sequence (x), and a factor (z).
df<-tibble(x = seq(0.5, 25, 0.5),
y = rnorm(50),
z = sample(x = c("A", "B"), replace = T, size = 50))
# a plot with each factor of z coloured and 2 regression lines for each factor
ggplot(df, aes(x, y, colour = z))+
geom_point()+
geom_smooth(data = ~head(df, 30), method = "lm", se = F)+
geom_smooth(data = ~tail(df ,20), method = "lm", se = F)+
theme_minimal()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…