R:制作透明的情节

ou6hu8tu  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(80)

我正在使用R编程语言。
下面是一个用Python(https://www.geeksforgeeks.org/3d-wireframe-plotting-in-python-using-matplotlib/)制作的透明线图:

# importing modules
from mpl_toolkits import mplot3d
import numpy 
from matplotlib import pyplot
 
# assigning coordinates    
a = numpy.linspace(-5, 5, 25)
b = numpy.linspace(-5, 5, 25)
x, y = numpy.meshgrid(a, b)
z = numpy.sin(numpy.sqrt(x**2 + y**2))
 
# creating the visualization
fig = pyplot.figure()
wf = pyplot.axes(projection ='3d')
wf.plot_wireframe(x, y, z, color ='green')
 
# displaying the visualization
wf.set_title('Example 2')
pyplot.show()

字符串


的数据

**我的问题:**我试图在R中为二维正态分布重新创建这种图

我知道如何在R中绘制一个规则的二维正态分布:

library(plotly)
library(MASS)
library(mvtnorm)

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

x <- seq(-3, 3, length.out = 100)
y <- seq(-3, 3, length.out = 100)
grid <- expand.grid(x=x, y=y)

z <- matrix(apply(grid, 1, function(x) dmvnorm(x, mean=mu, sigma=Sigma)), nrow=length(x), ncol=length(y))

plot_ly(x = x, y = y, z = z, type = "surface") %>%
    add_trace(
        type = "surface",
        showscale = FALSE,
        opacity = 0.5,
        surfacecolor = matrix(rep(1, length(x) * length(y)), nrow=length(x), ncol=length(y)),
        colorscale = list(c(0, 1), c("rgb(0,0,0)", "rgb(0,0,0)")),
        lighting = list(ambient = 1, diffuse = 0, fresnel = 0, specular = 0, roughness = 1)
    ) %>%
    layout(scene = list(xaxis = list(title = 'X1'),
                        yaxis = list(title = 'X2'),
                        zaxis = list(title = 'Density')))



但我不知道如何让这个图看起来像“透明丝网”风格。我能找到的最接近的东西是这样的:https://www.r-bloggers.com/2010/05/creating-surface-plots/
有人能告诉我怎么做吗?注意:情节不需要是3D的。2D或3D都可以。
谢谢你,谢谢

**编辑:**我认为这是可能的。

library(plotly)

# Creating the data
x <- seq(-5, 5, length.out = 50)
y <- seq(-5, 5, length.out = 50)
grid <- expand.grid(x = x, y = y)
R <- sqrt(grid$x^2 + grid$y^2)
z <- sin(R)

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

# Print 
fig

wgx48brx

wgx48brx1#

点阵封装具有wireframe()函数(https://cran.r-project.org/web/packages/lattice/lattice.pdf),例如

library(tidyverse)
library(lattice)
library(mvtnorm)
set.seed(123)

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

x <- seq(-3, 3, length.out = 100)
y <- seq(-3, 3, length.out = 100)
grid <- expand.grid(x=x, y=y)
grid$z <- apply(grid, 1, function(x) dmvnorm(x, mean=mu, sigma=Sigma))

wireframe(z ~ x * y, data = grid, aspect = c(61 / 89, 0.3))

字符串
x1c 0d1x的数据
如果你使用更少的数据点,你可以更好地看到“线”:

wireframe(z ~ x * y, data = grid[1:2500,], aspect = c(10 / 10, 1))



下面是一个复制你的python示例的可能方法:

x <- read_csv("/Users/jared/x.csv", col_names = NULL) %>%
  pivot_longer(everything(),
               values_to = "x")
#> Rows: 25 Columns: 25
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (25): X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, ...
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
y <- read_csv("/Users/jared/y.csv", col_names = NULL) %>%
  pivot_longer(everything(),
               values_to = "y")
#> Rows: 25 Columns: 25
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (25): X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, ...
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
z <- read_csv("/Users/jared/z.csv", col_names = NULL) %>%
  pivot_longer(everything(),
               values_to = "z")
#> Rows: 25 Columns: 25
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (25): X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, ...
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

python_data <- data.frame(x, y, z)
wireframe(z ~ x * y, data = python_data, aspect = c(10 / 10, 0.75),
          col.regions = "transparent", col = "green3")

创建于2023-12-13使用reprex v2.0.2

相关问题