Customising Violin Plots with Formula Input

Tom Kelly


Since boxplots have become the de facto standard for plotting the distribution of data most users are familiar with these and the formula input for dataframes. However this input is not available in the standard vioplot package. Thus it has been restored here for enhanced backwards compatibility with boxplot.

As shown below for the iris dataset, violin plots show distribution information taking formula input that boxplot implements but vioplot is unable to. This demonstrates the customisation demonstrated in the main vioplot vignette using vioplot syntax with the formula method commonly used for boxplot, t.test, and lm.

boxplot(Sepal.Length~Species, data = iris)

Whereas performing the same function does not work with vioplot (0.2).

devtools::install_version("vioplot", version = "0.2")
vioplot(Sepal.Length~Species, data = iris)
Error in min(data) : invalid 'type' (language) of argument

Plot Defaults

vioplot(Sepal.Length~Species, data = iris)

Another concern we see here is that the vioplot defaults are not aesthetically pleasing, with a rather glaring colour scheme unsuitable for professional or academic usage. Thus the plot default colours have been changed as shown here:

vioplot(Sepal.Length~Species, data = iris, main = "Sepal Length")

Plot colours: Violin Fill

Plot colours can be further customised as with the original vioplot package using the col argument:

vioplot(Sepal.Length~Species, data = iris, main = "Sepal Length", col="lightblue")


However the vioplot (0.2) function is unable to colour each violin separately, thus this is enabled with a vectorised col in vioplot (0.3):

Plot colours: Violin Lines and Boxplot

Colours can also be customised for the violin fill and border separately using the col and border arguments:

vioplot(Sepal.Length~Species, data = iris, main = "Sepal Length", col="lightblue", border="royalblue")

Similarly, the arguments lineCol and rectCol specify the colors of the boxplot outline and rectangle fill. For simplicity the box and whiskers of the boxplot will always have the same colour.

vioplot(Sepal.Length~Species, data = iris, main = "Sepal Length", rectCol="palevioletred", lineCol="violetred")

The same applies to the colour of the median point with colMed:

vioplot(Sepal.Length~Species, data = iris, main = "Sepal Length", colMed="violet")

### Combined customisation

These can be customised colours can be combined:

vioplot(Sepal.Length~Species, data = iris, main = "Sepal Length", col="lightblue", border="royalblue", rectCol="palevioletred", lineCol="violetred", colMed="violet")


These color and shape settings can also be customised separately for each violin: