wtd.violinplot {weights} | R Documentation |
Draw weighted violin plots by group
Description
wtd.violinplot
produces violin plots for weighted data by group using kernel density estimation.
Usage
wtd.violinplot(x, group = NULL, weight = NULL,
bw = "nrd0", adjust = 1,
col = "gray", border = "black",
names = NULL, width = 0.4,
na.rm = TRUE, ...)
Arguments
x |
Numeric vector of values. |
group |
Optional grouping factor indicating which group each value belongs to. If |
weight |
Optional numeric vector of weights, the same length as |
bw |
The smoothing bandwidth to be used, passed to |
adjust |
A multiplicative bandwidth adjustment. The bandwidth used is actually |
col |
Color(s) for the filled violin shapes. Passed to |
border |
Color(s) for the outline of the violins. |
names |
Optional vector of group names to be displayed on the x-axis. If |
width |
Width of the violin plots. Passed to |
na.rm |
Logical. Should missing values be removed? Default is |
... |
Additional graphical parameters passed to |
Details
This function uses kernel density estimates with weights to generate violin plots for each level of the grouping variable. Internally, it calls density
with the weights
argument, and constructs violin plots using vioplot
.
Value
A base R plot is produced showing weighted violin plots by group. No value is returned.
Author(s)
Josh Pasek
See Also
vioplot
, density
, wtd.hist
, wtd.boxplot
Examples
set.seed(123)
x <- c(rnorm(100), rnorm(100, mean = 2))
group <- rep(c("A", "B"), each = 100)
wts <- c(rep(1, 100), runif(100, 0.5, 2))
wtd.violinplot(x, group = group, weight = wts,
col = c("lightblue", "lightgreen"))
x2 <- c(seq(0,2,length.out=100), seq(0,6,length.out=100))
wts2 <- rep(1, 200)
wtd.violinplot(x2, group = group, weight = wts2,
col = c("lightblue", "lightgreen"))
wtd.violinplot(x2, group = group, weight = (wts2+.1)/(x2+.1),
col = c("lightblue", "lightgreen"))