Hi. I’m Sharon Machlis at IDG Communications, here with Episode 44 of Do More With R: Make
ggplot easier with ggeasy. Plus, a patchwork bonus!
ggplot2 is incredibly powerful and flexible. But it’s not always easy to remember how
to do some things – 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 tasks that I can never remember. But there’s also a package that can help:
ggeasy.
Like the name says, the goal of ggeasy is to, well, make ggplot2 easier. It has what
you may find to be more intuitive functions for some common tasks – mostly around text
and axis formatting (not if you want to change the way lines or points behave). All ggeasy
functions start with easy underscore, so it’s easy to find them using RStudio autocomplete.
Let’s take a look.
If you don’t have it on your system yet, ggeasy is now on CRAN so you can install it
with install.packages.
For this demo, I’m going to use data about what’s on most people’s minds these days:
Coronavirus.
You can download data by state from the Coronavirus Tracking Project at Covid Tracking dot com.
I’ll take a quick look at the data structure. The dates came in as numbers, so I’ll use
lubridate’s year-month-date function to change them into date objects. (That’s the
y.m.d. function). Finally, I’m going to filter this data for Louisiana to see the
rise in cases there. There’s been speculation that Mardi Gras in February might have caused
a cluster in New Orleans. And I’ll also include data from Massachusetts, a state with
about 50% more people, for comparison.
Here’s a basic ggplot line graph of the data. 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. We’ll look at that in a minute.
For now, though, how about tweaking this graph?
Let’s say I’d like to make the graph title larger. With ggeasy, I look for easy_ and
scroll until I find what I want. Ah, easy_plot_title_size()! I’ll make that 16-point type. Done.
If I’d like to rotate the x-axis text, I can do that with easy_rotate_x_labels().
And, I can remove the legend title (it’s pretty obvious these are states) with easy_remove_legend_title.
Next, I’d like to look at the negative coronavirus test results, to see if they’re rising at
similar rates to positives. That will help a bit to see how much of this might be related
to more tests. But first let me store this graph in a variable called positives.
Now I’ll run the same code as the first graph, but on the negatives column. It looks
like some of the increase in Massachusetts cases may be because testing ramped up – there
are a lot more negatives, too. But there’s a much bigger rise in positives than negatives
in Louisiana. Although we don’t know if that’s because testing criteria changed
or something else.
It sure would be helpful to see these side by side, though. That’s where the patchwork
package comes in. Let me save this graph in a variable called negatives.
Now look what happens if I load the patchwork package and then type the name of the first
graph plus the name of the second graph. Just like that, I can see side by side graphs!
Pretty useful for some quick – and easy – data exploration.
I can now use ggeasy to remove one of the two legends so they’re aren’t two.
That’s it for this episode, thanks for watching! For more R tips, head to the Do More With
R page at bit-dot-l-y slash do more with R, all lowercase except for the R. You can also
find the Do More With R playlist on the YouTube IDG Tech Talk channel -- where you can subscribe
so you never miss an episode. Hope to see you next time. Stay healthy and safe, everyone!