Changing axis displays for a graph using plot3D in R?

Viewed 315

I'm hoping to keep in the image below the ticks on the vertical z axis, but remove ticks and numbers from the x and y axes. I would like to be able to label my x and y axes with a label for each condition in my matrix, but have not figured out how to do this with text3D. For some reason (because I'm on a mac?) I can't download axes3D, which is one potential solution I've seen in other responses.

Here is my code:

x = c(0,1)
y = c(0,1)
zval = c(104.1861, 108.529, 110.3675, 110.4112)
z = matrix (zval, nrow=2, ncol=2, byrow=TRUE)
hist3D(x,y,z, zlim=c(101,111), colvar = NULL, d=2, col = "lightblue", NAcol = "white", breaks = NULL, colkey = NULL, theta=-60, phi=20, nticks=10, axes=TRUE, ticktype="detailed", space=0.5, lighting=TRUE, light="diffuse", shade=.5, ltheta = 50, bty = "g")

My output

output

Ultimately, I'd like something more along the lines of this:
enter image description here

I'm very new to R.

stackoverflow.com/questions/26794236/ggplot2-3d-bar-plot

^ this seems like it might be what I need, but I couldn't replicate the code without an error. When I tried to run this piece I got an error because my x and z (in this case) axes aren't numerical:

cloud(y~x+z, d, panel.3d.cloud=panel.3dbars, col.facet='grey', xbase=0.4, ybase=0.4, scales=list(arrows=FALSE, col=1), par.settings = list(axis.line = list(col = "transparent")))
1 Answers

Maybe this might be helpful (with the caveat that 3D plots can sometimes make interpretation more challenging).

First, I recreated a data frame d based on something similar to what you started with:

x = c(0, 0, 1, 1)
y = c(0, 1, 0, 1)
z = c(104.1861, 108.529, 110.3675, 110.4112)

d <- data.frame(
  x = factor(as.logical(x)),
  y = factor(as.logical(y)),
  z = z
)

Note that for x and y I converted the 0 and 1 to FALSE and TRUE with as.logical, then made them factors.

Then for the plot:

library(latticeExtra)

cloud(z ~ x + y, data = d, panel.3d.cloud=panel.3dbars, col.facet='grey', 
      xbase=0.4, ybase=0.4, scales=list(arrows=FALSE, col=1), 
      par.settings = list(axis.line = list(col = "transparent")))

You will want the formula as z ~ x + y where z is a numeric response.

Edit: If you wish to customize the axis labels, you can set the factor labels as follows (for example):

d <- data.frame(
  x = factor(as.logical(x), labels = c("Hi", "Lo")),
  y = factor(as.logical(y), labels = c("Label1", "Label2")),
  z = z
)

Plot

3D plot

Related