2

I am having issues limiting the regression lines in my "visreg" plots to the range of the data for each plot. They currently plot to the extent of the overall data frame and when I change the individual limits, it still plots to the overall data frame visually but not the correct data points. My sample codes are 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 an empty frame with 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 I plot it this way, the regression lines go to infinity especially for data in the left corner of my plot. I have also tried defining the individual data limits but it plots my points to the extent of the data frame which is wrong. How do I limit the regression lines to the data extent, please?

Joke O.
  • 515
  • 6
  • 29
  • Please make this reproducible or provide sample data. Also see [this link](http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/) for some examples of how to accomplish this with `ggplot2`. – JasonAizkalns Aug 19 '15 at 18:23
  • Hi @JasonAizkalns, I just added some sample data which should make the analysis reproducible, hopefully. – Joke O. Aug 20 '15 at 07:24

1 Answers1

1

If you want to extend the x (or y) range of the plot, but not of the fitted lines, you should first create the plot data using myPlotData <- visreg(myRegression, plot = FALSE). Then you can make the plot using plot.visreg(), including your custom x range: plot.visreg(myPlotData, xlim = 0.05, 0.31).

For your 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 enter image description here However, why make a separate regression analysis for each line? In this case (if you have no special reason to do separate analysis), I would join the two data sets together ans analyse them together, and then plotting with visreg() becomes much simpler (and the width of your 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", by = "Climate", overlay = TRUE, 
       main = "(a) Scatterplots without soil moisture (Dry season)",
       points=list(cex=1))

enter image description here Or, if you still needs your custom x limits, replace the last line with

visreg_data <- visreg(fit_combined, "SM", by = "Climate", plot = FALSE)
plot.visreg(visreg_data, overlay = TRUE, xlim = c(0.05, 0.31))

I do agree, however, that the default plot (or at least it should be an option) to limit the extent of each confidence band to the extent of the data for that level of your factor variable, such as ggplot2 does:

library(ggplot2)
ggplot(data_combined, aes(SM, CO, color=Climate)) + stat_smooth(method = "lm") + geom_point()

enter image description here visreg has more options for models than ggplot (I think), so it would have been nice to have this option.

Dag Hjermann
  • 1,960
  • 14
  • 18