5  Using ggplot2

5.1 Overview

ggplot2 is the primary visualization package in R. It is based on the grammar of graphics, a system for building plots by combining components.

ggplot(data, aes(...)) +
  geom_*()
  • data: dataset
  • aes(): maps variables to visual properties
  • geom_*(): defines how data are drawn
  • +: adds layers

5.2 Basic Scatterplot

library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point()

5.3 Encode Additional Information

ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
  geom_point()

5.4 Aesthetic Mapping vs Fixed Values

NoteKey Rule

Map variables inside aes(). Set constants outside aes().

ggplot(mtcars, aes(wt, mpg)) +
  geom_point(color = "blue")

5.5 Add Layers

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE)
`geom_smooth()` using formula = 'y ~ x'

5.6 Color Best Practices

TipUsing Color Effectively
  • Use color to highlight important differences
  • Avoid overly bright or distracting palettes
  • Ensure sufficient contrast with the background
  • Consider color blindness (use accessible palettes)
# Better color palette
ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
  geom_point(size = 2) +
  scale_color_brewer(palette = "Set1")

# Continuous color scale
ggplot(mtcars, aes(wt, mpg, color = mpg)) +
  geom_point(size = 2) +
  scale_color_viridis_c()

5.7 Improve Labels and Readability

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    title = "Fuel Efficiency vs Weight",
    x = "Weight",
    y = "Miles per Gallon"
  )
`geom_smooth()` using formula = 'y ~ x'

5.8 Step 7: Themes

Themes control the overall appearance of your plot.

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  theme_minimal()

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  theme_classic()

5.8.1 Customizing Themes

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    axis.title = element_text(size = 12)
  )

5.9 Step 8: Compare Groups with Faceting

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)

5.10 Common Geometries

ggplot(mtcars, aes(wt, mpg)) + geom_point()

ggplot(mtcars, aes(factor(cyl))) + geom_bar()

ggplot(mtcars, aes(mpg)) + geom_histogram(bins = 10)

ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()

5.11 Workflow

TipRecommended Approach
  1. Start simple
  2. Confirm it answers your question
  3. Add layers
  4. Improve clarity and aesthetics

5.12 Summary

  • Build plots with ggplot(data, aes(...))
  • Add layers with +
  • Map variables inside aes()
  • Use color intentionally
  • Use themes to improve readability

5.13 Exercise

Starting with the following plot, do the following:

  • Color by am
  • Add a smooth line
  • Apply a different theme
  • Use a color palette
ggplot(mtcars, aes(wt, mpg)) +
  geom_point()