R:如何在图的特定区域内放置点

oknwwptz  于 2024-01-03  发布在  其他
关注(0)|答案(1)|浏览(75)

我正在使用R编程语言。
下面是一个3D钟形曲线(高斯分布):

library(plotly)
library(mvtnorm)  

mu <- c(0, 0)  
Sigma <- matrix(c(1, 0, 0, 1), nrow=2)  

x <- seq(-5, 5, length.out = 50)
y <- seq(-5, 5, length.out = 50)
grid <- expand.grid(x=x, y=y)

grid$z <- apply(grid, 1, function(x) dmvnorm(x, mean=mu, sigma=Sigma))

fig <- plot_ly(data = grid, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'lines',
               line = list(color = '#0066FF', width = 2)) %>%
    layout(title = "3D Normal Distribution Wireframe Plot",
           scene = list(xaxis = list(title = "X",
                                     gridcolor = 'rgb(255, 255, 255)',
                                     zerolinecolor = 'rgb(255, 255, 255)',
                                     showbackground = TRUE,
                                     backgroundcolor = 'rgb(230, 230,230)'),
                        yaxis = list(title = "Y",
                                     gridcolor = 'rgb(255, 255, 255)',
                                     zerolinecolor = 'rgb(255, 255, 255)',
                                     showbackground = TRUE,
                                     backgroundcolor = 'rgb(230, 230,230)'),
                        zaxis = list(title = "Z",
                                     gridcolor = 'rgb(255, 255, 255)',
                                     zerolinecolor = 'rgb(255, 255, 255)',
                                     showbackground = TRUE,
                                     backgroundcolor = 'rgb(230, 230,230)')),
           showlegend = FALSE)

fig

字符串
x1c 0d1x的数据

**我的问题:**我想用x,y范围(-1到1)之间的许多红点填充这个图的内部。

我试着用下面的代码来做这件事:生成随机点,计算每个随机点的z值,然后把这些点放在图上:

points <- data.frame(x = runif(100, -1, 1), y = runif(100, -1, 1))
points$z <- apply(points, 1, function(x) dmvnorm(x, mean=mu, sigma=Sigma))

fig <- fig %>%
    add_trace(data = points, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'markers',
              marker = list(color = 'red', size = 2))

fig <- fig %>%
    layout(title = "3D Normal Distribution Wireframe Plot",
           scene = list(xaxis = list(title = "X",
                                     gridcolor = 'rgb(255, 255, 255)',
                                     zerolinecolor = 'rgb(255, 255, 255)',
                                     showbackground = TRUE,
                                     backgroundcolor = 'rgb(230, 230,230)'),
                        yaxis = list(title = "Y",
                                     gridcolor = 'rgb(255, 255, 255)',
                                     zerolinecolor = 'rgb(255, 255, 255)',
                                     showbackground = TRUE,
                                     backgroundcolor = 'rgb(230, 230,230)'),
                        zaxis = list(title = "Z",
                                     gridcolor = 'rgb(255, 255, 255)',
                                     zerolinecolor = 'rgb(255, 255, 255)',
                                     showbackground = TRUE,
                                     backgroundcolor = 'rgb(230, 230,230)')),
           showlegend = FALSE)

fig


但是点出现在图的表面上-我希望点出现在图的内部。有什么方法可以解决这个问题吗?

谢谢你,谢谢

hgncfbus

hgncfbus1#

你需要计算x,y,z空间中的许多随机点,然后过滤掉那些z值超过给定x,y点密度的点:

library(mvtnorm)
library(plotly)

mu <- c(0, 0)  
Sigma <- matrix(c(1, 0, 0, 1), nrow=2)  
grid <- expand.grid(x = seq(-5, 5, len = 50), y = seq(-5, 5, len = 50))

grid$z <- dmvnorm(grid, mu, Sigma)

grid3d <- data.frame(x = runif(10000, -1, 1), 
                     y = runif(10000, -1, 1),
                     z = runif(10000, 0, 0.16))

grid3d <- grid3d[grid3d[[3]] < dmvnorm(grid3d[1:2], mu, Sigma),]

字符串
导致:

plot_ly(x = ~x, y = ~y, z = ~z) %>%
  add_trace(data = grid3d, type = "scatter3d", mode = "marker",
            marker = list(size = 2, color = "red", opacity = 0.2)) %>%
  add_trace(data = grid, x = ~x, y = ~y, z = ~z, type = 'scatter3d', 
            mode = 'lines', line = list(color = '#0066FF', width = 2),
            showlegend = FALSE) %>%
  layout(title = "3D Normal Distribution Wireframe Plot",
         scene = list(xaxis = list(title = "X",
                                   gridcolor = 'rgb(255, 255, 255)',
                                   zerolinecolor = 'rgb(255, 255, 255)',
                                   showbackground = TRUE,
                                   backgroundcolor = 'rgb(230, 230,230)'),
                      yaxis = list(title = "Y",
                                   gridcolor = 'rgb(255, 255, 255)',
                                   zerolinecolor = 'rgb(255, 255, 255)',
                                   showbackground = TRUE,
                                   backgroundcolor = 'rgb(230, 230,230)'),
                      zaxis = list(title = "Z",
                                   gridcolor = 'rgb(255, 255, 255)',
                                   zerolinecolor = 'rgb(255, 255, 255)',
                                   showbackground = TRUE,
                                   backgroundcolor = 'rgb(230, 230,230)')))


的数据
我个人认为,这给出了一个比体积情节更少的美学版本,我在之前的一个非常类似的问题中演示过

相关问题