在单个3D图中混合表面和散点图 [英] Mixing surface and scatterplot in a single 3D plot
问题描述
我正在研究鲸鱼在特定海底结构周围的分布模式.我正在尝试创建同时显示的交互式3D图:
I am studying the patterns of distribution of whales around specific seabed structures. I am trying to create an interactive 3D plot showing at the same time:
- 作为表面的测深(
x
=经度,y
=纬度,z
=深度)和 - 鲸鱼群的地理位置(例如,
x
=经度,y
=纬度,z
=固定深度-30米).
- bathymetry as a surface (
x
= longitude,y
= latitude,z
= depth), and - geographic location of whale groups (
x
= longitude,y
= latitude,z
= fixed depth -30 meters for example).
坐标是在UTM坐标系中投影的.
Coordinates are projected in a UTM coordinate system.
我通常使用R和ggplot2
包来制作图形.在这里,plotly
软件包似乎是一个不错的选择.
I usually work with R and the ggplot2
package for producing figures. Here, the plotly
package seemed like a good option.
我从测深栅格bathy_ras
和点points
的data.frame开始.
I started with a bathymetry raster bathy_ras
and a data.frame of points 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 ...
我找不到在同一图形/同一轴上合并两个数据集的方法.我尝试了两种方法,但是都没有给我想要的输出.
I can't find a way to combine my two datasets on the same graph/same axis. I tried two methods but none gave me the output I wanted.
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的所有点的矩阵.
2) Creating the plot entirely from the plotly website. First I converted the raster "bathy_ras" into a matrix containing all points of coordinates (x,y) and depth 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 ...
我创建了一个帐户.我将两个数据框作为.txt文件导入了我的帐户:bathy_df
和points
.
I created a plotly account. I imported two dataframes as .txt files in my plotly account: bathy_df
and points
.
这将在plotly帐户中创建两个网格.我可以轻松地为这两个data.frames编写两个单独的3D图:一个是表面图(如下所示),另一个是散点图.在本教程之后,我尝试将散点图作为新轨迹包括在表面图中( http: //help.plot.ly/update-a-graphs-data/),但如果散点图为3D,则插入到"选项似乎不可用. 从plotly Web界面生成的表面图
This creates two grids in the plotly account. I can easily write two separate 3D plots for these two data.frames: one is a surface plot (shown below), the other is a scatterplot. I tried to include the scatterplot in the surface plot as a new trace following this tutorial (http://help.plot.ly/update-a-graphs-data/) but the "insert into" option just seem unavailable if the scatterplot is in 3D. surface plot produced from the plotly web interface
是否可以在plotly
中组合scatter3D
和表面图?
Is it possible to combine a scatter3D
and a surface plot in plotly
?
nb:我尝试将raster::persp
与points()
结合使用,但对表面图的总体美感并不十分满意,这就是为什么我更喜欢使用plotly
和/或ggplot2
进行此操作
nb: I tried raster::persp
in combinaison with points()
but I am not very satisfied by the general aesthetic of the surface plot, which is why I would prefer to do this with plotly
and/or ggplot2
.
推荐答案
我相信您正在做的事情应该可以正常工作.我认为这可能与您的x和y坐标有关.曲面图使用1:ncol(bathy_matrix)
作为x轴,使用1:row(bathy_matrix)
作为y轴点(如果需要,请打勾).
I believe what you are doing should work fine. I think it might have to do with your x and y coordinates. The surface plot is using 1:ncol(bathy_matrix)
as the x-axis and 1:row(bathy_matrix)
as the y axis points (ticks if you will).
您的点将需要在该范围内具有x和y坐标,以便它们显示在曲面图中.以下是一个简单的示例.
Your points will need to have x and y coordinates in that range for them to show up in the surface plot. Below is a simple example.
set.seed(123)
x = sample(1:ncol(volcano), size = 50)
y = sample(1:nrow(volcano), size = 50)
z = c()
for(i in 1:50) {z <- c(z, volcano[y[i], x[i]])}
df <- data.frame(x, y, z)
plot_ly(z = volcano, type = "surface") %>%
add_trace(data = df, x = x, y = y, z = z, mode = "markers", type = "scatter3d",
marker = list(size = 5, color = "red", symbol = 104))
我明白了:
希望这对您有帮助...
Hope this helps...
这篇关于在单个3D图中混合表面和散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!