Since you did not provide data, it is impossible to provide an answer that relates directly to your question. Using dput()
to provide at least a subset of your data is always recommended. A picture of the data does not provide useful information. Also indicate the packages you have loaded. In this case it seems likely that you have loaded plot3D
which contains the scatter3D
function.
The manual page for for scatter3D
provides the answers to all of your questions (?scatter3D
) but it is detailed and can be challenging to follow. Studying the examples provided on the manual page, the following seems to accomplish what you want. It uses the USArrests
data set that is provided with R:
library(plot3D)
data(USArrests)
UP <- cut(USArrests$UrbanPop, breaks=quantile(USArrests$UrbanPop), include.lowest=TRUE)
This loads the data set and creates a categorical variable from the variable, UrbanPop
. Now the basic plot:
with(USArrests, scatter3D(Murder, Assault, Rape, colvar = as.numeric(UP),
col = c("red", "green", "blue", "violet"), theta = 60, phi = 20,
xlab = "Murder", ylab = "Assault", zlab = "Rape", main = "USA arrests",
cex = 1, pch=16, bty = "g", ticktype = "detailed",
d = 2, clab = "Urban Population", adj = 0.5, font = 2,
colkey=list(at = c(1.4, 2.1, 2.9, 3.6), side = 1,
addlines = TRUE, length = 0.75, width = 0.5,
labels = c("1st Quart", "2nd Quart", "3rd Quart", "4th Quart"))))
This creates the basic plot with circles colored by the quartile for percent urban population for each state. Getting the positions for labeling the legend (at=
) takes some trial and error. Now we add the text labels:
with(USArrests, text3D(Murder, Assault, Rape, colvar = as.numeric(UP),
col = c("red", "green", "blue", "violet"), colkey=FALSE,
labels = state.abb, cex = .75, adj=-.5, add=TRUE))
We use state.abb
which provides the two-character abbreviation for each state. 