...

/

Making Better Figures with ggplot2

Making Better Figures with ggplot2

Learn to make better figures using the ggplot2 package.

Principles of effective figure making

Before we get into the meat of how to most efficiently use the ggplot2 package for plotting, it’s useful to take a moment to talk about what makes a good figure. What is it that makes a nice-looking figure, one that is suitable for publication or use in a presentation? We would argue that there are three hallmarks of a good figure:

  • Judicious use of color
  • Large clear text and labels
  • Efficient usage of plot space

It’s often useful to create multiple panels to show different aspects of our data. These fundamentals are the same whether we’re making our figures in R or not.

There are two main ways to make figures in R: base graphics, that is, those that are built into the base version of R we downloaded from CRAN, and using the ggplot2 package. There are functions in other packages—for example, the scatterplot() function in the car package or the barplot2() function in the gplots package—but all these functions utilize the coding of base graphics. While the ggplot2 package and base graphics use different coding styles, the fundamentals that make an effective graphic remain the same. Both types of coding allow us to build our graphics piece by piece and give us control over every aspect of the figure. We’ll just focus on ggplot2 package here because it’s vastly superior to base graphics.

Let’s talk about some basics that are useful to know.

Defining colors

As with so many things in R, there are multiple ways to define what colors we want to use. We can define colors by number, by name, by RGB, or by the hexadecimal system. Each method has its pros and cons. Here, we’ll use named colors.

Basics of ggplot2

In recent years, the ggplot2 package has become increasingly popular for making nice-looking figures with relative ease. In particular, ggplot2 is excellent for exploratory data analysis because it allows us to easily plot, for example, histograms for each of our groups or side-by-side. As we’ve already seen, we can easily add linear regression lines and confidence intervals to a scatterplot. However, ggplot2 can be used to make pretty much any type of graphic we want. We just have to know how (which is, of course, the same dilemma in base graphics).

Note: This course can’t cover all the use cases related to ggplot2. Instead, it’s meant to give an introduction and provide us with the necessary tools to get started.

Downside of ggplot2

The downside of the ggplot2 package is that it uses a very different syntax from base graphics. The basic ...