### Passiflora flower size r code PV<-read.csv("Passiflora_visits.csv",header=TRUE) #head(PV) mean(PV$Total_visitors); var(PV$Total_visitors) modellm<-lm(PV$Total_visitors ~ PV$Flower_diam_mm) summary(modellm) modelP<-glm(PV$Total_visitors ~ PV$Flower_diam_mm,family="poisson") modelQP<-glm(PV$Total_visitors ~ PV$Flower_diam_mm,family="quasipoisson") summary(modelQP) plot(PV,pch=16,col="red") abline(modelQP,col="red",lwd=2) abline(modelP,col="blue",lwd=2,lty=3) abline(modellm,col="black",lwd=2) ## for modelQP intercept<-modelQP$coeff[1] slope<-modelQP$coeff[2] mid_x_value<-range(PV$Flower_diam_mm)[1]+0.5*diff(range(PV$Flower_diam_mm)) # get the middle of the x range y<-slope*mid_x_value+intercept + 0.35 # 0.35 is a small offset to position text above line text(mid_x_value,y,"Poisson and quasipoisson fit", srt=3,col="blue") # 3 degrees is a first guestimate for the angle ###### Calculating precise angle in degrees taking into account the x and y limits of ###### the plot and the dimensions of the plot window ###### complicated but necessary as the printing text at an angle argument srt= ###### requires degrees plot(PV,pch=16,col="red") abline(modelQP,col="red",lwd=2) abline(modelP,col="blue",lwd=2,lty=3) abline(modellm,col="black",lwd=2) plotwindow_adjust<-par("pin")[2]/par("pin")[1] mylims <- par("usr") mid_x_value<-range(PV$Flower_diam_mm)[1]+0.5*diff(range(PV$Flower_diam_mm)) #### Poisson and quasipoisson model intercept<-modelQP$coeff[1] slope<-modelQP$coeff[2] ### y=slope*x+ intercept y<-slope*mid_x_value+intercept + 0.35 # 0.35 is a small offset to position text above line left<-slope*mylims[1]+intercept right<-slope*mylims[2]+intercept height<-right-left spread<-mylims[4]-mylims[3] tangent<-height/spread plotwindow_adjust<-par("pin")[2]/par("pin")[1] rotation_in_degrees<-plotwindow_adjust*atan(tangent) * 180 / pi text(mid_x_value,y,"Poisson and quasipoisson fit", srt=rotation_in_degrees,col="blue") ###Linear model intercept<-modellm$coeff[1] slope<-modellm$coeff[2] y<-slope*mid_x_value+intercept + 0.35 left<-slope*mylims[1]+intercept right<-slope*mylims[2]+intercept height<-right-left spread<-mylims[4]-mylims[3] tangent<-height/spread rotation_in_degrees<-plotwindow_adjust*atan(tangent) * 180 / pi text(mid_x_value,y,"Linear model fit", srt=rotation_in_degrees,col="blue")