A Solid Start to ggplot2

Author

Joyce Robbins

Published

November 2, 2024

Preface

This tutorial is designed for R users with no experience with ggplot2 or those who have tried to learn but got stuck along the way. It is not designed to be a comprehensive guide but rather to build a solid foundation. It differs from other resources in the following ways:

  • Theory We begin with the grammar of graphics, the philosophical approach upon which ggplot2 is based.

  • Data We focus on data types and data shape. From my experience a good portion of ggplot2 problems are caused by data having the wrong type or wrong shape rather than the wrong ggplot2 code.

  • Essentials We focus on the elements that matter most for creating effective graphs: the data layers, scales, and faceting. We pay minimal attention to theme (non-data) elements such as tweaking the size and positions of labels and the like. As such, this is not a comprehensive guide. If you have a basic understanding of ggplot2 and wish to learn how to do something specific, the following are great resources: ggplot2: Elegant Graphics for Data Analysis (3e) or R Graphics Cookbook, 2nd edition

  • Good graphs

  • Errors

Setup

As this is not an introduction to R, you probably already have R installed. If you haven’t updated R recently, say within the last year, download and install the appropriate version for your operating system from The Comprehensive R Archive Network. If you use RStudio check for updates by clicking “Help” “Check for Updates” from within the application. Finally, update or install the tidyverse packages by running install.packages("tidyverse"). For all three – R, RStudio, tidyverse – do not ignore the advice to update!

The basics

ggplot2 is based on a grammar of graphics (the “gg” in ggplot2) which makes it different from graphics programs that are based on chart types. Think of the old style lego kits that give you building blocks with which you can make whatever you want. To make the most of the package, it’s very helpful to think of it in these terms. Rather than think in terms of names of charts, think in terms of the graphical elements that are needed to create that type of chart. With knowledge of how to put together the basic elements you will be able to create anything you want.

This is a Quarto book.

To learn more about Quarto books visit https://quarto.org/docs/books.