Evaluates every group's density on one grid spanning all of them, which is what lets the violins be compared. Groups with fewer than two distinct values get a flat zero row rather than an error, since a cluster with one cell is a real thing to encounter.
Arguments
- values
A named list of numeric vectors, one per violin. Names of the form
"feature|group"are split into the two key columns.- n
Grid resolution.
- adjust
Bandwidth multiplier, passed to
stats::density().
Examples
violin_density(list(`A|x` = rnorm(20), `A|y` = rnorm(20, 3)))
#> $data
#> feature group
#> 1 A x
#> 2 A y
#>
#> $grid
#> [1] -1.89002714 -1.78135330 -1.67267945 -1.56400560 -1.45533175 -1.34665790
#> [7] -1.23798406 -1.12931021 -1.02063636 -0.91196251 -0.80328867 -0.69461482
#> [13] -0.58594097 -0.47726712 -0.36859328 -0.25991943 -0.15124558 -0.04257173
#> [19] 0.06610212 0.17477596 0.28344981 0.39212366 0.50079751 0.60947135
#> [25] 0.71814520 0.82681905 0.93549290 1.04416675 1.15284059 1.26151444
#> [31] 1.37018829 1.47886214 1.58753598 1.69620983 1.80488368 1.91355753
#> [37] 2.02223137 2.13090522 2.23957907 2.34825292 2.45692677 2.56560061
#> [43] 2.67427446 2.78294831 2.89162216 3.00029600 3.10896985 3.21764370
#> [49] 3.32631755 3.43499140 3.54366524 3.65233909 3.76101294 3.86968679
#> [55] 3.97836063 4.08703448 4.19570833 4.30438218 4.41305602 4.52172987
#> [61] 4.63040372 4.73907757 4.84775142 4.95642526
#>
#> $density
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> A|x 1.429307e-01 1.587193e-01 0.1718085 1.821781e-01 1.903097e-01 1.970728e-01
#> A|y 1.319957e-17 3.895257e-17 0.0000000 2.844790e-17 5.832665e-17 4.702902e-16
#> [,7] [,8] [,9] [,10] [,11]
#> A|x 2.035314e-01 2.106775e-01 2.191953e-01 2.292977e-01 2.406818e-01
#> A|y 7.365325e-15 9.946299e-14 1.189703e-12 1.262996e-11 1.191841e-10
#> [,12] [,13] [,14] [,15] [,16]
#> A|x 2.526247e-01 2.642019e-01 2.745632e-01 2.831707e-01 2.899322e-01
#> A|y 1.005669e-09 7.523435e-09 4.990890e-08 2.936419e-07 1.532552e-06
#> [,17] [,18] [,19] [,20] [,21] [,22]
#> A|x 2.951457e-01 2.992806e-01 0.3026951972 0.3053878450 0.3068878519 0.30633384
#> A|y 7.096620e-06 2.916169e-05 0.0001063637 0.0003444338 0.0009905927 0.00253144
#> [,23] [,24] [,25] [,26] [,27] [,28]
#> A|x 0.302714551 0.29517494 0.28331775 0.26735110 0.24804265 0.22655294
#> A|y 0.005752268 0.01163632 0.02099554 0.03389853 0.04920901 0.06473837
#> [,29] [,30] [,31] [,32] [,33] [,34] [,35]
#> A|x 0.20418376 0.1821352 0.16133921 0.1423979 0.12561794 0.11107917 0.09871470
#> A|y 0.07808122 0.0876401 0.09307786 0.0949409 0.09403474 0.09132694 0.08843008
#> [,36] [,37] [,38] [,39] [,40] [,41] [,42]
#> A|x 0.08837087 0.07981500 0.07272891 0.06671078 0.06130642 0.05607315 0.0506589
#> A|y 0.08796997 0.09331602 0.10795500 0.13504750 0.17701395 0.23437615 0.3038392
#> [,43] [,44] [,45] [,46] [,47] [,48] [,49]
#> A|x 0.04487203 0.03871478 0.03236157 0.02609889 0.02024388 0.01506764 0.0107436
#> A|y 0.37734318 0.44375655 0.49319142 0.52114020 0.52920598 0.52183227 0.5018433
#> [,50] [,51] [,52] [,53] [,54] [,55]
#> A|x 0.007329615 0.004781608 0.002981968 0.001776449 0.001010605 0.0005488796
#> A|y 0.468569605 0.419991995 0.356921381 0.285725670 0.217494711 0.1642754342
#> [,56] [,57] [,58] [,59] [,60]
#> A|x 0.0002845429 0.0001407705 6.644943e-05 2.992331e-05 1.286705e-05
#> A|y 0.1345301041 0.1303104331 1.468819e-01 1.744918e-01 2.014725e-01
#> [,61] [,62] [,63] [,64]
#> A|x 5.289056e-06 2.074686e-06 7.765196e-07 2.772828e-07
#> A|y 2.176709e-01 2.171453e-01 1.992829e-01 1.680964e-01
#>
#> $median
#> [1] -0.05850356 3.21991262
#>