0

I am new to R and having some trouble with visualizing some data. I have some data in this form: picture of data table

I used the function above in order to 3d plot them:

scatter3D(x=data$X, y=data$Y, z=data$Depth, bty="g", pch=20, cex=2, ticktype="detailed")

Now i want to add the names (labels) to each point (for example A1, A2 etc.) and create groups with different colours (for example points A1 to A3 blue, A4 to A6 red). I have no idea how i should do it.

g_souk
  • 1

1 Answers1

0

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. 3D plot

dcarlson
  • 10,936
  • 2
  • 15
  • 18