SlideShare una empresa de Scribd logo
1 de 33
Spruce up your ggplot2 visualizations
with formatted text
Claus O. Wilke
The University of Texas at Austin
https://wilkelab.org/ggtext
@clauswilke clauswilke
A common problem:
Mixing italics and normal text in ggplot
https://stackoverflow.com/questions/39282293/r-ggplot2-using-italics-and-non-italics-in-the-same-category-label
bactname OTUname name value
Staphylococcaceae OTU 1 Staphylococcaceae (OTU 1) -0.5
Moraxella OTU 2 Moraxella (OTU 2) 0.5
Streptococcus OTU 3 Streptococcus (OTU 3) 2.0
Acinetobacter OTU 4 Acinetobacter (OTU 4) 3.0
bactname OTUname name value
Staphylococcaceae OTU 1 Staphylococcaceae (OTU 1) -0.5
Moraxella OTU 2 Moraxella (OTU 2) 0.5
Streptococcus OTU 3 Streptococcus (OTU 3) 2.0
Acinetobacter OTU 4 Acinetobacter (OTU 4) 3.0
data %>%
ggplot(aes(name, value)) +
geom_col() + coord_flip()
bactname OTUname name value
Staphylococcaceae OTU 1 Staphylococcaceae (OTU 1) -0.5
Moraxella OTU 2 Moraxella (OTU 2) 0.5
Streptococcus OTU 3 Streptococcus (OTU 3) 2.0
Acinetobacter OTU 4 Acinetobacter (OTU 4) 3.0
data %>%
ggplot(aes(name, value)) +
geom_col() + coord_flip()
https://stackoverflow.com/questions/39282293/r-ggplot2-using-italics-and-non-italics-in-the-same-category-label
How should we specify formatted text?
data %>%
ggplot(aes(name, value)) +
geom_col() + coord_flip() +
ggtitle("species names are *in italics*")
data %>%
ggplot(aes(name, value)) +
geom_col() + coord_flip() +
ggtitle("species names are *in italics*")
library(ggtext) # remotes::install_github('wilkelab/ggtext')
data %>%
ggplot(aes(name, value)) +
geom_col() + coord_flip() +
ggtitle("species names are *in italics*") +
theme(plot.title = element_markdown())
library(ggtext) # remotes::install_github('wilkelab/ggtext')
data %>% mutate(
name = glue("*{bactname}* ({OTUname})") # library(glue)
)
data %>% mutate(
name = glue("*{bactname}* ({OTUname})") # library(glue)
)
bactname OTUname name value
Staphylococcaceae OTU 1 *Staphylococcaceae* (OTU 1) -0.5
Moraxella OTU 2 *Moraxella* (OTU 2) 0.5
Streptococcus OTU 3 *Streptococcus* (OTU 3) 2.0
Acinetobacter OTU 4 *Acinetobacter* (OTU 4) 3.0
data %>% mutate(
name = glue("*{bactname}* ({OTUname})")
) %>%
ggplot(aes(name, value)) +
geom_col() + coord_flip() +
theme(axis.text.y = element_markdown())
Your plots are boring.
Don’t you ever use color?
data %>% mutate(
color = c("#009E73", "#D55E00", "#0072B2", "#000000"),
name = glue("<i style='color:{color}'>{bactname}</i> ({OTUname})")
) %>%
data %>% mutate(
color = c("#009E73", "#D55E00", "#0072B2", "#000000"),
name = glue("<i style='color:{color}'>{bactname}</i> ({OTUname})")
) %>%
ggplot(aes(name, value, fill = color)) +
geom_col(alpha = 0.5) + coord_flip() +
scale_fill_identity() +
theme(axis.text.y = element_markdown())
What else can you do?
https://stackoverflow.com/questions/14070953/photo-
alignment-with-graph-in-r/30765590#30765590
labels <- c(
Interphase = "<img src='img/interphase.jpg' width='50' /><br>Interphase",
Prophase = "<img src='img/prophase.jpg' width='50' /><br>Prophase",
Metaphase = "<img src='img/metaphase.jpg' width='50' /><br>Metaphase",
Anaphase = "<img src='img/anaphase.jpg' width='50' /><br>Anaphase",
Telophase = "<img src='img/telophase.jpg' width='50' /><br>Telophase"
)
https://stackoverflow.com/questions/14070953/photo-
alignment-with-graph-in-r/30765590#30765590
https://stackoverflow.com/questions/14070953/photo-
alignment-with-graph-in-r/30765590#30765590
labels <- c(
Interphase = "<img src='img/interphase.jpg' width='50' /><br>Interphase",
...
)
ggplot(data, aes(phase, value, fill = cat)) +
geom_col(position = "dodge") +
scale_x_discrete(name = NULL, labels = labels) +
theme(axis.text.x = element_markdown(lineheight = 1.2))
https://stackoverflow.com/questions/14070953/photo-
alignment-with-graph-in-r/30765590#30765590
labels <- c(
Interphase = "<img src='img/interphase.jpg' width='50' /><br>Interphase",
...
)
ggplot(data, aes(phase, value, fill = cat)) +
geom_col(position = "dodge") +
scale_x_discrete(name = NULL, labels = labels) +
theme(axis.text.x = element_markdown(lineheight = 1.2))
Text boxes, backgrounds, and word-wrapping
https://wilkelab.org/ggtext
Does this only work for theme elements?
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() + geom_smooth(method = "lm") +
facet_wrap(~Species)
iris_cor <- iris %>%
group_by(Species) %>%
summarize(r_square = cor(Sepal.Length, Sepal.Width)^2)
iris_cor
#> # A tibble: 3 x 2
#> Species r_square
#> <fct> <dbl>
#> 1 setosa 0.551
#> 2 versicolor 0.277
#> 3 virginica 0.209
iris_cor <- iris %>%
group_by(Species) %>%
summarize(r_square = cor(Sepal.Length, Sepal.Width)^2) %>%
mutate(
Sepal.Length = 8,
Sepal.Width = 4.5,
label = glue("*r*<sup>2</sup> = {round(r_square, 2)}")
)
iris_cor
#> # A tibble: 3 x 5
#> Species r_square Sepal.Length Sepal.Width label
#> <fct> <dbl> <dbl> <dbl> <glue>
#> 1 setosa 0.551 8 4.5 *r*<sup>2</sup> = 0.55
#> 2 versicolor 0.277 8 4.5 *r*<sup>2</sup> = 0.28
#> 3 virginica 0.209 8 4.5 *r*<sup>2</sup> = 0.21
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() + geom_smooth(method = "lm") +
geom_richtext(
data = iris_cor, aes(label = label),
hjust = 1, vjust = 1
) +
facet_wrap(~Species)
The package that makes it all possible
text <- "Some text **in bold.**"
gp <- grid::gpar(
fontfamily = "Comic Sans MS", fontsize = 20, col = "blue"
)
g1 <- grid::textGrob(text, 0.1, 0.7, hjust = 0, gp = gp)
grid::grid.draw(g1)
text <- "Some text **in bold.**"
gp <- grid::gpar(
fontfamily = "Comic Sans MS", fontsize = 20, col = "blue"
)
g1 <- grid::textGrob(text, 0.1, 0.7, hjust = 0, gp = gp)
g2 <- gridtext::richtext_grob(text, 0.1, 0.4, hjust = 0, gp = gp)
grid::grid.draw(g1)
grid::grid.draw(g2)
https://wilkelab.org/ggtext
https://wilkelab.org/gridtext
@clauswilke
clauswilke

