Last active
February 21, 2020 06:08
-
-
Save SantoshSrinivas79/58168399703f7a69623c37b7480a09bd to your computer and use it in GitHub Desktop.
daily coding post to demonstrate using ggtext
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
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment