1
$\begingroup$

I have been trying to fit a scatter plot of a broken stick regression with confidence intervals. What I would like is to have two different regression lines and confidence intervals between the inflection point. I've already determined the inflection point using the SiZer package. I have tried to plot this two ways, by trying to graph 2 scatter plots (of the data groups based on the inflection point) with regressions and CIs on one graph, and also by indicating the inflection point (Tb>33.688) in the code, but have just received errors. I would like a graph similar to this, but with 2 different regression lines and CIs around the significant inflection point at 33.688

   p<-ggplot (thermalb, aes(x=thermalb$Ta, y=thermalb$Tb))+ 
   geom_point (color= "#69b3a2")+
   xlab("Air temperature (°C)")+ 
   ylab("Body temperature(°C)")+
   geom_smooth(method=lm , color="red", se=TRUE)
$\endgroup$

1 Answer 1

1
$\begingroup$

You need to categorize your observations according to the changepoint, and pass it as group in ggplot, for example:

library(SiZer)
library(ggplot2)
data(Arkansas)
dat = Arkansas
model <- piecewise.linear(dat$year,dat$sqrt.mayflies, CI=FALSE)
#group according to the changepoint
dat$grp = factor(ifelse(Arkansas$year > model$change.point,1,0))

ggplot(dat,aes(x=year,y=sqrt.mayflies,group=grp)) + 
geom_point() + geom_smooth(method="lm",formula=y~x,col="red")

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.