Más contenido relacionado

Similar a Spruce up your ggplot2 visualizations with formatted text

Tests in vitro de l'action inhibitrice de la croissance de P. falciparum par ...
Tests in vitro de l'action inhibitrice de la croissance de P. falciparum par ...Tests in vitro de l'action inhibitrice de la croissance de P. falciparum par ...
Tests in vitro de l'action inhibitrice de la croissance de P. falciparum par ...Institut Pasteur de Madagascar
 
Open Access In Medicine
Open Access In MedicineOpen Access In Medicine
Open Access In MedicineMartin Fenner
 
Mobile Number Portability - The Process Factory
Mobile Number Portability - The Process FactoryMobile Number Portability - The Process Factory
Mobile Number Portability - The Process FactoryBSP Media Group
 
ICSE-DAPSE 2013 talk
ICSE-DAPSE 2013 talkICSE-DAPSE 2013 talk
ICSE-DAPSE 2013 talkOlga Baysal
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出すTakashi Kitano
 
Introducing Security Access Control Policies into Legacy Business Processes
Introducing Security Access Control Policies into Legacy Business ProcessesIntroducing Security Access Control Policies into Legacy Business Processes
Introducing Security Access Control Policies into Legacy Business ProcessesSébastien Mosser
 
01_introduction_lab.pdf
01_introduction_lab.pdf01_introduction_lab.pdf
01_introduction_lab.pdfzehiwot hone
 
Assessing the impact of transposable element variation on mouse phenotypes an...
Assessing the impact of transposable element variation on mouse phenotypes an...Assessing the impact of transposable element variation on mouse phenotypes an...
Assessing the impact of transposable element variation on mouse phenotypes an...Thomas Keane
 
Benchmarking Perl (Chicago UniForum 2006)
Benchmarking Perl (Chicago UniForum 2006)Benchmarking Perl (Chicago UniForum 2006)
Benchmarking Perl (Chicago UniForum 2006)brian d foy
 
An Exploration of the Formal Properties of PromQL
An Exploration of the Formal Properties of PromQLAn Exploration of the Formal Properties of PromQL
An Exploration of the Formal Properties of PromQLBrian Brazil
 
Data Science for Folks Without (or With!) a Ph.D.
Data Science for Folks Without (or With!) a Ph.D.Data Science for Folks Without (or With!) a Ph.D.
Data Science for Folks Without (or With!) a Ph.D.Douglas Starnes
 
Science Fiction Sensor Networks
Science Fiction Sensor NetworksScience Fiction Sensor Networks
Science Fiction Sensor NetworksDiego Pizzocaro
 
名古屋Ruby会議01 - Rubyでライフハッキング10連発♪
名古屋Ruby会議01 - Rubyでライフハッキング10連発♪名古屋Ruby会議01 - Rubyでライフハッキング10連発♪
名古屋Ruby会議01 - Rubyでライフハッキング10連発♪Shoya Tsukada
 
Connectix webserver
Connectix webserverConnectix webserver
Connectix webserversteveheer
 
Connectix webserver
Connectix webserverConnectix webserver
Connectix webserversteveheer
 
Carnet des innovations 20 fev 2012
Carnet des innovations 20 fev 2012Carnet des innovations 20 fev 2012
Carnet des innovations 20 fev 2012DFIE Lyon
 

Similar a Spruce up your ggplot2 visualizations with formatted text (20)

Tests in vitro de l'action inhibitrice de la croissance de P. falciparum par ...
Tests in vitro de l'action inhibitrice de la croissance de P. falciparum par ...Tests in vitro de l'action inhibitrice de la croissance de P. falciparum par ...
Tests in vitro de l'action inhibitrice de la croissance de P. falciparum par ...
 
Open Access In Medicine
Open Access In MedicineOpen Access In Medicine
Open Access In Medicine
 
Into Clojure
Into ClojureInto Clojure
Into Clojure
 
Mobile Number Portability - The Process Factory
Mobile Number Portability - The Process FactoryMobile Number Portability - The Process Factory
Mobile Number Portability - The Process Factory
 
Ruby things
Ruby thingsRuby things
Ruby things
 
ICSE-DAPSE 2013 talk
ICSE-DAPSE 2013 talkICSE-DAPSE 2013 talk
ICSE-DAPSE 2013 talk
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出す
 
Introducing Security Access Control Policies into Legacy Business Processes
Introducing Security Access Control Policies into Legacy Business ProcessesIntroducing Security Access Control Policies into Legacy Business Processes
Introducing Security Access Control Policies into Legacy Business Processes
 
R programming language
R programming languageR programming language
R programming language
 
01_introduction_lab.pdf
01_introduction_lab.pdf01_introduction_lab.pdf
01_introduction_lab.pdf
 
Assessing the impact of transposable element variation on mouse phenotypes an...
Assessing the impact of transposable element variation on mouse phenotypes an...Assessing the impact of transposable element variation on mouse phenotypes an...
Assessing the impact of transposable element variation on mouse phenotypes an...
 
XML for bioinformatics
XML for bioinformaticsXML for bioinformatics
XML for bioinformatics
 
Benchmarking Perl (Chicago UniForum 2006)
Benchmarking Perl (Chicago UniForum 2006)Benchmarking Perl (Chicago UniForum 2006)
Benchmarking Perl (Chicago UniForum 2006)
 
An Exploration of the Formal Properties of PromQL
An Exploration of the Formal Properties of PromQLAn Exploration of the Formal Properties of PromQL
An Exploration of the Formal Properties of PromQL
 
Data Science for Folks Without (or With!) a Ph.D.
Data Science for Folks Without (or With!) a Ph.D.Data Science for Folks Without (or With!) a Ph.D.
Data Science for Folks Without (or With!) a Ph.D.
 
Science Fiction Sensor Networks
Science Fiction Sensor NetworksScience Fiction Sensor Networks
Science Fiction Sensor Networks
 
名古屋Ruby会議01 - Rubyでライフハッキング10連発♪
名古屋Ruby会議01 - Rubyでライフハッキング10連発♪名古屋Ruby会議01 - Rubyでライフハッキング10連発♪
名古屋Ruby会議01 - Rubyでライフハッキング10連発♪
 
Connectix webserver
Connectix webserverConnectix webserver
Connectix webserver
 
Connectix webserver
Connectix webserverConnectix webserver
Connectix webserver
 
Carnet des innovations 20 fev 2012
Carnet des innovations 20 fev 2012Carnet des innovations 20 fev 2012
Carnet des innovations 20 fev 2012
 

Último

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 

Último (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 

Spruce up your ggplot2 visualizations with formatted text