Color by group and legend in plot3D

91 Views Asked by At

I'm trying to create my own 3D histogram using plot3D, so I took a look at this page as a starter before applying it on my own data. The code is clear and straightforward enough but I wonder how I can group rows of data (formatted as matrix indices in the example VADeaths data) into fewer categories (e.g., 50-54 & 55-59 --> "Fifties", 60-64 & 65-69 --> "Sixties", 70-74 --> "> 70") and assign them to different colors (e.g, "Fifties: blue", "Sixties: yellow", "> 70: white")? And, also, to have this information displayed on the legend to the right of the 3D plot?

Below is a working example listed on the reference page.

# install.packages("plot3D")
library(plot3D)

data(VADeaths)
# plotting
hist3D (x = 1:5, y = 1:4, z = VADeaths,
        bty = "g", phi = 20,  theta = -60,
        xlab = "", ylab = "", zlab = "", main = "VADeaths",
        col = "#0072B2", border = "black", shade = 0.8,
        ticktype = "detailed", space = 0.15, d = 2, cex.axis = 1e-9)
        
# label x axis: age group (this part is to be sorted into fewer categories)
 text3D(x = 1:5, y = rep(0.5, 5), z = rep(3, 5),
       labels = rownames(VADeaths),
       add = TRUE, adj = 0)
       
# label y axis: gender by urbal/rural residency
 text3D(x = rep(1, 4),   y = 1:4, z = rep(0, 4),
       labels  = colnames(VADeaths),
       add = TRUE, adj = 1)

enter image description here

I've referenced this earlier post but can't seem to understand it. It'll be really appreciated if someone could share some tips on this.

0

There are 0 best solutions below