pbreheny / visreg

Visualization of regression functions
http://pbreheny.github.io/visreg/
61 stars 18 forks source link

Changing y limits on y-axis when gg=T #38

Closed makresh closed 6 years ago

makresh commented 6 years ago

Hi, I'm having some trouble changing the y limits on the y-axis, when using gg=T within the visreg function, and I can't seem to find anything online about this.

Here's the data I'm using:

Testdata.txt

Here's a model I'm running:

summary(m1<-lm( TPAPercentMortalityIndexLT5 ~ per_ADS_2k_total * TreatmentType , data=xx))

Here's the code for the plot: ' library(boot) #To backtransform the data (which is on a logit scale) so that it's on a regular scale theme_set(theme_bw()) visreg(m1, xvar= 'per_ADS_2k_total', by='TreatmentType', gg=T, line.par = list(col = 'black'), trans=inv.logit ,ylab='TPA Mortality Index, <5 in dbh', xlab='ADS 2-km Radius (%)' ,partial=T, points=list(size=2.5, color='black'), ylim=c(0,0.75)) '

I would like to create a plot that has the y limits on the y-axis as 0-0.75. However, when gg=T, I can't seem to override the y-limits.

Here's the plot from my data: testplot.pdf

I also tried this code, which also did not work:

`(v<-visreg(m1, xvar= 'per_ADS_2k_total', by='TreatmentType', trans=inv.logit, line.par = list(col = 'black'), ylab='TPA Mortality Index, <5 in dbh', xlab='ADS 2-km Radius (%)' ,partial=T, points=list(size=2.5, color='black')))

plot(v, gg=T, ylim=c(0,.7))`

If I take out the gg=T argument, then I'm able to override the y limits, however, I'm now not plotting in the ggplot2 framework:

visreg(m1, xvar= 'per_ADS_2k_total', by='TreatmentType', line.par = list(col = 'black'), trans=inv.logit ,ylab='TPA Mortality Index, <5 in dbh', xlab='ADS 2-km Radius (%)' ,partial=T, points=list(size=2.5, color='black'), ylim=c(0,0.75))

testplot2.pdf

Please let me know if I'm doing something wrong. Thanks!

pbreheny commented 6 years ago

If you're using ggplot2, you need to specify options according to its framework. In particular, for setting limits,

fit <- lm(Ozone ~ Solar.R + Wind + Temp, data=airquality)
visreg(fit, "Wind", gg=TRUE) + ylim(-10, 100)

Note, however, that ggplot2's `ylim replaces plot data outside the limits with NA; not a big deal for residuals, but causes problems with the confidence band. So you might need the more flexible

visreg(fit, "Wind", gg=TRUE) + coord_cartesian(ylim=c(25, 80)
makresh commented 6 years ago

Thanks! That worked.