Skip to content

Instantly share code, notes, and snippets.

@AlbertRapp
Created December 31, 2022 19:22
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertRapp/c0dc4b98cdced2574dac226f2d00a3af to your computer and use it in GitHub Desktop.
Save AlbertRapp/c0dc4b98cdced2574dac226f2d00a3af to your computer and use it in GitHub Desktop.
Lollipop chart comparison with bar chart
library(tidyverse)
manufacturers <- mpg |>
count(manufacturer, sort = TRUE) |>
mutate(
manufacturer = str_to_title(manufacturer),
manufacturer = fct_reorder(manufacturer, n)
)
## Bar plot
manufacturers |>
ggplot(aes(y = manufacturer, x = n)) +
geom_col(fill = 'dodgerblue4') +
theme_minimal() +
scale_x_continuous(
expand = expansion(mult = c(0, 0.05))
) +
labs(
x = element_blank(),
y = element_blank(),
title = 'Number of cars in the {mpg} data set'
) +
theme(
panel.grid.major.y = element_blank()
)
## Lollipop chart
manufacturers |>
ggplot(aes(y = manufacturer, x = n)) +
geom_point(col = 'dodgerblue4', size = 5) +
geom_segment(
aes(x = 0, xend = n, y = manufacturer, yend = manufacturer),
linewidth = 1.5,
col = 'dodgerblue4'
) +
theme_minimal() +
scale_x_continuous(
expand = expansion(mult = c(0, 0.05))
) +
labs(
x = element_blank(),
y = element_blank(),
title = 'Number of cars in the {mpg} data set'
) +
theme(
panel.grid.major.y = element_blank()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment