Learn R Programming

weights (version 1.1.1)

wtd.violinplot: 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, ...)

Value

A base R plot is produced showing weighted violin plots by group. No value is returned.

Arguments

x

Numeric vector of values.

group

Optional grouping factor indicating which group each value belongs to. If NULL, all values are treated as a single group.

weight

Optional numeric vector of weights, the same length as x. If NULL, equal weights are used.

bw

The smoothing bandwidth to be used, passed to density. Default is "nrd0".

adjust

A multiplicative bandwidth adjustment. The bandwidth used is actually adjust * bw. See density.

col

Color(s) for the filled violin shapes. Passed to vioplot.

border

Color(s) for the outline of the violins.

names

Optional vector of group names to be displayed on the x-axis. If NULL, levels of the group factor are used.

width

Width of the violin plots. Passed to vioplot.

na.rm

Logical. Should missing values be removed? Default is TRUE.

...

Additional graphical parameters passed to vioplot.

Author

Josh Pasek

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.

See Also

vioplot, density, wtd.hist, wtd.boxplot

Examples

Run this code
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"))

Run the code above in your browser using DataLab