liveBook · Manning

Ggplot2

As explained in Statistics Slam Dunk

ggplot2 is an R graphics library for constructing statistical visualizations from data using a layered grammar of graphics.

ggplot2 is an R graphics package for turning data into professional-looking statistical visualizations. Build a plot in layers: start with ggplot() and a data source, map variables with aes(), add a geometric object, then refine labels, scales, themes, annotations, or faceting with further function calls joined by +. section 2.5.3 section 1.3

Use visualizations to explore a wrangled data set before deeper testing or modeling. Histograms, boxplots, correlation heat maps, bar charts, and facet plots can reveal distributions, group differences, and relationships that computations alone may not communicate as clearly. section 2.7 section 1.2.1

A typical build assigns a clear job to each layer:

  • Initialize the plot with ggplot(data, aes(...)), supplying the data and axis or visual mappings. section 2.5.3
  • Choose a geom such as geom_histogram(), geom_boxplot(), geom_tile(), geom_line(), geom_point(), or geom_text() to determine what is drawn. section 2.5.3 section 13.4
  • Clarify the result with labs(), scales, themes, faceting, summaries, or annotations. section 2.5.3 section 13.4

Explore career win shares

Map career win shares to the x-axis, add geom_histogram() with eight blue bins, then use labs() and theme() to label and emphasize the result. The resulting histogram shows a right-skewed distribution, providing a quick view of how career outcomes are concentrated. section 2.5.3 section 2.5.3

p1 <- ggplot(draft, aes(x = WS)) + 
 geom_histogram(fill = "royalblue3", color = "royalblue3", 
                bins = 8) + 
 labs(title = "Career Win Shares Distribution of
      NBA First-Round Selections",
      subtitle = "2000-09 NBA Drafts",
      x = "Career Win Shares",
      y = "Frequency") +
 theme(plot.title = element_text(face = "bold"))
print(p1)

This executable histogram shows the foundation, geometric layer, labels, and theme combined into one ggplot2 object. section 2.5.3

ggplot2 also supports specialized analytical displays. A correlation matrix can be reshaped and drawn as a heat map with geom_tile(), color scales, and overlaid coefficient labels; grouped summaries can become connected line charts with points, text, and annotations. section 2.5.4 section 13.4

How Statistics Slam Dunk covers it

How deep it goes
Explains layered plot construction through functions, mappings, geoms, labels, themes, scales, faceting, and annotations.
Examples
Demonstrates automobile plots, NBA win-share histograms and boxplots, a correlation heat map, and a Gini-coefficient line chart.
What it assumes
Introduces plot-building functions while relying on prepared data and R package loading.
What you can do after
Apply it via ggplot2 functions to build and customize statistical visualizations from data.
How it connects
Connects with exploratory data analysis, dplyr, tidyr, correlation, histograms, boxplots, facet plots, and Gini coefficients.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.