You've successfully subscribed to MyPad Blog
Great! Next, complete checkout for full access to MyPad Blog
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Using ggtext to spruce up your plots!

Using ggtext to spruce up your plots!

Quick daily coding post. Follow this thread for details: https://twitter.com/geokaramanis/status/1230549592103456769

I watched this awesome video last night before bed: Spruce up your ggplot2 visualizations with formatted text? - Claus Wilke

Here is a quick demo to enhance your plots! ;-)

# daily coding post to demonstrate using ggtext

# credit: late night watch of [Spruce up your ggplot2 visualizations with formatted text? - Claus Wilke](https://resources.rstudio.com/rstudio-conf-2020/spruce-up-your-ggplot2-visualizations-with-formatted-text-claus-wilke)

# Inspired by:
# https://gist.github.com/gkaramanis/a18438d624e495784af38932650c6abe
# https://twitter.com/geokaramanis/status/1230549592103456769?s=20

# remotes::install_github("wilkelab/ggtext")

library(ggtext)
library(ggplot2)

library(extrafont)
font_import() # Import all fonts
fonts() # Print list of all fonts

# original by https://twitter.com/geokaramanis

ggplot(data = data.frame(x = c(-4, 6)), aes(x)) +
  stat_function(fun = dnorm, n = 101, args = list(mean = 0, sd = 1), geom = "area", fill = "#F5BB87", alpha = 0.9) +
  stat_function(fun = dnorm, n = 101, args = list(mean = 2.7, sd = 1), geom = "area", fill = "#BBD4EB", alpha = 0.9) +
  coord_fixed(xlim = c(-5, 7), ratio = 8, clip = "off") +
  annotate("segment", x = -3.9, y = 0, xend = 6, yend = 0, color = "grey60", size = 1.3) +
  annotate("segment", x = -3.5, y = -0.04, xend = -3.5, yend = 0.42, color = "grey60", size = 1.3) +
  annotate("text", x = 0, y = 0.18, label = "what people\nactually do\nin R", family = "Arial Bold", color = "white", size = 5.2) +
  annotate("text", x = 2.75, y = 0.18, label = "what people\nassume others\ndo in R", family = "Arial Bold", color = "white", size = 5.2) +
  annotate("text", x = -1.25, y = -0.03, label = "← less complicated", family = "Arial Bold", color = "grey60", size = 5.2) + 
  annotate("text", x = 3.25, y = -0.03, label = "more complicated →", family = "Arial Bold", color = "grey60", size = 5.2) + 
  theme_void() +
  ggsave("~/Desktop/normal.png", width = 10, height = 4)

# butchered by https://twitter.com/TweetSKS

# credit: https://github.com/wilkelab/ggtext/issues/8
ggplot(data = data.frame(x = c(-4, 6)), aes(x)) +
  stat_function(fun = dnorm, n = 101, args = list(mean = 0, sd = 1), geom = "area", fill = "#F5BB87", alpha = 0.9) +
  stat_function(fun = dnorm, n = 101, args = list(mean = 2.7, sd = 1), geom = "area", fill = "#BBD4EB", alpha = 0.9) +
  coord_fixed(xlim = c(-5, 7), ratio = 8, clip = "off") +
  annotate("segment", x = -3.9, y = 0, xend = 6, yend = 0, color = "grey60", size = 1.3) +
  annotate("segment", x = -3.5, y = -0.04, xend = -3.5, yend = 0.42, color = "grey60", size = 1.3) +
  annotate(geom='richtext', x=0, y=0.18, label = "what people <br><span style='color:red'>actually do</span><br>in R.", fill = NA, label.color = NA, , size = 10, vjust = 1, family = "Impact") +
  annotate(geom='richtext', x=2.75, y=0.18, label = "what people <br><span style='color:blue'>assume others do</span><br>in R.", fill = NA, label.color = NA, size = 10, vjust = 1, family = "Impact") +
  annotate("text", x = -1.25, y = -0.03, label = "← less complicated", family = "Arial Bold", color = "grey60", size = 5.2) + 
  annotate("text", x = 3.25, y = -0.03, label = "more complicated →", family = "Arial Bold", color = "grey60", size = 5.2) + 
  theme_void() +
  ggsave("~/Desktop/normal.png", width = 15, height = 10)