liveBook · Manning

Geom_segment() function

As explained in Statistics Slam Dunk

The ggplot2 geom_segment() function adds line segments between coordinates specified by x, y, xend, and yend aesthetics.

geom_segment() adds straight line segments between coordinates supplied through x, y, xend, and yend. In ggplot2, those aesthetics let you connect a starting position to an ending position for each row of data. section 10.4.1

Use the function to turn differences or ranges into visible stems:

  • Create lollipop charts by drawing from y = 0 to each group’s mean payroll, then placing a geom_point() marker at the endpoint. section 10.4.3
  • Compare standardized payrolls and wins by keeping x and xend equal to Team, then connecting y = zSalaries to yend = zWins. section 11.4.2
  • Add directional annotations with arrow = arrow(...), using a short segment to point toward an important part of a chart. section 10.4.1

Comparing payroll investment with wins

A Cleveland dot plot can show two values for every NBA team. The segment connects each team’s standardized payroll to its standardized wins: ascending stems indicate wins exceed salary investment, while descending stems indicate the reverse. ```r ggplot(final) + geom_segment(aes(x = Team, xend = Team, y = zSalaries, yend = zWins), color = "grey50") + geom_point(aes(x = Team, y = zSalaries), color = "springgreen3") + geom_point(aes(x = Team, y = zWins), color = "darkred") ``` section 11.4.2

p1 <- ggplot(final) +
 geom_segment(aes(x = Team, xend = Team, 
                  y = zSalaries, yend = zWins), color = "grey50") +
 geom_point(aes(x = Team, y = zSalaries), color = "springgreen3", 
            size = 3) +
 geom_point(aes(x = Team, y = zWins), color = "darkred", size = 3) +
 labs(title = "Inflation-Adjusted Payrolls vs. Regular Season Wins",
      subtitle = "2000-17", 
      x = "", 
      y = "Standard Deviations", 
      caption = "green/light = salaries\nred/dark = wins") + 
 theme(plot.title = element_text(face = "bold")) +
 theme(axis.text.x = element_text(angle = 45, hjust = 1))
print(p1)

Browse the complete Cleveland dot plot code, including the segment layer, two endpoint layers, labels, caption, and angled team names. section 11.4.2

How Statistics Slam Dunk covers it

How deep it goes
Explains x, y, xend, and yend through complete chart examples, but does not develop the geom's broader parameter set.
Examples
Uses NBA payroll lollipops, year-over-year correlation annotations, and team-level standardized payroll-versus-wins comparisons.
What it assumes
Assumes basic ggplot2 layering and prepared numeric or categorical variables, while demonstrating the coordinate mapping directly.
What you can do after
Build it from scratch by mapping starting and ending coordinates to a ggplot2 segment layer.
How it connects
Connects geom_segment() with ggplot2 aesthetics, geom_point(), geom_line(), annotations, standardization, and Cleveland dot plots.

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.