0

I want to use the plot3D or persp3D functions from package rgl and already looked into the package description and the dummy-dataset "Volcano". But I'm actually not able to to the following with my data:

I have some data listed in 3 columns like

Col1 Col2 Col3
1    0.2   2 
2    0.5   5
3    0.6   9
4    19    8
5    1.3   10
6    0.1   60

and I wanted to create a surface plot in 3D with persp3D or plot3D. Of course I have more data but cant list it here. As I saw in the Volcano Plot (and if my suggestions are right) the Volcano data contains data, arranged like a matrix.
So I thought, to start I would like to set my data as a matrix like:

 2   5   9   8   10   
 60  1 0.2  NA   NA  
 NA  2  NA 0.5   NA
  3  NA  NA  0.6
  4  NA  NA  NA  19    

and so on

5
6

of course, I saw that the Matrix of Data from Volcano is complete and I have many NA's. Is there a possibility to calculate the NAs based on the model between the already measured values? I think the certainty of the calculated values is decreasing, the far away you calculate from the diagonal line of the measured values.

So now my questions:

How can I put my 3 columns into the matrix style and how can I calculate the missing NA's?

Thank you very much in advance, Chris

see above

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LC2H
  • 15
  • 6
  • Can you describe your data more clearly? What is Col1, Col2 and Col3? – Istrel Dec 27 '18 at 19:24
  • Your matrix data is not clear. Some values seem to be missing from the post. What are those 2 extra numbers, `5` and `6`? – Rui Barradas Dec 27 '18 at 19:48
  • There are functions `plot3D` and `persp3D`, but not in the `rgl` package, they're in `plot3D`. In `rgl` there are `plot3d` and `persp3d`. Uppercase vs lowercase makes a difference in R. – user2554330 Dec 27 '18 at 20:55
  • Hello, first, thank you all very very much for this fast help. I describe now my data more precisely, as you wished: There are 3 columns, the first column is a value with a range vom 3 to 12 which are unordered. Samples i measured for colour, giving a a*-value and ranges from 3 to 12 and these samples i measured also for other parameters like the next column with a range from 50 to 70 and the third column vom 0.5e-06 to 1 e-07 roughly (so these values are of the third cloumn pretty small) - i tried out the plot3D comment but with this example – LC2H Dec 28 '18 at 12:00
  • here is sometimes the message "Error in v[, it[2, i]] : subscript out of bounds". By changing the parameters in another ordering of x y and z, it worked and i got a very messed up 3-dimensional rotable plot which i cant interpret properly – LC2H Dec 28 '18 at 12:05

2 Answers2

0

If you want to create a 3D scatterplot, the plotly package is a great choice. You can directly run the code below to create an example plot. You just specify the x, y, z variables by referencing them with their variable names (see data frame mpg).

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

plot_ly(data = mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
  add_markers(color = ~cyl, colors = colorRamp(c("yellow", "darkred")),
              alpha = 0.7, size = I(1)) %>% colorbar(title = "Cylinder") %>%
  layout(title= "3D Scatterplot", scene = list(xaxis = list(title = "CTY"),
                                               yaxis = list(title = "HWY"), 
                                               zaxis = list(title = "CYL"),
                                               range = c(3, 8)))

Regarding NA values: Visualisation packages in R generally either exclude NA observations (rows) completely or they give an error. If they give an error, you can manually remove them (or replace them, which may not be a good idea though).

For removing rows with NA in any columns:

df = df[!apply(df, 1, anyNA), ]

Here is an extended example for that:

df = mtcars
df[4, 1] = NA
df[10, 4] = NA

# WITH NA
dim(df) # rows, columns in data set
anyNA(df) # any NAs in data set?
sum(is.na(df)) # number of NAs in data set

# REMOVING NA
df = df[!apply(df, 1, anyNA), ]
dim(df) # rows, columns in data set
anyNA(df) # any NAs in data set?
sum(is.na(df)) # number of NAs in data set
jollycat
  • 137
  • 3
  • 10
0

If your real data has as many NA values as your sample matrix, it is probably not going to work well in rgl::persp3d. What I'd suggest is that you stick with the 3 column format, including only non-NA values.

To do a surface plot of data in that format, see https://stackoverflow.com/a/53485406/2554330. The short version is this: If your data is in a dataframe called df, the code would be:

library(rgl)
dxyz <- deldir::deldir(x = df$col1, y = df$col2, z = df$col3, suppressMsge = TRUE)
plot3d(dxyz, col = "gray")
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Dear @user2554330, thank you very much, it worked now. Now i tried to apply inside the deldir command to use tesselation command but in 3d i cant apply it, with triangulation it workes. Like i Mentioned, i have for every point 3 colour values (L a and b) in which i want to colour the polygons or at least the triangles (where then should be used the mean of the 3 points surrounding the triangles) If needed, i can transform the Lab colours tom RGB colours or even into the HEX system. Thank you very very much for your fast help, Chris – LC2H Dec 29 '18 at 14:19
  • If you can convert your colours into R colours (e.g. `"#FF0000"` as an RGB code for red), then you can specify one colour per vertex pretty easily in `rgl` version 0.100.2 or greater. One colour per triangle is also easy, but given your data, it might be hard to figure out the triangles. See https://stackoverflow.com/a/53958326/2554330 for more details. – user2554330 Dec 29 '18 at 21:03
  • Dear user 2554330, thank you much for your answer. I tried now to use the voronoi diagramm style for 3D, because the surface of the polygons are more representative than the mean values of the triangles from the triangulation but it didnt work, do you know how i can solve that? – LC2H Dec 30 '18 at 17:20
  • You should post another question saying what you tried and what you wanted to happen that didn't. This time, make sure it contains a minimal, reproducible example: others should be able to run the code you post and see what you saw. – user2554330 Dec 30 '18 at 17:37