Skip to content

Instantly share code, notes, and snippets.

@cararthompson
Created June 8, 2026 16:47
Show Gist options
  • Select an option

  • Save cararthompson/b7b145897c49dc34cb3f418b731fb811 to your computer and use it in GitHub Desktop.

Select an option

Save cararthompson/b7b145897c49dc34cb3f418b731fb811 to your computer and use it in GitHub Desktop.
Facetted bars
# Penguin bars
# Make a graph ----
summary_penguins <- penguins |>
dplyr::group_by(species, sex) |>
dplyr::summarise(count = length(bill_len)) |>
dplyr::filter(!is.na(sex))
ggplot(summary_penguins, aes(y = species, x = count, fill = species)) +
geom_bar(stat = "identity") +
facet_grid(~sex) +
labs(title = "Let's count the penguins") +
theme_minimal() +
theme(legend.position = "none", axis.title.y = element_blank())
# Change the colours ----
penguin_colours <- c(
"Adelie" = "#E59608",
"Gentoo" = "#005410",
"Chinstrap" = "#F2B7C4"
)
ggplot(summary_penguins, aes(y = species, x = count, fill = species)) +
geom_bar(stat = "identity") +
facet_grid(~sex) +
labs(title = "Let's count the penguins") +
scale_fill_manual(values = penguin_colours) +
theme_minimal() +
theme(legend.position = "none", axis.title = element_blank())
# Add theme_glasgow() ----
ggplot(summary_penguins, aes(y = species, x = count, fill = species)) +
geom_bar(stat = "identity") +
facet_grid(~sex) +
labs(title = "Let's count the penguins") +
scale_fill_manual(values = penguin_colours) +
theme_glasgow() +
theme(legend.position = "none", axis.title = element_blank())
# Add annotations and declutter a bit ----
ggplot(summary_penguins, aes(y = species, x = count, fill = species)) +
geom_bar(stat = "identity") +
facet_grid(~ toupper(sex)) +
labs(title = "Let's count the penguins") +
scale_fill_manual(values = penguin_colours) +
ggtext::geom_textbox(
aes(
label = paste0(species, "<br>", count),
colour = dplyr::case_when(
species == "Gentoo" ~ "white",
.default = "black"
)
),
hjust = 1,
halign = 1,
fontface = "bold",
family = "Noto Sans",
box.colour = NA,
fill = NA
) +
scale_colour_identity() +
theme_glasgow() +
theme(
legend.position = "none",
axis.title = element_blank(),
axis.text.y = element_blank(),
# We need to specify the panel background again - you could add this to your theme function, but you'll get a panel outline on every graph if you do
panel.background = element_rect(colour = "#343536", fill = "#F6F5FA"),
strip.text = element_text(
colour = "#343536",
face = "bold",
hjust = 0
),
panel.grid = element_blank()
)
# Make interactive ----
# |- Rework the data a bit to give us something to add to the tooltip
summary_penguins <- penguins |>
dplyr::group_by(species, sex) |>
dplyr::mutate(
tooltip_content = paste0(
"Living on<br>- <b>Biscoe</b>: ",
sum(island == "Biscoe"),
"<br>",
"- <b>Torgersen</b>: ",
sum(island == "Torgersen"),
"<br>",
"- <b>Dream</b>: ",
sum(island == "Dream")
)
) |>
dplyr::summarise(
count = length(bill_len),
tooltip_content = unique(tooltip_content)
) |>
dplyr::filter(!is.na(sex))
# |- Build a make a small change to the plot
interactive_plot <- ggplot(
summary_penguins,
aes(y = species, x = count, fill = species)
) +
ggiraph::geom_bar_interactive(
aes(tooltip = tooltip_content, data_id = species),
stat = "identity"
) +
facet_grid(~ toupper(sex)) +
labs(title = "Let's count the penguins") +
scale_fill_manual(values = penguin_colours) +
ggtext::geom_textbox(
aes(
label = paste0(species, "<br>", count),
colour = dplyr::case_when(
species == "Gentoo" ~ "white",
.default = "black"
)
),
hjust = 1,
halign = 1,
fontface = "bold",
family = "Noto Sans",
box.colour = NA,
fill = NA
) +
scale_colour_identity() +
theme_glasgow() +
theme(
legend.position = "none",
axis.title = element_blank(),
axis.text.y = element_blank(),
panel.background = element_rect(colour = "#343536", fill = "#F6F5FA"),
strip.text = element_text(
colour = "#343536",
face = "bold",
hjust = 0
),
panel.grid = element_blank()
)
ggiraph::girafe(
ggobj = interactive_plot,
options = list(
ggiraph::opts_tooltip(css = glasgow_tooltip_css),
ggiraph::opts_hover(css = "stroke:#343536"),
ggiraph::opts_hover_inv(css = "opacity:0.5")
),
height_svg = 6,
width_svg = 9
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment