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
