Add quasi-random jitter on the x-axis for violin plots when the x-axis variable is categorical. Add random jitter within a rectangular area for square plots when both x- and y-axis variables are categorical.
Arguments
- X
A factor corresponding to a categorical variable.
- Y
A numeric vector of the same length as
X
forjitterViolinPoints
, or a factor of the same length asX
forjitterSquarePoints
.- grouping
A named list of factors of the same length as
X
, specifying how elements should be grouped.- ...
Further arguments to be passed to
offsetX
.
Value
For jitterViolinPoints
, a numeric vector is returned containing the jittered x-axis coordinates for all points.
For jitterSquarePoints
, a list is returned with numeric vectors X
and Y
, containing jittered coordinates on the x- and y-axes respectively for all points;
and summary
, a data.frame of frequencies and side lengths for each unique pairing of X/Y levels.
Details
The jitterViolinPoints
function calls offsetX
to obtain quasi-random jittered x-axis values.
This reflects the area occupied by a violin plot, though some tuning of arguments in ...
may be required to get an exact match.
The jitterSquarePoints
function will uniformly jitter points on both the x- and y-axes.
The jitter area is a square with area proportional to the frequency of the paired levels in X
and Y
.
If either factor only has one level, the jitter area becomes a rectangle that can be interpreted as a bar plot.
If grouping
is specified, the values corresponding to each point defines a single combination of levels.
Both functions will then perform jittering separately within each unique combination of levels.
This is useful for obtaining appropriate jittering when points are split by group, e.g., during faceting.
If grouping!=NULL
for jitterSquarePoints
the statistics in the returned summary
data.frame will be stratified by unique combinations of levels.
To avoid clashes with existing fields, the names in grouping
should not be "X"
, "Y"
, "Freq"
, "XWidth"
or "YWidth"
.
Examples
X <- factor(sample(LETTERS[1:4], 100, replace=TRUE))
Y <- rnorm(100)
(out1 <- jitterViolinPoints(X=X, Y=Y))
#> [1] 2.0500000 0.8893758 0.8899593 3.1496844 1.2750469 2.8582797 4.0399356
#> [8] 2.9502212 4.1586024 2.2993633 2.1701514 0.8373861 3.0614356 2.8564883
#> [15] 3.9096526 1.9705971 4.3486534 2.6564092 1.8501772 2.0186873 1.0484710
#> [22] 1.8692546 1.1465912 1.7734327 1.0684801 3.2628313 3.7296977 4.2307153
#> [29] 2.2861059 2.2326911 2.1669979 2.1078282 2.0912514 4.0000000 1.9032514
#> [36] 3.0000000 3.2480538 3.7998051 0.9771998 2.8616668 3.3419125 1.6947087
#> [43] 0.6687678 2.8118968 4.2003115 1.0000000 2.9582012 1.6505964 3.8251822
#> [50] 4.0435637 3.0219286 3.0886432 1.0161698 3.0809928 3.6255848 0.9336320
#> [57] 3.0459629 0.7500239 2.9581931 1.1117043 3.0332806 4.0652152 0.8541657
#> [64] 1.3251555 3.1588572 4.2212163 4.0249987 0.9237476 2.1339054 1.8640104
#> [71] 3.9418035 2.9377403 3.9313848 3.8797157 2.8538861 1.9562700 0.9533277
#> [78] 1.7362654 3.7252899 1.2470089 3.9510323 1.1620378 3.1647597 1.0698611
#> [85] 4.0077456 3.9521291 2.0000000 2.0501066 4.1181614 1.8615332 4.1450051
#> [92] 2.9302886 2.6881227 0.8649157 1.0374500 1.0213286 0.6734481 1.9271624
#> [99] 2.7501753 3.9513421
Y2 <- factor(sample(letters[1:3], 100, replace=TRUE))
(out2 <- jitterSquarePoints(X=X, Y=Y2))
#> $X
#> [1] 1.9495754 0.9089405 0.8355532 2.7921842 0.9289167 3.3719968 4.0551850
#> [8] 3.0936322 3.8339355 2.2090233 1.9911639 0.9178856 2.7849067 2.6732414
#> [15] 4.1884306 1.6585142 4.0172072 3.0375027 2.0144042 1.7661667 1.3309876
#> [22] 2.1969353 0.8230441 1.9872037 1.0450439 3.1682415 4.0610926 4.4092807
#> [29] 2.0856583 2.1613519 2.3900933 2.2408061 1.7809078 4.2548948 2.2495187
#> [36] 2.9354813 3.0667438 4.1146378 0.6753907 2.9297993 3.2306341 1.8941528
#> [43] 1.3886913 3.2207687 3.8995448 1.1880871 2.6634506 1.7543726 3.7957101
#> [50] 4.0295947 3.0729067 2.8662895 1.0878522 3.3666766 3.7001942 1.4097378
#> [57] 3.3386645 1.4813181 3.0579173 0.6321159 3.0766112 3.9626333 0.6973428
#> [64] 1.3784386 2.6402256 3.5967848 4.3088769 0.8134789 1.6352737 2.3912129
#> [71] 3.8615163 2.9142120 3.9533216 3.7445090 3.1273045 1.9938850 1.0676882
#> [78] 1.9147764 3.7387005 0.7567651 3.9364801 1.0414115 3.3833723 1.2492157
#> [85] 3.9606917 3.8790900 1.8135411 2.3012346 4.1220923 1.7318039 4.0225187
#> [92] 2.9698934 3.3245682 0.6685873 1.3314906 1.4628522 0.9984940 2.0710498
#> [99] 2.6767761 3.7938305
#>
#> $Y
#> [1] 1.0253240 2.3154660 0.7361730 2.2205276 3.0232770 2.3748163 0.7166442
#> [8] 0.9161478 1.0742420 1.7827311 2.5627558 0.6711853 1.2538303 0.7373006
#> [15] 1.9557981 2.5514525 1.0948882 1.7386785 1.9840793 3.0943681 1.6853035
#> [22] 2.0786747 1.3740544 2.0410120 3.0776355 1.6626202 3.1052209 2.8205924
#> [29] 0.9037369 2.2784551 3.4277475 1.9595635 3.3026770 0.7569484 2.8431712
#> [36] 2.8953870 0.8232270 2.6925559 1.5795500 0.9286359 2.2519533 2.0286958
#> [43] 1.7915189 3.0144174 3.1636657 1.7043558 2.7449274 0.7574729 1.6962600
#> [50] 1.8970808 0.6415096 0.6791356 3.1533535 2.3547780 3.1347330 2.1761730
#> [57] 2.0148579 1.7354062 2.6478222 1.6843982 1.6293848 1.7187418 0.9527499
#> [64] 0.6671415 1.0405145 2.1450451 3.2952328 1.5921611 1.8993768 2.8975969
#> [71] 2.7071206 3.0398466 2.8531299 0.8470482 2.2612219 0.8183248 0.6888380
#> [78] 1.1756410 3.3579325 1.6404724 1.6065929 1.1497526 1.6905962 2.8480246
#> [85] 1.6446501 2.0666914 2.9554693 2.9639995 1.6407072 2.9125304 1.6714144
#> [92] 2.8278190 2.7227578 1.0026810 2.3836906 2.4185117 2.4058246 3.0090779
#> [99] 1.8344844 3.3279402
#>
#> $summary
#> X Y Freq YWidth XWidth
#> 1 A a 8 0.3843876 0.3843876
#> 2 B a 5 0.3038851 0.3038851
#> 3 C a 8 0.3843876 0.3843876
#> 4 D a 5 0.3038851 0.3038851
#> 5 A b 13 0.4900000 0.4900000
#> 6 B b 8 0.3843876 0.3843876
#> 7 C b 11 0.4507344 0.4507344
#> 8 D b 10 0.4297584 0.4297584
#> 9 A c 4 0.2718031 0.2718031
#> 10 B c 11 0.4507344 0.4507344
#> 11 C c 7 0.3595617 0.3595617
#> 12 D c 10 0.4297584 0.4297584
#>
grouped <- sample(5, 100, replace=TRUE)
(out3 <- jitterViolinPoints(X=X, Y=Y, grouping=list(FacetRow=grouped)))
#> [1] 2.0000000 1.0000000 1.0412141 2.7013931 0.8261786 3.0000000 3.8000093
#> [8] 3.0000000 4.0682049 2.1964634 1.8015925 1.0927543 2.8640032 3.0838742
#> [15] 4.0000000 1.7062433 3.7314457 2.8001936 2.0732271 1.8559668 1.0000000
#> [22] 2.1987191 0.7783999 1.7721889 1.0000000 2.8246809 3.9037355 4.0000000
#> [29] 1.8000918 2.0000000 1.7030832 2.0788154 2.1999765 4.0000000 1.8988630
#> [36] 3.0000000 3.1980088 4.1773090 0.7020831 3.1993675 2.7004018 2.1998774
#> [43] 1.1999640 3.1499881 4.1999730 1.0000000 2.9459003 1.7026550 4.0813577
#> [50] 4.0000000 3.0953857 2.8256806 0.9135163 3.0864922 3.7005752 0.8022788
#> [57] 3.1820901 0.8060702 2.8675600 1.0996635 2.7988095 3.8003129 0.8013484
#> [64] 1.1922533 3.1926924 4.0963185 3.8084914 1.1893310 1.8836070 2.0000000
#> [71] 3.8894219 3.0000000 3.8179035 3.9362155 2.9383965 2.0668687 1.1714601
#> [78] 2.0000000 4.1934024 1.0000000 3.7418518 0.7096929 2.9145301 0.8285490
#> [85] 4.0595573 4.0000000 2.0000000 2.0472169 4.1995874 2.1902503 4.1871270
#> [92] 3.0000000 2.8000105 0.7202545 1.0459592 1.1948891 0.7524773 1.9685616
#> [99] 2.7005313 3.7768063
(out4 <- jitterSquarePoints(X=X, Y=Y2, grouping=list(FacetRow=grouped)))
#> $X
#> [1] 2.2590928 0.5812832 0.8684062 3.1806678 0.6842768 2.7119407 3.8662549
#> [8] 3.1423057 4.4881485 1.8371824 2.1179192 0.8226591 3.2428385 3.3264162
#> [15] 4.3029899 2.3086500 3.6569952 2.7614328 2.1037213 2.0432154 1.0046870
#> [22] 2.1705932 1.1828541 1.9000355 1.0734125 2.7340626 4.2256856 4.2663529
#> [29] 2.0266805 2.3541803 1.7326689 1.8346648 2.1867047 4.2548683 2.1717980
#> [36] 3.1445993 2.7341369 4.0950611 1.0342489 2.6789913 3.3135840 2.3198187
#> [43] 1.2621425 2.5310520 4.2078611 0.9054950 3.0046218 1.8273601 4.2266935
#> [50] 3.6862434 2.9648024 3.0101755 1.3188277 2.6865164 4.1480814 1.2263403
#> [57] 3.0402191 0.7431071 3.4216718 0.8718275 2.8441430 3.9934466 0.7995416
#> [64] 1.1171310 2.9188390 4.0183045 3.9754214 1.3946246 1.9503191 2.4045957
#> [71] 4.0685773 3.1540228 4.0093479 4.1535895 3.2580416 1.8664209 0.9955560
#> [78] 2.1720224 3.9126513 1.3849157 3.8754973 0.8184985 3.0062110 0.9580382
#> [85] 4.1128491 3.5373597 2.0511810 1.8787725 3.5846609 2.0113190 4.0191847
#> [92] 3.0039147 3.1813530 1.2933452 1.2040126 0.8668507 1.0500220 1.5525244
#> [99] 2.7785808 3.8490815
#>
#> $Y
#> [1] 0.7230860 2.1607144 0.9402148 1.6325615 3.0954073 1.9291382 1.0745604
#> [8] 1.0261577 1.4548174 1.8624174 3.4544657 0.9244661 0.9263742 1.2634243
#> [15] 1.5252874 2.8058292 0.5437814 2.0688041 2.1015116 2.6488238 2.0845076
#> [22] 2.2045320 0.8643915 1.7699437 2.8127755 1.6139196 3.0303953 2.6922492
#> [29] 1.0499866 1.5790822 3.0280645 1.7680425 2.8138475 1.0657584 2.9513193
#> [36] 3.1677430 0.8743490 3.3368421 1.5976091 1.0472788 2.0105033 1.5941144
#> [43] 1.8258451 3.1939583 3.0394290 1.7880017 3.1098410 1.2877602 2.2467412
#> [50] 1.9247354 1.0617190 1.0237407 2.8140349 1.9584184 3.0359282 2.1886321
#> [57] 2.1336434 1.9172594 2.9814459 2.1744216 2.4483620 2.3291493 0.7021384
#> [64] 0.7984457 1.0257671 2.2324504 3.3779691 1.9341709 1.7576484 3.3523066
#> [71] 3.3600712 2.9768893 2.9976547 1.0553769 2.1076202 0.8655904 1.2438560
#> [78] 0.9774184 2.7878202 1.7778623 2.4366301 0.6324694 1.5347797 3.2241110
#> [85] 1.5420229 1.6229215 2.6814903 3.0683635 1.7063840 3.3778621 1.8840198
#> [92] 3.2085969 2.9606370 1.1008625 1.7388157 2.1191842 2.4016960 2.8881182
#> [99] 1.8020365 3.2216740
#>
#> $summary
#> X Y Freq YWidth XWidth FacetRow
#> 1 A a 2 0.3464823 0.3464823 1
#> 2 B a 2 0.3464823 0.3464823 1
#> 3 C a 2 0.3464823 0.3464823 1
#> 4 D a 0 0.0000000 0.0000000 1
#> 5 A b 3 0.4243524 0.4243524 1
#> 6 B b 2 0.3464823 0.3464823 1
#> 7 C b 3 0.4243524 0.4243524 1
#> 8 D b 4 0.4900000 0.4900000 1
#> 9 A c 0 0.0000000 0.0000000 1
#> 10 B c 1 0.2450000 0.2450000 1
#> 11 C c 1 0.2450000 0.2450000 1
#> 12 D c 1 0.2450000 0.2450000 1
#> 13 A a 1 0.2450000 0.2450000 2
#> 14 B a 1 0.2450000 0.2450000 2
#> 15 C a 0 0.0000000 0.0000000 2
#> 16 D a 1 0.2450000 0.2450000 2
#> 17 A b 3 0.4243524 0.4243524 2
#> 18 B b 1 0.2450000 0.2450000 2
#> 19 C b 0 0.0000000 0.0000000 2
#> 20 D b 0 0.0000000 0.0000000 2
#> 21 A c 2 0.3464823 0.3464823 2
#> 22 B c 4 0.4900000 0.4900000 2
#> 23 C c 2 0.3464823 0.3464823 2
#> 24 D c 3 0.4243524 0.4243524 2
#> 25 A a 2 0.4900000 0.4900000 3
#> 26 B a 1 0.3464823 0.3464823 3
#> 27 C a 1 0.3464823 0.3464823 3
#> 28 D a 2 0.4900000 0.4900000 3
#> 29 A b 2 0.4900000 0.4900000 3
#> 30 B b 2 0.4900000 0.4900000 3
#> 31 C b 1 0.3464823 0.3464823 3
#> 32 D b 1 0.3464823 0.3464823 3
#> 33 A c 0 0.0000000 0.0000000 3
#> 34 B c 0 0.0000000 0.0000000 3
#> 35 C c 2 0.4900000 0.4900000 3
#> 36 D c 2 0.4900000 0.4900000 3
#> 37 A a 2 0.4000833 0.4000833 4
#> 38 B a 1 0.2829016 0.2829016 4
#> 39 C a 3 0.4900000 0.4900000 4
#> 40 D a 0 0.0000000 0.0000000 4
#> 41 A b 3 0.4900000 0.4900000 4
#> 42 B b 1 0.2829016 0.2829016 4
#> 43 C b 1 0.2829016 0.2829016 4
#> 44 D b 3 0.4900000 0.4900000 4
#> 45 A c 0 0.0000000 0.0000000 4
#> 46 B c 3 0.4900000 0.4900000 4
#> 47 C c 2 0.4000833 0.4000833 4
#> 48 D c 2 0.4000833 0.4000833 4
#> 49 A a 1 0.2000417 0.2000417 5
#> 50 B a 0 0.0000000 0.0000000 5
#> 51 C a 2 0.2829016 0.2829016 5
#> 52 D a 2 0.2829016 0.2829016 5
#> 53 A b 2 0.2829016 0.2829016 5
#> 54 B b 2 0.2829016 0.2829016 5
#> 55 C b 6 0.4900000 0.4900000 5
#> 56 D b 2 0.2829016 0.2829016 5
#> 57 A c 2 0.2829016 0.2829016 5
#> 58 B c 3 0.3464823 0.3464823 5
#> 59 C c 0 0.0000000 0.0000000 5
#> 60 D c 2 0.2829016 0.2829016 5
#>