gpt4 book ai didi

r - 使用 ggplot2 绘制多元高斯轮廓

转载 作者:行者123 更新时间:2023-12-02 14:23:11 25 4
gpt4 key购买 nike

我正在尝试使用已知均值和协方差的二维高斯分布的轮廓来增强绘图。理想情况下,我只需要指定函数,它就会以 2D 形式绘制(类似于 stat_function 除了 2 维之外)。我可以通过生成概率网格来使用 geom_raster 来完成此操作。我可以使用 geom_contour2d 来代替吗?

m <- c(.5, -.5)
sigma <- matrix(c(1,.5,.5,1), nrow=2)
data.grid <- expand.grid(s.1 = seq(-3, 3, length.out=200), s.2 = seq(-3, 3, length.out=200))
q.samp <- cbind(data.grid, prob = mvtnorm::dmvnorm(data.grid, mean = m, sigma = sigma))
ggplot(q.samp, aes(x=s.1, y=s.2)) +
geom_raster(aes(fill = prob)) +
coord_fixed(xlim = c(-3, 3), ylim = c(-3, 3), ratio = 1)

enter image description here

最佳答案

我在看2d的东西时找错了树。您可以使用 geom_contour 并添加 z 美学来实现我想要的效果:

m <- c(.5, -.5)
sigma <- matrix(c(1,.5,.5,1), nrow=2)
data.grid <- expand.grid(s.1 = seq(-3, 3, length.out=200), s.2 = seq(-3, 3, length.out=200))
q.samp <- cbind(data.grid, prob = mvtnorm::dmvnorm(data.grid, mean = m, sigma = sigma))
ggplot(q.samp, aes(x=s.1, y=s.2, z=prob)) +
geom_contour() +
coord_fixed(xlim = c(-3, 3), ylim = c(-3, 3), ratio = 1)

enter image description here

关于r - 使用 ggplot2 绘制多元高斯轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36221596/

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