【问题标题】:asymmetric violin plots or back-to-back density plots in RR中的不对称小提琴图或背靠背密度图
【发布时间】:2014-05-19 21:56:43
【问题描述】:

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

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

x <- rnorm(1000)
d <- density(x)
plot(d)

我知道density 返回密度估计的xy 分量,但我似乎无法将这些部分组合在一起。

【问题讨论】:

  • 你能提供一些你已经拥有的代码吗?如果您提供示例,社区会更容易提供帮助。

标签: r plot


【解决方案1】:

我会为此使用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)

【讨论】:

  • 不错。我确信我能找到一个副本,但我找不到任何非常接近的东西。 (好吧,直方图也一样,但还是有很大不同。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多