gpt4 book ai didi

r - 如何将 2D 图像投影为 3D 散点图中的表面(在 R 中)?

转载 作者:行者123 更新时间:2023-12-01 12:28:13 24 4
gpt4 key购买 nike

我正在处理 3 维坐标数据,我在散点图中绘制它,我有大约 30.000 个数据点,我在这里包含了前 10 个,以便您可以重现它

library(rgl)
library(plot3D)
library(car)

df <- data.frame(meanX = c(147.34694,
173.89244,
135.73004,
121.93766,
109.72152,
92.53709,
165.46588,
169.77744,
127.01796,
99.34347),
meanY = c(140.40816,
110.99128,
134.56023,
164.18703,
166.04051,
155.97329,
105.29377,
104.42683,
130.17066,
155.99696),
avgDist = c(40.788118,
12.957329,
14.24348,
39.10424,
34.694258,
25.532335,
21.491695,
23.528944,
9.309201,
31.916879))

我一直在使用 scatter3d 函数来绘制它

scatter3d(x = df$meanX, y = df$meanY, z = df$avgDist, surface = FALSE)

现在我的“问题”是我想要一个二维表面,在 z=0 处叠加一个外部图像文件,作为奖励,如果我可以从散点图数据投影热图/等高线( meanX 和 meanY 也用于该图像上的轮廓),那会很棒。

这是我想在 z = 0 处覆盖的图像:

http://i.imgur.com/m6j4q3M.png

该图像是用这个 ggplot 制作的:

map.colors <- colorRampPalette(c("green","yellow","red"))

densityPlot <- ggplot(direData, aes(x = meanX, y = ,meanY)) +
stat_density2d(geom="tile", aes(fill=..density.., alpha=sqrt(sqrt(..density..))), contour=FALSE, n=100) +
scale_alpha(range = c(0, 1.0)) + scale_fill_gradientn(colours = map.colors(5)) +
xlim(70,185) + ylim(70,185)

minimap <- readPNG('~/yasp/minimap.png')

densityPlot + annotation_raster(minimap, ymin = 70 ,ymax=185 ,xmin = 70,xmax = 185) +
stat_density2d(geom="tile", aes(fill=..density.., alpha=10*sqrt(..density..)), contour=FALSE, n=100)

有什么办法吗?我在谷歌上搜索了很多解决方案,但没有找到真正的方法。我不介意首先在 ggplot2 中使用热图创建图像,保存它,然后将其用作表面覆盖的输入,但如果它可以在一次绘图调用中完成,那当然会很酷。

最佳答案

(第二次编辑)我尝试编写更好的代码并确认两个 xy 坐标相同。 ggplot2 theme with no axes or grid帮我只绘制面板区域。

library(rgl); library(grid); library(gtable)

df <- data.frame(meanX = c(147.34694, 173.89244, 135.73004, 121.93766,
109.72152, 92.53709, 165.46588, 169.77744,
127.01796, 99.34347),
meanY = c(140.40816, 110.99128, 134.56023, 164.18703,
166.04051, 155.97329, 105.29377, 104.42683,
130.17066, 155.99696),
avgDist = c(40.788118, 12.957329, 14.24348, 39.10424,
34.694258, 25.532335, 21.491695,23.528944,
9.309201, 31.916879))

map.colors <- colorRampPalette(c("green","yellow","red"))

# set scale_*_continuous() to plot only the panel region. limits mean xlim (or ylim)
# change "tile" into "raster" because of making noise lines on my screen
densityPlot <- ggplot(df[,1:2], aes(x = meanX, y = ,meanY)) +
stat_density2d(geom="raster", aes(fill=..density.., alpha=sqrt(sqrt(..density..))), contour=FALSE, n=100) +
scale_alpha(range = c(0, 1.0)) + scale_fill_gradientn(colours = map.colors(5)) +
scale_x_continuous(limits=c(70,185), expand = c(0,0)) + scale_y_continuous(limits=c(70,185), expand = c(0,0)) +
geom_point(size=4) # to test XY-coordinate (black points on the map)

open3d()
plot3d( df, type="s", radius=1, col="red", axes=F,
xlim = c(70,185), ylim = c(70,185),
expand = 1 )
plot3d( df, type="h", col="blue", add=T ) # to test XY-coordinate (line segments from z = 0)
axes3d(c("x","y","z") )
show2d({ # show2d uses 2D plot function's output as a texture on a box.
grid.draw(gtable_filter(ggplotGrob(densityPlot), "panel"))
},
expand = 1 , texmipmap = F ) # texmipmap = F makes tone clear (not essential)

# I think this is clearly better than using a intermediate file,
# so I deleted related code. Thanks Mike !

plot

关于r - 如何将 2D 图像投影为 3D 散点图中的表面(在 R 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37401864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com