简体   繁体   English

R:将2D坐标与3D绘图中的线连接,并根据坐标添加正态分布pdf

[英]R: Connect 2D coordinates with lines in 3D plot and add a normal distribution pdf based on the coordinates

Similar to 3D Plot of normal distribution in R around a (x,y) point using code like 类似于使用代码类似的围绕(x,y)点的R中的正态分布的3D图

library(rgl)
open3d()

x <- seq(0, 10, length=100)
y <- seq(0, 10, length=100)


z = outer(x,y, function(x,y) dnorm(x,2.5,1)*dnorm(y,2.5,1))

persp3d(x, y, z,col = rainbow(100))

I want to draw a normal distribution onto a (x,y) diagram. 我想在(x,y)图表上绘制正态分布。 However, differently to the first question, I want to draw this normal distribution along some specific coordinates (they have all z=0 and "lie on the ground" of (x,y)). 然而,与第一个问题不同,我想沿着一些特定坐标绘制这个正态分布(它们都具有z = 0并且“位于地面上”(x,y))。 They mimic the walk of a person and have coordinates 他们模仿人的行走并具有坐标

g=matrix(c(0,0,3,1,4,2,5,3,6,4,5,5,4,6,6,6,8,5,9,6)
  ,nrow=10,ncol=2,byrow= TRUE)

So my questions are: (1)How can I add this 2D data to the 3D plot and connect all points through a line such that it depicts the walk? 所以我的问题是:(1)如何将这些2D数据添加到3D图中并通过一条线连接所有点,以便描绘步行? It should lie on the ground. 它应该躺在地上。 (2) How can I draw a normal distribution around the "walk"? (2)如何在“步行”周围绘制正态分布? This should be 3D. 这应该是3D。

Thanks a lot 非常感谢

Update: The idea is to assign probabilities in a next exercise to the walk of a second person, too, and then compute the probability they met somewhere. 更新:想法是在下一个练习中也将概率分配给第二个人的行走,然后计算他们在某处遇到的概率。

Update2: I was maybe not very clear what I want, so I try my best to be more precise. Update2:我可能不是很清楚我想要什么,所以我尽力做到更精确。 The walk, as depicted by the coordinates in g, is just a rough line. 如g中的坐标所描绘的那样,步行只是一条粗线。 It should be displayed on the ground. 它应该显示在地面上。 The real task I am interested in is as follows: it is possible that the person is not exactly walking on the line. 我感兴趣的真正任务如下:这个人可能没有完全走在线上。 The probability, however, at any point is given by a Normal function (the PDF) of its shortest distance to the line. 然而,在任何点上的概率由其到线的最短距离的正常函数(PDF)给出。 So I want to draw a normal PDF multiple (probably infinite times) around the line. 所以我想在线周围绘制一个普通的PDF倍数(可能是无限次)。 The final 3D plot will probably look like some mountains. 最终的3D图可能看起来像一些山脉。 This is related to my earlier question where I asked how to draw a normal PDF multiple times such that its 3D plot looks like a vulcano. 这与我之前的问题有关,在该问题中,我问过如何多次绘制普通的PDF,以使其3D图看起来像是火山。

Update3: The answer below does not show the result to Update2 but it clearly provides guidance for a part of the original question. Update3:下面的答案没有向Update2显示结果,但它明确地为原始问题的一部分提供了指导。 Thus, I mark it as solved. 因此,我将其标记为已解决。

How about this. 这个怎么样。 (Note that I made the rainbow surface transparent). (注意,我使彩虹表面透明)。

library(rgl)
open3d()

x <- seq(0, 10, length=100)
y <- seq(0, 10, length=100)

z = outer(x,y, function(x,y) dnorm(x,2.5,1)*dnorm(y,2.5,1))

persp3d(x, y, z,col = rainbow(100),alpha=0.5)

# now draw the line 
#    note:
#    -  the "add=T" parameter that appends it to the previous 3d-plot

g=matrix(c(0,0,3,1,4,2,5,3,6,4,5,5,4,6,6,6,8,5,9,6)
         ,nrow=10,ncol=2,byrow= TRUE)

lines3d(g[,1],g[,2],0,color="purple",add=T)

Yielding: 产量:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM