r plotly 如何使用纬度、经度和 z 获得 3d 表面 [英] r plotly how to get 3d surface with lat, long and z
问题描述
我有大约 10 个位置的纬度、经度和数据值.这是我可以轻松为我的问题构建的数据框示例
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))
我想根据数据框中的 x、y 和 z 值创建一个 3d 表面.x 和 y 是纬度和经度.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.
我可以用 plot_ly(df, x = ~x, y = ~y, z = ~z) %>% add_markers(color = ~z)
做一个 3d 散点图,但添加add_surface 到此代码不起作用.
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.
涉及火山 df 的 plotly 3d 表面示例 (plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~volcano
) 使用均匀分布的 x 和 yvalues 和 z 是一个二维数组.如果我理解正确,我需要每个位置的 x 和 y 对.
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.
是否可以进行某种操作来创建 add_surface 代码所需的 z 矩阵?
Is there some kind of manipulation I can do to create the z matrix needed for the add_surface code?
推荐答案
有时写问题有助于找到答案.所需的操作是某种空间插值(克里金法)程序.这个 stackoverflow Q 和 A 解释了基础知识并给出了具体的例子.对于上述问题中描述的数据集,以下几行提供了解决方案.该链接还有其他方法.
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()
这篇关于r plotly 如何使用纬度、经度和 z 获得 3d 表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!