r绘图如何获得带有lat,long和z的3d曲面 [英] r plotly how to get 3d surface with lat, long and z

查看:82
本文介绍了r绘图如何获得带有lat,long和z的3d曲面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约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(plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~volcano)的可绘制3d表面示例使用均匀间隔的x和y值,而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?

推荐答案

有时写一个问题有助于找到答案.所需的操作是某种空间插值(kriging)程序.该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绘图如何获得带有lat,long和z的3d曲面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