pbreheny / visreg

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

color points by group #56

Closed DemaLM closed 6 years ago

DemaLM commented 6 years ago

Hi,

I'd like to have specific colors for groups in my points in the visreg output.

So for instance, taking the mixed model example:

library(lme4)
data(Milk, package="nlme")
fit <- lmer(protein ~ Diet + Time + (Time|Cow), Milk)
visreg(fit, "TIme", ylab="Protein", gg=TRUE)
 theme_bw()+
      geom_point(size=3, alpha=0.1,group="Diet", shape=16)

image

In this case, I don't know why are there dots in the middle of the points. Just can't remove them. Second, I'd color the points with specific colors for Diet using ggplot as above .

Many thanks!

pbreheny commented 6 years ago
  1. The points are added by the call to visreg. You're adding the gray circles with geom_point. If you don't want visreg to plot the partial residuals, the option is partial=FALSE. So, for example:
visreg(fit, "Time", ylab="Protein", gg=TRUE, partial=FALSE, rug=FALSE) +
  theme_bw() +
  geom_point(size=3, alpha=0.1, shape=16)
  1. Colors can be added with the by and overlay arguments:
visreg(fit, "Time", ylab="Protein", gg=TRUE, by="Diet", overlay=TRUE)

a

  1. If you want to turn off visreg's plotting of points and plot them yourself, you can, but (a) this would need to be done through the aes interface in ggplot2 and (b) you still need to set up a by variable, otherwise it won't be included in the data frame that ggplot2 is based on:
visreg(fit, "Time", ylab="Protein", gg=TRUE, by="Diet", overlay=TRUE, partial=FALSE, rug=FALSE) +
  theme_bw() +
  geom_point(aes(color=Diet), size=3, alpha=0.1, shape=16)

b

  1. As of commit 4d74360, you can also just pass the graphical parameters to visreg via points.par:
visreg(fit, "Time", ylab="Protein", gg=TRUE, by="Diet", overlay=TRUE,
       points.par=list(size=3, alpha=0.1), jitter=TRUE)

c

DemaLM commented 6 years ago

Thank you very much for your help! One last thing: I understood your last two examples to use Diet to color the points, however, is it possible to use a variable which is not specified in the model to color the points? For example, if there were a "Farm.ID" variable in the Milk dataset and I'd like to use it as a grouping variable to color the points according to this variable.

Thanks

pbreheny commented 6 years ago

You can do it, although you would have to manually add the variable to the visreg object; otherwise, if the variable isn't in the model frame, visreg doesn't have access to it. For example:

fit <- lmer(protein ~ Time + (Time|Cow), Milk)
v <- visreg(fit, "Time", ylab="Protein", jitter=TRUE, plot=FALSE)
v$res$Diet <- Milk$Diet
plot(v, gg=TRUE, partial=FALSE, rug=FALSE) + theme_bw() +
  geom_point(data=v$res, aes(x=Time, y=visregRes, color=Diet), size=3, alpha=0.1, shape=16)

d

DemaLM commented 6 years ago

That's exactly what I wanted! Thank you very much!!!

pbreheny commented 6 years ago

Happy to help!