R中的不对称小提琴图或背靠背密度图

use*_*388 2 plot r

我试图绘制一个不对称的小提琴图来比较两个小提琴图(我猜这基本上只是两个核心密度估计图背靠背).有没有办法用R的香草小提琴绘图工具做到这一点?

我知道我可以创建一个像这样的密度图:

x <- rnorm(1000)
d <- density(x)
plot(d)
Run Code Online (Sandbox Code Playgroud)

我知道,density返回xy密度估计的成分,但我似乎不能把拼在一起.

Gre*_*gor 6

我会用density它,它应该可以正常工作.我不认为你通过做"小提琴"的风格获得了很大的收获,反映在0线上.我个人认为这种比较更容易相互比较.

a <- rnorm(20)
b <- rnorm(50)


ad <- density(a)
bd <- density(b)

abd <- list(x = c(ad$x, bd$x),
            y = c(ad$y, bd$y))

# "violin" style comparison (psuedo-mirrored), switch x and y to make vertical.
plot(range(abd$x), c(-max(abd$y), max(abd$y)), type = "n")
lines(ad$x, ad$y, type = "l")
lines(bd$x, -bd$y, type = "l")
abline(h = 0)

# on top of each other comparison, would nicely generalize for more distributions
plot(range(abd$x), range(abd$y), type = "n")
lines(ad)
lines(bd)
Run Code Online (Sandbox Code Playgroud)