-
-
Save cararthompson/3810baf179e001bb046cbc4b85036422 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggplot2) | |
| # Our starting point ---- | |
| beak_lengths <- penguins |> | |
| ggplot() + | |
| geom_jitter( | |
| aes( | |
| x = bill_len, | |
| y = species, | |
| fill = species, | |
| size = body_mass | |
| ), | |
| shape = 21, | |
| width = 0, | |
| height = 0.15 | |
| ) + | |
| labs( | |
| title = "Beak lengths in the dataset", | |
| subtitle = "The adelie penguins all have beaks shorter than 50mm - you'll be safe with them", | |
| x = "Beak length (mm)" | |
| ) + | |
| theme_minimal(base_size = 16) + | |
| theme(legend.position = "none", axis.title.y = element_blank()) | |
| # Change the colours ---- | |
| penguin_colours <- | |
| c("Adelie" = "#F4A305", "Gentoo" = "#00670E", "Chinstrap" = "#F2BCC8") | |
| beak_lengths + | |
| scale_fill_manual(values = penguin_colours) | |
| # Add `theme_abfi()` ---- | |
| beak_lengths + | |
| scale_fill_manual(values = penguin_colours) + | |
| theme_afbi() + | |
| theme(legend.position = "none", axis.title.y = element_blank()) | |
| # Add annotations ---- | |
| beak_means_df <- penguins |> | |
| dplyr::group_by(species) |> | |
| dplyr::summarise(mean_length = mean(bill_len, na.rm = TRUE)) | |
| penguins |> | |
| ggplot() + | |
| geom_segment( | |
| data = beak_means_df, | |
| aes(x = mean_length, xend = mean_length, y = -Inf, yend = species), | |
| linetype = 3 | |
| ) + | |
| geom_jitter( | |
| aes( | |
| x = bill_len, | |
| y = species, | |
| fill = species, | |
| size = body_mass | |
| ), | |
| shape = 21, | |
| width = 0, | |
| height = 0.15 | |
| ) + | |
| ggtext::geom_textbox( | |
| data = beak_means_df, | |
| aes( | |
| x = mean_length, | |
| y = species, | |
| label = paste0( | |
| species, | |
| " mean<br>**", | |
| janitor::round_half_up(mean_length), | |
| "mm**" | |
| ) | |
| ), | |
| hjust = 0, | |
| nudge_y = -0.4, | |
| box.colour = NA, | |
| family = "Work Sans", | |
| colour = "#1A242F", | |
| fill = NA | |
| ) + | |
| scale_fill_manual(values = penguin_colours) + | |
| labs( | |
| title = "Beak lengths in the dataset", | |
| subtitle = "The adelie penguins all have beaks shorter than 50mm - you'll be safe with them", | |
| x = "Beak length (mm)" | |
| ) + | |
| theme_afbi() + | |
| theme(legend.position = "none", axis.title.y = element_blank()) | |
| # Make interactive ---- | |
| interactive_plot <- penguins |> | |
| ggplot() + | |
| geom_segment( | |
| data = beak_means_df, | |
| aes(x = mean_length, xend = mean_length, y = -Inf, yend = species), | |
| linetype = 3 | |
| ) + | |
| ggiraph::geom_jitter_interactive( | |
| aes( | |
| x = bill_len, | |
| y = species, | |
| fill = species, | |
| size = body_mass, | |
| tooltip = paste(bill_len, "mm<br>A ", sex, " penguin from ", island) | |
| ), | |
| shape = 21, | |
| width = 0, | |
| height = 0.15 | |
| ) + | |
| ggtext::geom_textbox( | |
| data = beak_means_df, | |
| aes( | |
| x = mean_length, | |
| y = species, | |
| label = paste0( | |
| species, | |
| " mean<br>**", | |
| janitor::round_half_up(mean_length), | |
| "mm**" | |
| ) | |
| ), | |
| hjust = 0, | |
| nudge_y = -0.4, | |
| box.colour = NA, | |
| family = "Work Sans", | |
| colour = "#1A242F", | |
| fill = NA | |
| ) + | |
| scale_fill_manual(values = penguin_colours) + | |
| labs( | |
| title = "Beak lengths in the dataset", | |
| subtitle = "The adelie penguins all have beaks shorter than 50mm - you'll be safe with them", | |
| x = "Beak length (mm)" | |
| ) + | |
| theme_afbi() + | |
| theme( | |
| legend.position = "none", | |
| axis.title = element_blank(), | |
| axis.text = 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