(This article was first published on

**ExploringDataBlog**, and kindly contributed to R-bloggers)This post is the third in a series of four on boxplots and closely related data visualization techniques for comparing subsets of a dataset, or comparing different datasets that we hope or expect to be similarly distributed. The previous two posts in this series have dealt with the basic boxplot, simple variations like log transformations and variable-width boxplots, and the procedures used to identify and represent outliers in a boxplot display, both traditionally and in the face of significant asymmetry. All of the boxplot variations discussed so far retain the basic “box” structure, however, and this is one of the inherent limitations of this data display: their visual simplicity can hide a lot of important details about how the data values are distributed. A much more flexible extension of the basic boxplot is the

*violin plot*, constructed by combining the concept of the boxplot with that of nonparametric density estimates. Both boxplots and nonparametric density estimates are discussed in*Exploring Data*, but the idea of putting them together into a single data display was not. The violin plot discussed in this post is one way of achieving this combination, and my next post will discuss*beanplots,*also built from these same components. Violin plots are supported in*R*both as part of the**lattice**graphics package and via the add-on package**wvioplot**. Because I find it somewhat easier to use, this post discusses the second of these options.I have previously discussed the Old Faithful geyser data frame

**faithful**, included in the**datasets**package in the base*R*installation, in part because it provides a nice, real example of a dataset with a bimodal distribution of values. In fact, this data frame includes two variables – the waiting times between successive eruptions discussed last time, and the duration of each eruption – and it turns out that both of these variables exhibit strongly bimodal distributions. This point is illustrated clearly in the four plots shown above. The upper two plots are scatter plots of the observed values of the variables themselves, while the bottom two plots are nonparametric density estimates generated via the**density**command in R, using its default options. As in my previous discussion of the waiting time data from this dataset, the bimodal character of these variables is clear from the two pronounced peaks seen in these density estimates. Further, this bimodal character is also clearly evident in the scatter plots of the data values themselves, particularly in the duration data which shows a gap between approximately 2.5 and 3.0 where there are almost no points at all. The main reason for considering this example here is that bimodality represents one of those potentially significant distributional details that is generally lost in boxplots. This point is clearly illustrated in the figure shown below, which presents side-by-side boxplots of the waiting time divided by 20 on the left, and the duration of the eruptions on the right. (The waiting times were rescaled simply to make the two boxplots fit reasonably in the same figure.) Both plots suggest significant distributional asymmetry, but neither one gives any hint of the strongly bimodal character of these variables.For comparison, the two figures shown below were both generated using the

**wvioplot**package, which generates plots that may be viewed as boxplots whose “boxes” have been curved to reflect the estimated distribution of values over the observed data range. The plot on the left uses the default options for**wvioplot,**and one of the points of this example is that these default options are not always much more informative than the basic boxplot. This is certainly the case here: neither of the left-hand violin plots is at all suggestive of the bimodality we know to be a key characteristic of these variables. The right-hand violin plots represent are slightly customized, generated by specifying two options. First, note that the default violin plots on the left are both*clipped,*exhibiting hard limits at the smallest and largest values that appear in the data. Clipping is the default, but specifying “**clip = F**” overrides this behavior, which is not typical for nonparametric density estimates: instead of abruptly dropping to zero, these estimates typically decay smoothly to zero and, as a result, extrapolate somewhat beyond the observed data range. Specifying “**clip = F**” causes the density estimates used in the violin plots to exhibit this smooth decay behavior.The second, more important, difference between the two sets of violin plots shown in the above pair of figures is that the right-hand pair specifies a different bandwidth parameter for these nonparametric density estimators. In Chapter 8 of

*Exploring Data*, I discuss the trade-offs inherent in selecting the bandwidth parameter for nonparametric density estimators: making the bandwidth too small yields a high-variance, “noisy” estimate with a lot of spurious “details,” while making the bandwidth too large yields low-variance but highly biased estimates that can miss critical details that are present. For the violin plots generated by the**wvioplot**package, this bandwidth is specified by the**adjust**parameter, with the default value “**adjust = 3**” giving the results shown on the left. The plots on the right were generated using the smaller value, “**adjust = 1**”, which reduces the bandwidth of the density estimator enough to see the bimodal character of the two variables.One of the key points of this example is that, while the added capability inherent in the violin plot relative to the simpler boxplot can convey extremely useful details that we might otherwise miss, we can’t always rely on the default settings for the procedure to show us these details. The basic source of this difficulty is that more complex procedures like nonparametric density estimators (at least, more complex compared with the five-number summary on which the basic boxplot is based) have tuning parameters, and simply using the default options for these parameters doesn’t always lead to the results we want. For this reason, it is a good idea to spend a bit of time exploring the key options in using these procedures.

To

**leave a comment**for the author, please follow the link and comment on their blog:**ExploringDataBlog**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...