library(tidyverse)#> ── Attaching core tidyverse packages ───────────────────── tidyverse 2.0.0 ──#> ✔ dplyr 1.1.0.9000 ✔ readr 2.1.4#> ✔ forcats 1.0.0 ✔ stringr 1.5.0#> ✔ ggplot2 3.4.1 ✔ tibble 3.1.8#> ✔ lubridate 1.9.2 ✔ tidyr 1.3.0#> ✔ purrr 1.0.1#> ── Conflicts ─────────────────────────────────────── tidyverse_conflicts() ──#> ✖ dplyr::filter() masks stats::filter()#> ✖ dplyr::lag() masks stats::lag()
Chapter 1. Data Visualization
Introduction
“The simple graph has brought more information to the data analyst’s mind than any other device.” —John Tukey
R has several systems for making graphs, but ggplot2 is one of the most elegant and most versatile. ggplot2 implements the grammar of graphics, a coherent system for describing and building graphs. With ggplot2, you can do more faster by learning one system and applying it in many places.
This chapter will teach you how to visualize your data using ggplot2. We will start by creating a simple scatterplot and use it to introduce aesthetic mappings and geometric objects—the fundamental building blocks of ggplot2. We will then walk you through visualizing distributions of single variables as well as visualizing relationships between two or more variables. We’ll finish off with saving your plots and troubleshooting tips.
Prerequisites
This chapter focuses on ggplot2, one of the core packages in the tidyverse. To access the datasets, help pages, and functions used in this chapter, load the tidyverse by running:
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access