Replicating Plots – Boxplot Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:
matlab_boxplot

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.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)