pbreheny / visreg

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

Specifying point shape and color #79

Closed zdiazmar closed 4 years ago

zdiazmar commented 4 years ago

I've really enjoyed using visreg - thanks for the great package!

I'm having trouble specifying the color and shape of points when using ggplot2 with visreg. I've recreated what I am trying to do with the iris data.

##read in data
data(iris)
head(iris)

##set the colors scheme to be used 
cb <- c("seagreen", "goldenrod1", "deepskyblue3")

#log transform y variable
iris$LogPetal.Width <- log(iris$Petal.Width)

#run model
mod <-lmer(LogPetal.Width ~ Species + Petal.Length + (1|Sepal.Width), data=iris)

#make first figure (should work fine)
p1 <- visreg(mod, xvar = "Petal.Length", by = "Species", trans = exp,
             overlay = T, partial =F, band = F, rug=F, gg=T) +
  geom_point(aes(color=Species, shape=Species)) +
  scale_colour_manual(values = cb) + 
  labs(color = "Species", fill = "Species", shape = "Species", title = "A.") +
  theme_bw()
p1

#make second figure similar to the first but with species on the x axis (does not work)
p2 <- visreg(mod, xvar = "Species", partial = F, band= F, rug = F, gg=T, trans = exp) +
    geom_point(aes(color=Species, shape=Species)) +
    scale_colour_manual(values = cb) + 
    labs(color = "Species", fill = "Species", shape = "Species",title = "A.", x = "Species", y = "Petal Width") + 
    theme_bw()
p2 

When I run the code above I get the following error: Error in FUN(X[[i]], ...) : object 'Species' not found

I apologize in advance if someone else has asked a similar question, but I haven't been able to find information about this error online. Thanks for any help!

pbreheny commented 4 years ago

Have you tried looking here? http://pbreheny.github.io/visreg/gg.html

Big picture is that specifying color using +scale_color_manual() or something like that is a little problematic because there are multiple layers with different colors, so the best way to handle that is to pass color as an argument to visreg and let visreg pass it along to the relevant layer when the layer is created.

Does this help?

zdiazmar commented 4 years ago

Thanks for the quick response! I have been working on incorporating your suggestion, but I still can't seem to get it to work.

I have been running the same model and trying the following code based on your comment:

##prepare data
data(iris)
iris$LogPetal.Width <- log(iris$Petal.Width)

##model
mod <-lmer(LogPetal.Width ~ Species + Petal.Length + (1|Sepal.Width), data=iris)

##Figure specifying different color and shapes for different species 
visreg(mod, xvar="Species", trans= exp, rug = F, gg=T,
       line=list(col=29:31),
       points=list(pch=6:8))

But I get the error: Error: Aesthetics must be either length 1 or the same as the data (2): colour

I've also tried specifying the list and points in a variety of different ways, but without success. Do you have any idea of what the issue and potential solution might be?

Thanks again for your help and time!

zdiazmar commented 4 years ago

Hi again,

It seems that while I have solved one problem, I have run into another! The below code plots a partial residual plot for a mixed model with the log transformed variable petal.length with specific colors for each species.

##read in data
data(iris)
head(iris)

##set the colors scheme to be used 
cb <- c("seagreen", "goldenrod1", "deepskyblue3")

#log transform y variable
iris$LogPetal.Width <- log(iris$Petal.Width)

#run model
mod <-lmer(LogPetal.Width ~ Species + Petal.Length + (1|Sepal.Width), data=iris)

#make plot
p10 <- visreg(mod, xvar = "Species", by = "Species",
             overlay = T, gg=T, points = T, line=list(col="black")) +
  scale_colour_manual(values = cb) +
  labs(color = "Species", fill = "Species") +
  theme_bw() +
  theme(legend.position='none',
        plot.title = element_text(color="black", size=10, face="bold"),
        axis.title.x = element_text(color="black", size=8, face = "plain"),
        axis.title.y = element_text(color="black", size=8, face="plain"))
p10

However, if I back-transform the variable with trans=exp in the code below, then the points no longer plot.

p11 <- visreg(mod, xvar = "Species", by = "Species", trans = exp,
             overlay = T, gg=T, points = T, line=list(col="black")) +
  scale_colour_manual(values = cb) +
  labs(color = "Species", fill = "Species") +
  theme_bw() +
  theme(legend.position='none',
        plot.title = element_text(color="black", size=10, face="bold"),
        axis.title.x = element_text(color="black", size=8, face = "plain"),
        axis.title.y = element_text(color="black", size=8, face="plain"))
p11

I am not sure if there is another way I need to be specifying the points? I have included geom_point(aes(color="Species")) before scale_colour_manual but I get Error in FUN(X[[i]], ...) : object 'Species' not found.

I would appreciate any help!

pbreheny commented 4 years ago

Hello again zdiazmar! This one, fortunately, is easy to solve: the argument for plotting the points (partial residuals) is called partial. Replace points=T with partial=T and your code works.

zdiazmar commented 4 years ago

Wonderful, thanks so much for your time and assistance!