Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

r plotly how to get 3d surface with lat, long and z

I have latitude, longitude and data value for about 10 locations. Here's an example of a data frame I can easily construct for my problem

x <- c("-108.6125","-108.5114","-108.805","-108.4014","-108.5615","-108.8349","-108.225","-108.3139","-108.5568","-108.4968")
y <- c("39.02205","39.22255","39.598","38.89478","39.06429","39.27625","39.03","39.1306","39.14823","38.89795")
z <- c("60.7735","56.45783","49.65","60.15","50","53.95417","50.825","56","55.843","38.73333")
df <- data.frame(x = as.numeric(x),y = as.numeric(y),z = as.numeric(z))

I'd like to create a 3d surface based on the x,y, and z values in the data frame. x and y are lat and long. z is the value at the lat, long pair.

I can do a 3d scatter plot with plot_ly(df, x = ~x, y = ~y, z = ~z) %>% add_markers(color = ~z) but adding add_surface to this code doesn't work.

The plotly 3d surface example involving the volcano df (plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~volcano) uses evenly spaced x and y values and z is a 2 dimensional array. If I understand correctly, I would need x and y pairs for each location.

Is there some kind of manipulation I can do to create the z matrix needed for the add_surface code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sometimes writing a question helps to find the answer to it. The manipulation needed is a spatial interpolation (kriging) program of some kind. This stackoverflow Q and A explains the basics and gives specific examples. With the data set described in the question above, the following lines provide a solution. The link also has other approaches as well.

library(akima)
library(plotly)
s = interp(x = df$x, y = df$y, z = df$z)
p <- plot_ly(x = s$x, y = s$y, z = s$z) %>% add_surface()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...