Other sites

Z is for Additional Axes

[This article was first published on Deeply Trivial, 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.

Here we are at the last post in Blogging A to Z! Today, I want to talk about adding additional axes to your ggplot, using the options for fill or color. While these aren’t true z-axes in the geometric sense, I think of them as a third, z, axis.

Some of you may be surprised to learn that fill and color are different, and that you could use one or both in a given plot.

Color refers to the outline of the object (bar, piechart wedge, etc.), while fill refers to the inside of the object. For scatterplots, the default shape doesn’t have a fill, so you’d just use color to change the appearance of those points.

Let’s recreate the pages read over 2019 chart, but this time, I’ll just use fiction books and separate them as either fantasy or other fiction; this divides that dataset pretty evenly in half. Here’s how I’d generate the pages read over time separately by those two genre categories.

This plot uses the default R colorscheme. I could change those colors, using an existing colorscheme, or define my own. Let’s make a fivethirtyeight style figure, using their theme for the overall plot, and their color scheme for the genre variable.

You can also use fill (or color) with the same variable you used for x or y; that is, instead of having it be a third scale, it could add some color and separation to distinguish categories from the x or y variable. This is especially helpful if you have multiple categories being plotted, because it helps break up the wall of bars. If you do this, I’d recommend choosing a color palette with highly complementary colors, rather than highly contrasting ones; you probably also want to drop the legend, though, since the axis will also be labeled.

If you only have a couple categories and want to draw a contrast, that’s when you can use contrasting shades: for instance, at work, when I plot performance on an item, I use red for incorrect and blue for correct, to maximize the contrast between the two performance levels for whatever data I’m presenting.

I hope you enjoyed this series! There’s so much more you can do with tidyverse than what I covered this month. Hopefully this has given you enough to get started and sparked your interest to learn more. Once again, I highly recommend checking out R for Data Science.