R’s boxplot
function has a lot of useful parameters allowing us to change the behaviour and appearance of the boxplot graphs. In this exercise we will try to use those parameters in order to replicate the visual style of Matlab’s boxplot. Before trying out this exercise please make sure that you are familiar with the following functions: bxp
, boxplot
, axis
, mtext
Here is the plot we will be replicating:
We will be using the same iris dataset which is available in R by default in the variable of the same name – iris
. The exercises will require you to make incremental changes to the default boxplot style.
Answers to the exercises are available here.
If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.
Exercise 1
Make a default boxplot of Sepal.Width stratified by Species.
Exercise 2
Change the range of the y-axis so it starts at 2 and ends at 4.5.
Exercise 3
Modify the boxplot function so it doesn’t draw ticks nor labels of the x and y axes.
Exercise 4
Add notches (triangular dents around the median representing confidence intervals) to the boxes in the plot.
Exercise 5
Increase the distance between boxes in the plot.
Exercise 6
Change the color of the box borders to blue.
Exercise 7
a. Change the color of the median lines to red.
b. Change the line width of the median line to 1.
Exercise 8
a. Change the color of the outlier points to red.
b. Change the symbol of the outlier points to “+”.
c. Change the size of the outlier points to 0.8.
Exercise 9
a. Add the title to the boxplot (try to replicate the style of matlab’s boxplot).
b. Add the y-axis label to the boxplot (try to replicate the style of matlab’s boxplot).
Exercise 10
a. Add x-axis (try to make it resemble the x-axis in the matlab’s boxplot)
b. Add y-axis (try to make it resemble the y-axis in the matlab’s boxplot)
c. Add the y-axis ticks on the other side.
NOTE: You can use format(as.character(c(2, 4.5)), drop0trailing=TRUE, justify="right")
to obtain the text for y-axis labels.
Would not have been able to even start without reviewing the answers. Many of the arguments used are not in the “help” pages. Any good sources to read that are actually informative?
No information on
1. xaxt
2. yaxt
3 boxwex (not listed in args, description could mean different things
4. boxcol
5. medcol
6 medlwd
7 outcol
8 outpch
9 outcex
10 lab
11 mtext
12 tck
13 at
14 pretty
15 las
16 line
17drop()trailing
18 justify
19 hadj
20 padj
21 axis
Any wonder why I ignore base and use packages????
Hello and thank you for the feedback.
I noticed you are unsatisfied with the exercise, so let me try to defend it.
In the beginning of this exercise I left the following comment: “Before trying out this exercise please make sure that you are familiar with the following functions: bxp, boxplot, axis, mtext”
If you were to look at help(…) of those functions you would see all of your parameter questions answered. help(bxp) in particular – that’s why it is in the front.
Now despite that I think you made a good point – this exercise might require a deeper reading of the help pages than usual. But I tried making it engaging and incremental in complexity. I don’t think there is any harm done if you decided to stop after a certain step and look at the answers instead. In particular if (like you mentioned) you use 3rd party packages for plotting.
Hope this helps.