limiting regression lines to data extent in a multi plot using visreg R -
i having issues limiting regression lines in "visreg" plots range of data each plot. plot extent of overall data frame , when change individual limits, still plots overall data frame visually not correct data points. sample codes below:
codrysub humid wet.sub.himid dry.sub.himid semi.arid arid hyper.arid nov 2004 2.20 2.22 2.16 2.03 1.79 1.68 dec 2004 2.75 2.72 2.52 2.32 2.08 1.93 jan 2005 2.98 2.92 2.68 2.43 2.18 2.03 feb 2005 2.81 2.80 2.62 2.37 2.14 2.04 mar 2005 2.43 2.44 2.33 2.16 1.99 1.96 apr 2005 2.30 2.25 2.08 2.00 1.90 1.91 smdrysub humid wet.sub.himid dry.sub.himid semi.arid arid hyper.arid nov 2004 0.26 0.21 0.15 0.08 0.05 0.07 dec 2004 0.22 0.16 0.10 0.07 0.06 0.08 jan 2005 0.19 0.12 0.08 0.07 0.07 0.09 feb 2005 0.18 0.11 0.08 0.07 0.08 0.10 mar 2005 0.18 0.13 0.09 0.07 0.07 0.10 apr 2005 0.19 0.15 0.10 0.07 0.06 0.08 library(visreg) humida<-cbind(codrysub[,1],smdrysub[,1]) humid2a<-as.data.frame(humida) colnames(humid2a)<-c("co","sm") fit1a<-lm(co~sm,data=humid2a) wsha<-cbind(codrysub[,2],smdrysub[,2]) wsh2a<-as.data.frame(wsha) colnames(wsh2a)<-c("co","sm") fit2a<-lm(co~sm,data=wsh2a) ##to plot ## overall plot of empty frame dataframe labels plot(0,xlim=c(0.05,0.31),ylim=c(1.5,3.4),cex.main=1, main= "(a) scatterplots without soil moisture (dry season)",xlab=na,ylab=na) par(new=t) visreg(fit1a,xlab="sm",ylab="co",points=list(cex=1.2, pch=20,col="deepskyblue4") ,alpha=0.8,line=list(col="deepskyblue4",lwd=2),fill=list(col=adjustcolor("deepskyblue4", alpha.f = 0.09)), xlim=c(0.05,0.31),ylim=c(1.5,3.4),xaxt="n",yaxt="n") par(new=t) visreg(fit2a,xlab=na,ylab=na,points=list(cex=1.2, pch=20,col="dodgerblue1") ,line=list(col="dodgerblue1",lwd=2),fill=list(col=adjustcolor("dodgerblue1", alpha.f = 0.09)) ,xlim=c(0.05,0.31),ylim=c(1.5,3.4),alpha=0.8,xaxt="n",yaxt="n")
when plot way, regression lines go infinity data in left corner of plot. have tried defining individual data limits plots points extent of data frame wrong. how limit regression lines data extent, please?
if want extend x (or y) range of plot, not of fitted lines, should first create plot data using myplotdata <- visreg(myregression, plot = false)
. can make plot using plot.visreg()
, including custom x range: plot.visreg(myplotdata, xlim = 0.05, 0.31)
.
for example:
xlim <- c(0.05,0.31) ylim <- c(1.5,3.4) plot(0,xlim=xlim, ylim=ylim, cex.main=1, main="(a) scatterplots without soil moisture (dry season)", xlab=na, ylab=na) par(new=t) visreg1a <- visreg(fit1a, plot = false) plot.visreg(visreg1a, xlab="sm", ylab="co", points=list(cex=1.2, pch=20,col="deepskyblue4"), alpha=0.8, line=list(col="deepskyblue4", lwd=2), fill=list(col=adjustcolor("deepskyblue4", alpha.f = 0.09)), xlim=xlim, ylim=ylim, xaxt="n", yaxt="n") par(new=t) visreg2a <- visreg(fit2a, plot = false) plot.visreg(visreg2a, xlab=na, ylab=na, points=list(cex=1.2, pch=20,col="dodgerblue1"), line=list(col="dodgerblue1",lwd=2), fill=list(col=adjustcolor("dodgerblue1", alpha.f = 0.09)), xlim=xlim, ylim=ylim, alpha=0.8,xaxt="n",yaxt="n")
which results in however, why make separate regression analysis each line? in case (if have no special reason separate analysis), join 2 data sets ans analyse them together, , plotting visreg()
becomes much simpler (and width of confidence intervals decreases!):
data_combined <- rbind(data.frame(climate = "humid", humid2a), data.frame(climate = "wet-subhumid", wsh2a)) fit_combined <- lm(co ~ sm*climate, data = data_combined) visreg(fit_combined, "sm", = "climate", overlay = true, main = "(a) scatterplots without soil moisture (dry season)", points=list(cex=1))
or, if still needs custom x limits, replace last line with
visreg_data <- visreg(fit_combined, "sm", = "climate", plot = false) plot.visreg(visreg_data, overlay = true, xlim = c(0.05, 0.31))
i agree, however, default plot (or @ least should option) limit extent of each confidence band extent of data level of factor variable, such ggplot2
does:
library(ggplot2) ggplot(data_combined, aes(sm, co, color=climate)) + stat_smooth(method = "lm") + geom_point()
visreg
has more options models ggplot (i think), have been nice have option.
Comments
Post a Comment