Created
May 18, 2021 14:27
-
-
Save farach/76398784cd8cfc99ddefe291705d66fd to your computer and use it in GitHub Desktop.
An example on how to use 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(ggtext) | |
library(magick) | |
library(grid) | |
logo_pp <- image_read("../pp_logo.png") | |
logo_gg <- image_read("../ggtext.png") | |
#imgurl <- system.file("../ggtext.png", package="hexSticker") | |
# sticker(logo_gg, package="hexSticker", p_size=20, s_x=1, s_y=.75, s_width=.6, | |
# filename="../imgfile.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), | |
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.01, | |
just = c("left", "bottom"), | |
width = unit(0.75, "inches") | |
) | |
grid.raster( | |
logo_gg, | |
x = 0.12, | |
y = 0.01, | |
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