具有空间数据的 3D 交互式曲面图 [英] 3D interactive surface plot with spatial data
问题描述
我想创建一个湖泊深度的交互式 3D 表面图,最好使用 plotly
或 rgl
库.我从 Gauss-Krueger/EPSG:31468 CRS 等高线的 SpatialLinesDataFrame
中提取了我的数据,即公制单位.现在每条等高线都会产生一组具有相同深度值的坐标.生成的数据框相当大,但看起来像这样:
I would like to create an interactive 3D surface plot of depths in a lake, ideally using the plotly
or rgl
libraries. I have extracted my data from a SpatialLinesDataFrame
of contour lines in Gauss-Krueger/EPSG:31468 CRS, i.e. metric units. Now each contour line produces a set of coordinates with the same depth value. The resulting data frame is rather large, but looks something like this:
set.seed(41)
xx <- rnorm(100,4448929,100)
yy <- rnorm(100,5308097,100)
zz <- c(rep(-10,10),rep(-20,10),rep(-30,10),rep(-40,10),rep(-50,10),rep(-60,10),rep(-70,10),rep(-80,10),rep(-90,10),rep(-100,10))
df <- data.frame(xx,yy,zz)
我尝试使用 plotly
绘制数据,如 this 示例和 rgl
如this 帖子.在这两种情况下,我都会收到与我的数据不是矩阵格式相关的错误消息,即 x 和 y 值表示为行号和列号.
I have tried plotting the data with plotly
as in this example and with rgl
as in this post. In both cases I get error messages relating to my data not being in a matrix format, i.e. where x- and y-values are represented as row- and column-numbers.
有效的是在 plotly
中使用 add_trace
命令:
What does work, is using the add_trace
command in plotly
:
plot_ly() %>% add_trace(df,x = ~df$xx, y = ~df$yy, z = ~df$zz,type="mesh3d")
然而,生成的图形不仅缺少 add_surface
命令的花哨颜色图例,而且更重要的是,相对于 z 值扭曲了 x 和 y 值.z 值显示的太大了,尽管它们都具有相同的度量单位.
However, the resulting graph not only lacks the fancy colour legend of the add_surface
command, but more importantly, warps the x- and y-values in relation to the z-values. The z-values are shown much too large, although all have the same metric unit.
我也尝试将数据框重塑为矩阵,如 this 帖子,但它要么根本不起作用,要么给了我一个几乎完全由 NA 组成的矩阵.我只能推测,与该范围内所有坐标的 x-y 组合相比,附加了深度值的坐标数量非常少?
I have also tried reshaping the data frame to a matrix as in this post, but it either doesn't work at all, or gives me a matrix consisting almost entirely of NAs. I can only speculate that the number of coordinates that have depth values attached is very small in comparison to all x-y-combinations of coordinates in that range?
任何建议将不胜感激 - 谢谢!
Any suggestions will be much appreciated - thanks!
推荐答案
那些是随机定位的点,所以 rgl::persp3d
不能直接处理它们.但是,您可以按照 ?rgl::persp3d.deldir
中的示例对它们进行三角测量,然后进行绘图.例如,
Those are randomly located points, so rgl::persp3d
can't handle them directly. However, you can follow the example in ?rgl::persp3d.deldir
to triangulate them and then plot. For example,
dxyz <- deldir::deldir(df$xx, df$yy, z = df$zz, suppressMsgs=TRUE)
persp3d(dxyz, col = "lightblue")
这会导致一张非常难看的图片,但通过一些工作(例如修复轴标签,使用真实数据),您应该会得到一些合理的东西.
This results in a pretty ugly picture, but with some work (e.g. fixing the axis labels, using real data) you should get something reasonable.
这篇关于具有空间数据的 3D 交互式曲面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!