Chapter 28 ggplot2 Extensions

What You’ll Learn:

  • Extension packages
  • Special plot types
  • Interactive plots
  • Combining plots

Key Errors Covered: 15+ extension errors

Difficulty: ⭐⭐⭐ Advanced

28.1 Introduction

ggplot2 extensions add specialized functionality:

library(ggplot2)
library(dplyr)

28.2 patchwork

💡 Key Insight: Combining Plots

library(patchwork)

p1 <- ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point()
p2 <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()

p1 + p2
p1 / p2

28.3 ggrepel

💡 Key Insight: Better Labels

library(ggrepel)

ggplot(mtcars, aes(x = mpg, y = hp, label = rownames(mtcars))) +
  geom_point() +
  geom_text_repel(max.overlaps = 10)
#> Warning: ggrepel: 4 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

28.4 Summary

Key Takeaways:

  1. patchwork - Combine plots
  2. ggrepel - Non-overlapping labels
  3. Many extensions available

Common Extensions:

library(patchwork)  # Combine plots
library(ggrepel)    # Labels
library(ggthemes)   # Themes
library(plotly)     # Interactive