liveBook · Manning

Geom_line() function

As explained in Statistics Slam Dunk

The geom_line() function is a ggplot2 function that adds line segments connecting observations in their data order or a specified grouping.

geom_line() adds line segments connecting observations in their data order or specified grouping. It is especially useful for showing change across seasons, possession time, or another ordered variable. section 13.4 section 12.5

Use geom_line() to:

  • Draw a line through summarized values, such as annual Gini coefficients or average points per possession second. section 13.4 section 8.5.1
  • Combine connected observations with geom_point() to create a connected scatterplot or emphasize individual measurements. section 13.4 section 8.5.1
  • Set group = 1 when the plotted observations should form one continuous line across the x-axis. section 13.4 section 8.5.1

Plot annual inequality

After calculating one Gini coefficient per season, connect the values with: ggplot(gini_summary, aes(x = season_end, y = gc, group = 1)) + geom_line(color = "coral3", size = 1.5) + geom_point(size = 3, color = "coral3"). Add geom_smooth(method = lm, se = FALSE) when a regression trend is useful. section 12.5

ggplot(ws_gini_summary, aes(x = Year, y = gc, group = 1)) + 
 geom_line(aes(y = gc), color = "black", size = .5) + 
 geom_point(size = 5, color = "seagreen3") +
 geom_text(aes(label = gc),
           nudge_x = 0.01, nudge_y = 0.01,
           check_overlap = TRUE, size = 2.5) +
 labs(title = "Gini Coefficient for Win Shares", 
      subtitle = "1991-2017",
      x = "Season", 
      y = "Gini Coeffiicient",
      caption = "includes a maximum top 14 win shares for each team") +
 ylim(0.42, 0.60) +
 theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
 theme(plot.subtitle = element_text(hjust = 0.5)) +
 theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
 annotate("rect", xmin = "1991", xmax = "2009", 
          ymin = 0.42, ymax = 0.60, alpha = 0.1, fill = "orange") +
 annotate("rect", xmin = "2010", xmax = "2017", 
          ymin = 0.42, ymax = 0.60, alpha = 0.1, fill = "blue")

Inspect a complete ggplot2 example connecting annual win-share Gini coefficients, then layering points, labels, annotations, and styling. section 13.4

How Statistics Slam Dunk covers it

How deep it goes
Explains line drawing, grouping, width, and layering, but defers broader trend interpretation to geom_smooth().
Examples
Demonstrates annual salary and win-share Gini coefficients plus Milwaukee scoring and field-goal rates by possession second.
What it assumes
Assumes a summarized data frame, x and y variables, and basic ggplot2 aesthetic mapping.
What you can do after
Apply it via ggplot2 to connect ordered observations and combine the line with points, labels, or regression layers.
How it connects
Connects with ggplot2 aesthetics, geom_point(), geom_smooth(), grouped summaries, and time-series charts.

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.