Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly 3d surface graph has incorrect x and y axis values

Tags:

r

plotly

I would like to make a 3d surface plot with plotly. This is going quite well, however the values on the x and y axis make no sense. They are much higher than they should be. I used a matrix for the plot, the x and y values are the row and column names and the z value (called elevation) of course the matrix itself. Plotly doesn't seem to use the values of the x and y data. The x and y values range from 0 to 6 in the matrix. This is a small sample of my matrix:

head(m.dune)    
>       1.90 1.95 2    2.05 2.01 2.15 
> 0     NA   NA   NA   NA   NA   NA
> 0.05  NA   NA   NA   NA   NA   NA
> 0.10  1.14 1.14 NA   NA   NA   NA
> 0.15  1.15 1.15 1.15 1.15 1.16 1.16
> 0.20  1.16 1.16 1.16 1.16 1.16 1.17
> 0.25  1.17 1.17 1.17 1.17 1.18 1.18

This is the code I have been using:

dune.plot <- plot_ly(z = m.dune, type = "surface") %>%
layout(
  scene=list(
    xaxis=list(title='x (m)'),
    yaxis=list(title='y (m)'),
    zaxis=list(title='Elevation (m)')))
dune.plot

The graph looks like this, with incorrect x and y axis, I am not sure how I can get correct values.

3d surface plot

like image 418
Marinka Avatar asked Sep 19 '16 15:09

Marinka


1 Answers

As Jimbou pointed out in his comment the values on the x and y axes are the number of rows and columns of your matrix. Since I have a cell size of 0.05, the values do not represent the correct distance. To show the correct distance you have to add the correct x and y values as list in the plotly graph. These x and y values have to have the same size, otherwise a part of the graphs gets cutoff. The code to get the correct x and y values is below:

y <- x <- as.numeric(rownames(m.dune))
# More rows than column, and the x and y should have the same size

dune1.plot <- plot_ly(z = m.dune1, x= x, y=y, type = "surface") %>%
layout(
  scene=list(
    xaxis=list(title='x (m)'),
    yaxis=list(title='y (m)'),
    zaxis=list(title='Elevation (m)')))
dune1.plot
like image 136
Marinka Avatar answered Oct 24 '22 06:10

Marinka