Varying box widths by the number of observations

In this recipe, we will learn how to vary box widths in proportion to the number of observations for each variable.

Getting ready

Just like the previous recipe, we will continue to use the metals.csv example dataset for this recipe. So, let's first load it:

metals<-read.csv("metals.csv")

How to do it...

Let's build a box plot with boxes of width proportional to the number of observations in the dataset:

boxplot(Cu ~ Source, data = metals,varwidth=TRUE,
main="Summary of Copper concentrations by Site")
How to do it...

How it works...

In the example, we set the varwidth argument to TRUE, which makes the width of the boxes proportional to the square roots of the number of observations in the groups.

We can see that the box for Site4 is the narrowest, as it has the least number of observations in the dataset. Differences in the other boxes' widths might not be so obvious, but this setting is useful when we are dealing with larger datasets. By default, varwidth is set to FALSE.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.172.75