An Easy Way to Make Ggplot2 Boxplots, ezplot - Part 2

Master R

By Guangming Lang Comment

Previously, I introduced the ezplot package and demoed how to use it to make nice looking ggplot2 bar charts easily and quickly. Today, I’m going to show you how to make nice looking boxplots. Once again, the code is super simple.

Prerequisites

  1. Install a set of development tools
    • On Windows, download and install Rtools.
    • On Mac, install the Xcode command line tools.
    • On Linux, install the R development package, usually called r-devel or r-base-dev.
  2. Install devtools by running install.packages("devtools") in R.

Install and Load ezplot

devtools::install_github("gmlang/ezplot")
library(ezplot)

The ezplot package comes with a films dataset obtain from IMBD.com, and I’ll use it to demo boxplots.

First, let’s draw boxplots of budget vs. years.

# call the films dataset make a function that will draw boxplots 
# using the variables in the films data set
plt = mk_boxplot(films)

# plot distributions of budget over the years
title1 = "Annual Distribution of Budget from 1913 to 2014"
p = plt("year_cat", "budget", ylab="budget", main=title1)
print(p)

center

We see the y-axis tick labels are expressed in scientific notations. This makes it difficult to read. We can use the comma scale on the y-axis, which will display the numbers in 000,000 format.

scale_axis(p, "y", scale = "comma")

center

We also observe that all the boxes are squashed down, indicating budget is heavily right-skewed. We can use either the log scale or the log10 scale on the y-axis. Once again, this can be easily done using the function scale_axis().

scale_axis(p, "y", scale = "log")

center

scale_axis(p, "y", scale = "log10")

center

I’m writing a book called ezplot: How to Easily Make ggplot2 Graphics for Data Analysis, and it is 20% complete. Take a sneak peek and get notified when the book is published.

If you enjoyed this post, get updates. It's FREE