Sharon Machlis
Contributing Writer

Easier ggplot with the ggeasy R package

how-to
Mar 24, 20205 mins
AnalyticsR Language

See easy-to-remember ways of customizing ggplot2 visualizations โ€“ plus the super-simple patchwork package to visualize plots side by side

Do More With R [video teaser/video series] - R Programming Guide - Tips & Tricks
Credit: Thinkstock

The ggplot2 data visualization R package is extremely powerful and flexible. However, itโ€™s not always easy to remember how to do every task โ€“ especially if youโ€™re not a frequent user. How do you change the size of a graph title? How do you remove legend titles? My usual solution is to save RStudio code snippets for things I have trouble remembering. But thereโ€™s also a package that can help: ggeasy.

As the name says, the goal of ggeasy is to, well, make ggplot2 easy โ€“ or at least easier. It has what some people may find to be more intuitive functions for typical tasks, mostly around text and axis formatting. (This package doesnโ€™t affect the way lines, points, and bars look and behave). All ggeasy functions start with easy_ so itโ€™s, yes, easy to find them using RStudio autocomplete. You can see how that works in the video above.

If youโ€™d like to follow along with my example below, ggeasy is on CRAN, so you can install it with install.packages("ggeasy"). I will also be using the ggplot2 (naturally), dplyr, rio, and lubridate packages. Later, I will add the patchwork package for super simple placement of multiple graphs; thatโ€™s also on CRAN.

For this example, Iโ€™m going to use data about whatโ€™s on most peopleโ€™s minds these days: coronavirus. You can download a CSV file with data by U.S. state from the Coronavirus Tracking Project with

download.file("http://covidtracking.com/api/states/daily.csv", 
              destfile = "covid19.csv")

(You can name the destfile destination file anything youโ€™d like.) I used rio::import() to import the data, but you can also use readr::read_csv(), read.csv(), data.table::fread(), or any other function to import the CSV.

With rio, the dates came in as integers, so Iโ€™ll use lubridateโ€™s ymd() function to turn that column into Date objects:

data$date <- lubridate::ymd(data$date)

To create a graph that is not too difficult to understand, Iโ€™ll filter this data for just a couple of states so there arenโ€™t 50 separate time-series lines. I chose Louisiana to see the rise in cases there โ€“ the Louisiana governor said the state has among the worldโ€™s fastest growth in cases. (There is speculation that Mardi Gras in February might have caused a cluster in New Orleans.) I will also add Massachusetts, a state with about 50 percent more people than Louisiana, since Iโ€™m based there.

After filtering the data, Iโ€™ll create a basic line graph of the data:

states2 <- filter(data, state %in% c("LA", "MA"))
ggplot(states2, aes(x = date, y = positive, color = state)) +
geom_line() +
geom_point() +
theme_minimal() +
ggtitle("Lousiana & Massachusetts Daily Covid-19 Cases")
01ggeasy Sharon Machlis, IDG

Basic graph of Louisiana and Massachusetts daily total COVID-19 cases made with ggplot1.

Thatโ€™s a pretty steep increase. Some of that may be due to an increase in testing โ€“ maybe we just know about more cases because testing ramped up. Iโ€™ll look at that in a minute.

First, though, how about a few tweaks to this graph?

Letโ€™s start by making the graph title larger. To use ggeasy, Iโ€™d start typing easy_ย in the RStudio top left source pane and scroll until I find what I want.ย 

RStudio autocomplete list for ggeasy functions starting with easy_. Sharon Machlis, IDG

Typing easy_ in RStudio helps find ggeasy functions.

easy_plot_title_size() looks like the function I need. I can change the graph title to 16-point type with this code:

ggplot(states2, aes(x = date, y = positive, color = state)) +
  geom_line() +
  geom_point() +
  theme_minimal() +
  ggtitle("Lousiana & Massachusetts Daily Covid-19 Cases") +
  easy_plot_title_size(16)

I can rotate x-axis text with easy_rotate_x_labels(90) for a 90-degree rotation, and remove the legend title (itโ€™s pretty obvious these are states) with easy_remove_legend_title(). The full graph code is below, including storing the graph in a variable called positives.

positives <- ggplot(states2, aes(x = date, y = positive, color = state)) +
  geom_line() +
  geom_point() +
  theme_minimal() +
  ggtitle("Lousiana & Massachusetts Daily Covid-19 Cases") +
  easy_plot_title_size(16) +
  easy_rotate_x_labels(90) +
  easy_remove_legend_title()
Graph with several ggeasy function tweaks including rotating x-axis text and increasing title size. Sharon Machlis, IDG

Graph with several ggeasy function tweaks including rotating x-axis text and increasing title size.

Next, Iโ€™d like to look at the negative coronavirus test results, to see if theyโ€™re rising at similar rates to positives. Iโ€™ll use the same code but just switch the y column to negatives.

negatives <- ggplot(states2, aes(x = date, y = negative, color = state)) +
  geom_line() +
  geom_point() +
  theme_minimal() +
  ggtitle("Lousiana & Massachusetts Negatives") +
  easy_plot_title_size(16) +
  easy_rotate_x_labels(90) +
  easy_remove_x_axis("title") +
  easy_remove_y_axis("title") +
  easy_remove_legend_title()
Graph of daily negative COVID-19 test results in Louisiana and Massachusetts Sharon Machlis, IDG

Graph of negative COVID-19 test results.

There seems to be a larger rise in positives than negatives in Louisiana. Although we donโ€™t know if thatโ€™s because testing criteria changed or something else.

It would be helpful to see these two graphs side by side. Thatโ€™s where the patchwork package comes in.ย 

With just these two lines of code, the first loading the patchwork package:ย 

library("patchwork")
positives + negatives

I get this:

Side-by-side ggplot2 graphs with the patchwork package. Sharon Machlis, IDG

Side-by-side ggplot2 graphs with the patchwork package.

Itโ€™s incredibly easy to place multiple graphs with patchwork. For more on how to customize layouts, head to the patchwork website.

I can now go back and use ggeasy to remove one of the legends so there arenโ€™t two, and then re-run patchwork:

negatives <- negatives +
  easy_remove_legend()
positives + negatives

Clearly, ggeasy is quite useful for some quick โ€“ and easy โ€“ data exploration!

For more R tips, head to theย โ€œDo More With Rโ€ page on InfoWorldย or check out theย โ€œDo More With Rโ€ YouTube playlist.