pbreheny / visreg

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

gg=TRUE does not work without variable specification #46

Closed jebyrnes closed 5 years ago

jebyrnes commented 6 years ago

I'm a huge fan of how visreg works with ggplot. So, the default example of


library(visreg)
library(ggplot2)

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

Is awesome.

However,

visreg(fit, gg=TRUE)

produces nothing. I can understand that perhaps having different variables requires some more variable transformation. But, it's still frustrating to have nothing happens - as a user, it makes me think I did something wrong. Might I suggest either

  1. An error message (HA!) or
  2. I think you could accomplish what is done with base by, once you have the data frame for plotting in hand, using tidyr::gather() to make a long data frame, and then plotting using ggplot2. Given that gg=TRUE works for multiple continuous variables, I'm guessing you have something similar built there?
pbreheny commented 6 years ago

This is a good point; on a deeper level, the issue is that various visreg commands (including but not limited to visreg(fit, gg=TRUE)) produce an object of type visregList (a list of visreg objects). But plot.visregList does not (yet) work with ggplot2.

Thank you for bringing this to my attention; I'm kind of busy the next few weeks, but I hope to fix this soon.

jebyrnes commented 6 years ago

No worries! I hadn't even realized that you could use this with ggplot2 until just a few days ago. I was absolutely delighted, as I'm teaching an intro data science class that is all ggplot2 all the time, and had been sad I couldn't use visreg... until I discovered a few posts saying I could! I tried to poke into the codebase myself, but, don't have the time to get a deep understanding and make the fixes myself.

Sidenote: I wonder if there's an argument where instead of kicking back a visregList object, visreg could just return a tidy long data frame? Might be a way to allow for longevity if some hot new graphing package came up to avoid similar questions. Heck, like using plotly or somesuch? Just a thought that popped into my head.

Thank you for a wonderful package - it's my goto for teaching multiple linear regression!!! Students love it!

chri4354 commented 5 years ago

I was wondering if this problem had been fixed, because I am having the same issue.

pbreheny commented 5 years ago

OK, just committed a change that fixes this issue. To clarify (continuing the above example), this has always worked fine:

# CRAN release: install.packages('visreg')
v <- visreg(fit, plot=FALSE)
plot(v, gg=TRUE)

However, with the new release, this also works:

# Current GitHub version: devtools::install_github("pbreheny/visreg")
visreg(fit, gg=TRUE)

Thanks to both of you for bringing this to my attention -- sorry for the delay in fixing this. Please let me know if you're still having issues or notice anything else!

pbreheny commented 5 years ago

By the way, just to respond to @jebyrnes above, visreg can't exactly return a tidy data frame because it needs to return two data frames, one for the fit/confidence intervals and a different one for the residuals. These are returned:

v <- visreg(fit, 'x')

The two data frames are v$fit and v$res. You can take those data frames and edit them, manipulate them in various ways, assign them to various aesthetics in the plot (if you know what you're doing). I've been meaning to investigate how easy it is to pipe visreg output to plotly, but haven't gotten around to it. I think it should be easy, since plotly can handle gg objects, but haven't actually tried it yet.