pbreheny / visreg

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

Color customization in the `plot.visreg2d` function #109

Closed AlexisZr closed 1 year ago

AlexisZr commented 1 year ago

plot.Visreg2d issue

Hey amazing work with visreg package!! I have a question regarding the color customization in the 3D plot. Is it possible to assign colors based on the values of the z-axis? In the provided image, the plot demonstrates a color gradient corresponding to the x-axis. However, I aim to achieve a similar gradient effect based on the z-axis values, where lower hazard ratios are represented by blue and higher hazard ratios by red. Could you kindly advise on how to accomplish this?"

Steps to reproduce the issue

#fit <- my coxph model
n_colors <- 99  
  color_palette <- colorRampPalette(c("blue","red"))(n_colors)
  vis_obj <- visreg2d(fit, x="prot_expr", y="age_of_onset",plot = F)
  haz3D <-plot(vis_obj,plot.type="rgl",xlab(label = "Protein_expression"), ylab(label = "NDD_onset"),
               zlab="Hazard ratio", col = color_palette)
  print(haz3D)

image

pbreheny commented 1 year ago

You're almost there, just apply cut() to z:

library(visreg)
fit <- lm(Ozone ~ Solar.R + Wind + Temp + I(Wind^2) + I(Temp^2) +
            I(Wind*Temp)+I(Wind*Temp^2) + I(Temp*Wind^2) + I(Temp^2*Wind^2),
          data=airquality)
v <- visreg2d(fit, x="Wind", y="Temp", plot=FALSE)
n_colors <- 99  
color_palette <- colorRampPalette(c("blue","red"))(n_colors)
zcol <- cut(v$z, n_colors)
plot(v, plot.type="rgl", col=color_palette[zcol])