## Some examples of 3-D plots # persp will do some nice 3-D things # Try demo(persp) and help(persp) ## Otherwise... # Must load the lattice package first: library(lattice) ## Plotting some random normal values in 3-D my.x <- rnorm(15) my.y <- rnorm(15) my.z <- rnorm(15) cloud(my.z ~ my.x * my.y) # To create tick marks rather than arrows on axes: cloud(my.z ~ my.x * my.y, scales = list(distance = rep(1, 3), arrows = FALSE)) # Changing the axis limits cloud(my.z ~ my.x * my.y, xlim=c(-5,5), ylim=c(-5,5), zlim=c(-5,5)) # A more complicated example using variables in R's built-in "iris" data set. # Sepal.Length, Petal.Length, and Petal.Width are numeric variables measured # on the iris flowers. # Species is a categorical variables defining three different types of iris species. data(iris) cloud(Sepal.Length ~ Petal.Length * Petal.Width | Species, data = iris, screen = list(x = -90, y = 70), distance = .4, zoom = .6) # screen allows for rotation, zoom controls the size, and distance controls the perspective ## Some useful help files: help(cloud) help(panel.cloud) # wireframe is another option: g <- expand.grid(x = 1:10, y = 5:15, gr = 1:2) g$z <- log((g$x^g$g + g$y^2) * g$gr) wireframe(z ~ x * y, data = g, groups = gr, scales = list(arrows = FALSE), drape = TRUE, colorkey = TRUE, screen = list(z = 30, x = -60)) ## Read more about it in the cloud help file. # To use the scatterplot3d package, # Type install.packages("scatterplot3d") # and choose a mirror site: # Then load the package: library(scatterplot3d) with(iris, scatterplot3d(Sepal.Length, Petal.Length,Petal.Width, main="Iris measurements", xlab = "Sepal Length (cm)", ylab = "Petal Length (cm)",zlab = "Petal Width (cm)", pch = 16, color="darkblue") ) # Separating by species: my.cols <- c("blue", "orange", "green") with(iris, scatterplot3d(Sepal.Length, Petal.Length,Petal.Width, main="Iris measurements", xlab = "Sepal Length (cm)", ylab = "Petal Length (cm)",zlab = "Petal Width (cm)", pch = 16, color=my.cols[as.numeric(iris$Species)])) legend("right", legend = levels(iris$Species), col = c("blue", "orange", "green"), pch = 16)