【发布时间】:2016-07-03 03:42:22
【问题描述】:
我正在研究鲸鱼在特定海底结构周围的分布模式。我正在尝试创建一个同时显示的交互式 3D 绘图:
-
作为表面的测深(
x= 经度,y= 纬度,z= 深度)和 -
鲸群的地理位置(例如,
x= 经度,y= 纬度,z= 固定深度 -30 米)。
坐标在 UTM 坐标系中投影。
我通常使用 R 和 ggplot2 包来制作数字。在这里,plotly 包似乎是一个不错的选择。
我从测深栅格 bathy_ras 和点的 data.frame 开始 points。
> bathy_ras
class : RasterLayer
dimensions : 784, 821, 643664 (nrow, ncol, ncell)
resolution : 102, 111 (x, y)
extent : 755070, 838812, -2612148, -2525124 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=58S +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : bathymetry
values : -1949.42, -34.27859 (min, max)
> str(points)
'data.frame': 214 obs. of 3 variables:
$ x: num 774264 777293 775476 773430 773284 ...
$ y: num -2534165 -2533556 -2531012 -2532904 -2533695 ...
$ z: num -30 -30 -30 -30 -30 -30 -30 -30 -30 -30 ...
我找不到将我的两个数据集组合在同一个图形/同一个轴上的方法。我尝试了两种方法,但都没有给我想要的输出。
1) 使用 plotly 包在 Rstudio 中创建绘图。
#convert raster into a matrix of bathymetry values
bathy_matrix <- as.matrix(bathy_ras)
> str(bathy_matrix)
num [1:784, 1:821] -362 -365 -367 -369 -371 ...
#create interactive plot
p <- plot_ly(z=bathy_matrix, type="surface",showscale=FALSE)
#this works fine, it get a 3D interactive surface plot of the seabed
#but if I try to add the "points" layer it doesn't show on the plot
p <- plot_ly(z=bathy_matrix, type="surface",showscale=FALSE) %>%
add_trace(x = gp_seamounts_df$utmx, y = gp_seamounts_df$utmy, z = gp_seamounts_df$z, type = "scatter3d", mode = "markers", showscale=FALSE, opacity=0.98)
2) 完全从情节网站创建情节。 首先,我将栅格“bathy_ras”转换为包含所有坐标点 (x,y) 和深度 z 的矩阵
#convert raster into a dataframe
bathy_df <- as.data.frame(coordinates(bathy_ras))
bathy_df$z <- values(bathy_ras)
> str(bathy_df)
'data.frame': 643664 obs. of 3 variables:
$ x: num 755121 755223 755325 755427 755529 ...
$ y: num -2525179 -2525179 -2525179 -2525179 -2525179 ...
$ z: num -362 -361 -360 -359 -358 ...
我创建了一个阴谋帐户。我在我的 plotly 帐户中将两个数据框作为 .txt 文件导入:bathy_df 和 points。
这会在 plotly 帐户中创建两个网格。我可以轻松地为这两个 data.frames 编写两个单独的 3D 图:一个是曲面图(如下所示),另一个是散点图。我尝试在本教程 (http://help.plot.ly/update-a-graphs-data/) 之后将散点图作为新轨迹包含在曲面图中,但如果散点图为 3D,则“插入”选项似乎不可用。 surface plot produced from the plotly web interface
是否可以在plotly 中组合scatter3D 和曲面图?
nb:我尝试将raster::persp 与points() 结合使用,但我对表面图的一般审美不太满意,这就是为什么我更愿意与plotly 和/或ggplot2 一起执行此操作.
【问题讨论】: