Created
May 18, 2021 20:51
-
-
Save farach/5acf1ec55980c1d11ae62c4c7e5f6911 to your computer and use it in GitHub Desktop.
Example of the ggtext package
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(palmerpenguins) | |
| library(tidyverse) | |
| library(magick) | |
| library(grid) | |
| library(ggtext) | |
| logo_pp <- image_read("../pp_logo.png") | |
| logo_gg <- image_read("../ggtext.png") | |
| p <- | |
| penguins %>% | |
| filter_all(~ !is.na(.)) %>% | |
| ggplot(aes(bill_depth_mm, bill_length_mm)) + | |
| geom_point(aes(color = species), alpha = 0.8, size = 3) + | |
| scale_color_manual( | |
| name = NULL, | |
| values = c(Adelie = "#6495ED", Gentoo = "#6E8B3D", Chinstrap = "#FF7F00"), | |
| labels = c( | |
| Adelie = "<i style='color:#6495ED'>Adelie</i>", | |
| Gentoo = "<i style='color:#6E8B3D'>Gentoo</i>", | |
| Chinstrap = "<i style='color:#FF7F00'>Chinstrap</i>" | |
| ) | |
| ) + | |
| theme_afs() + # can use theme_minimal() | |
| theme( | |
| legend.position = "none", | |
| axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)), | |
| plot.title = element_markdown( | |
| lineheight = 1.1, | |
| margin = margin(t = 0, r = 0, b = 20, l = 0) | |
| ), | |
| legend.text = element_markdown(size = 11) | |
| ) + | |
| labs( | |
| y = "Bill length", | |
| x = "Bill depth", | |
| title = "**This is an example of the *ggtext* package**<br> | |
| <span style='font-size:12pt'>Here we are using it to change the color of the subtitle in order to remove<br> | |
| the legend and have more screen real estate: | |
| <span style='color:#6495ED;'>Adelie</span>, | |
| <span style='color:#6E8B3D;'>Gentoo</span>, and | |
| <span style='color:#FF7F00;'>Chinstrap</span> penguins</span>", | |
| caption = "Source:\nhttps://allisonhorst.github.io/palmerpenguins/\nhttps://wilkelab.org/ggtext/index.html" | |
| ) | |
| p | |
| grid.raster( | |
| logo_pp, | |
| x = 0.02, | |
| y = 0.05, | |
| just = c("left", "bottom"), | |
| width = unit(0.75, "inches") | |
| ) | |
| grid.raster( | |
| logo_gg, | |
| x = 0.1, | |
| y = 0.08, | |
| just = c("left", "bottom"), | |
| width = unit(0.75, "inches") | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment