Skip to content

Instantly share code, notes, and snippets.

@cararthompson
Last active September 30, 2025 16:34
Show Gist options
  • Select an option

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

Select an option

Save cararthompson/b14db92e5a66960d4518d427a8b64ee8 to your computer and use it in GitHub Desktop.
# 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" = "pink",
"Chinstrap" = "orange",
"Gentoo" = "darkgreen"
)
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_afbi() ----
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_afbi() +
theme(legend.position = "none", axis.title = element_blank())
# Add annotations ----
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 = "Open Sans",
box.colour = NA,
fill = NA
) +
scale_colour_identity() +
theme_afbi() +
theme(
legend.position = "none",
axis.title = element_blank(),
axis.text.y = 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 = "Open Sans",
box.colour = NA,
fill = NA
) +
scale_colour_identity() +
theme_afbi() +
theme(
legend.position = "none",
axis.title = element_blank(),
axis.text.y = element_blank()
)
ggiraph::girafe(
ggobj = interactive_plot,
options = list(
ggiraph::opts_tooltip(css = afbi_tooltip_css),
ggiraph::opts_hover(css = "stroke:#30373b"),
ggiraph::opts_hover_inv(css = "opacity:0.5")
),
# Play around with these dimensions to get the desired aspect ratio
height_svg = 9,
width_svg = 6
)
# Exporting graphs
ggsave(plot = my_plot_name,
filename = "the-path/filename.png", height = 9, width = 6, dpi = 300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment