![](/img/trans.png)
[英]Back-to-back histogram - how to 'merge' two different bar plots with qplot in R…?
[英]asymmetric violin plots or back-to-back density plots in R
我正在嘗試繪制一個不對稱的小提琴圖以比較兩個小提琴圖(我猜這基本上只是兩個背對背的內核密度估計圖)。 有沒有辦法用R的香草小提琴繪圖工具來做到這一點?
我知道我可以創建一個像這樣的密度圖:
x <- rnorm(1000)
d <- density(x)
plot(d)
而且我知道density
返回了密度估算的x
和y
分量,但是我似乎無法將各個部分放在一起。
我將為此使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.