简体   繁体   English

在R中设计多变量密度图

[英]Designing multivariate density plot in R

I saw an appealing multivariate density plot using Tikz and was wondering if there was a way to replicate this plot with my own data within R. I am not familiar with Tikz, but I found this reference which seems to imply I may be able to use this feature within R. http://www.texample.net/tikz/examples/tikzdevice-demo/ 我看到了使用Tikz的一个吸引人的多变量密度图,并想知道是否有办法用R中的我自己的数据复制这个图。我不熟悉Tikz,但我发现这个参考似乎意味着我可以使用这个功能在R. http://www.texample.net/tikz/examples/tikzdevice-demo/

In short, what is the best way to produce a plot very similar (different distribution of course) to the one shown below using the two data samples provided? 简而言之,使用提供的两个数据样本生成与下图所示的图非常相似(当然不同的分布)的最佳方法是什么?

Here is some sample data that can be used to create the distribution plot. 以下是一些可用于创建分布图的示例数据。

# Sample data
var1 <- exp(rlnorm(100000, meanlog=0.03, sdlog=0.15))/100
var2 <- 1-(var1 + rnorm(100000, 0, 0.01))

Here is the reference page where I found the original chart 这是我找到原始图表的参考页面

https://tex.stackexchange.com/questions/31708/draw-a-bivariate-normal-distribution-in-tikz https://tex.stackexchange.com/questions/31708/draw-a-bivariate-normal-distribution-in-tikz

在此输入图像描述

You could start with the persp function to draw the 3 dimensional plot (if you do this from data rather than the formula then you need to use some form of density estimation first, the example plot looks smooth enough that it is probably based on the formula rather than estimated from the data). 您可以从persp函数开始绘制三维图(如果您从数据而不是公式执行此操作,则需要首先使用某种形式的密度估计,示例图看起来足够平滑,可能基于公式而不是从数据估计)。 Then use the return value from persp to project the additional plotting info. 然后使用persp的返回值来投影其他绘图信息。

There may also be an option using the rgl package, I seem to remember that it has a way to project a plot onto the axes planes. 可能还有一个使用rgl包的选项,我似乎记得它有一种方法将绘图投影到轴平面上。

Edit 编辑

Here is some sample code to get you started. 这里有一些示例代码可以帮助您入门。 It uses a parametric distribution, but could be adapted to use kde2d from MASS or other ways of estimating the density from data: 它使用参数分布,但可以适用于使用MASS中的kde2d或其他估算数据密度的方法:

x <- seq( -3, 3, length=25 )
y <- seq( -3, 3, length=25 )

z <- outer( x, y, function(x,y) dnorm(x,0,0.5)*dnorm(y,0,1) )
zl <- c(0,4*max(z))

## persp plot
trmat <- persp(x,y,z, theta=120, zlim=zl, box=FALSE, shade=0.5)

## x grid
for( i in seq(-3,3, by=0.5 ) ) {
    lines( trans3d( c(i,i), c(-3,-3), zl, trmat ), col='grey' )
}
for( i in seq(0,zl[2], length=7) ) {
    lines( trans3d( c(-3,3), c(-3,-3), c(i,i), trmat ), col='grey' )
}

## marginal for x

lines( trans3d( seq(-3,3,length=100), -3, dnorm(seq(-3,3,length=100),0,.5), 
    trmat), lwd=2, col='blue' )

## y grid
for( i in seq(-3,3, by=0.5 ) ) {
    lines( trans3d( c(-3,-3), c(i,i), zl, trmat ), col='grey' )
}
for( i in seq(0,zl[2], length=7) ) {
    lines( trans3d( c(-3,-3), c(-3,3), c(i,i), trmat ), col='grey' )
}

## marginal for y

lines( trans3d( -3, seq(-3,3,length=100), dnorm(seq(-3,3,length=100),0,1), 
    trmat), lwd=2, col='blue' )

在此输入图像描述

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

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