在R中设计多元密度图。

时间:2021-06-22 22:48:07

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和想知道有一个办法复制这个情节与我自己的数据在Tikz r .我不熟悉,但我发现这个引用这似乎暗示我可以使用这个特性在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

在R中设计多元密度图。

1 个解决方案

#1


9  

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). Then use the return value from persp to project the additional plotting info.

你可以先persp函数画出三维图(如果你这样做从数据而不是公式,那么你需要使用某种形式的密度估计第一,示例图看起来是足够光滑,这可能是基于公式而不是估计的数据)。然后使用从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:

这里有一些示例代码供您入门。它使用参数分布,但可适用于从质量中使用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' )

在R中设计多元密度图。

#1


9  

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). Then use the return value from persp to project the additional plotting info.

你可以先persp函数画出三维图(如果你这样做从数据而不是公式,那么你需要使用某种形式的密度估计第一,示例图看起来是足够光滑,这可能是基于公式而不是估计的数据)。然后使用从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:

这里有一些示例代码供您入门。它使用参数分布,但可适用于从质量中使用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' )

在R中设计多元密度图。