{ggtext} for images as x-axis labels

[This article was first published on rstats on Irregularly Scheduled Programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’ve written a few times about using an image as an x-axis label, and the solutions have been slowly improving. This one blows all of them out of the water.

Claus Wilke (@ClausWilke) now has a {ggtext} package which can very neatly add images as x-axis labels!

This makes the solution as simple as

library(ggplot2)
library(ggtext)
library(rvest)

## GDP per capita, top 11 countries
url      <- "https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)_per_capita"
html     <- xml2::read_html(url)
gdppc    <- html_table(html_nodes(html, "table")[3])[[1]][1:11,]

## clean up; remove non-ASCII and perform type conversions
gdppc$Country <- gsub("Â ", "", gdppc$Country)
gdppc$Country <- iconv(gdppc$Country, "latin1", "ASCII", sub="")
gdppc$Country[9] <- "United States of America"
gdppc$Rank    <- iconv(gdppc$Rank,    "latin1", "ASCII", sub="")
gdppc$`US$`   <- as.integer(sub(",", "", gdppc$`US$`))

## switching to a different source of flag images
labels <- setNames(
  paste0("<img src='http://www.senojflags.com/images/country-flag-icons/", 
         gsub(" ", "-", gdppc$Country), "-Flag.png'  width='30' /><br>", 
         sapply(strwrap(gdppc$Country, width = 10, simplify = FALSE), 
                function(x) paste(x, collapse = "<br>"))),
  gdppc$Country
)

## create a dummy dataset
npoints <- length(gdppc$Country)
y       <- gdppc$`US$`
x       <- gdppc$Country
dat     <- data.frame(x=factor(x, levels=gdppc$Country), y=y)

## NB: #85bb65 is the color of money in the USA apparently.
gg <- ggplot(dat, aes(x=x, y=y/1e3L, group=1)) 
gg <- gg + geom_bar(col="black", fill="#85bb65", stat="identity")
gg <- gg + scale_x_discrete(name = NULL, labels = labels)
gg <- gg + theme_minimal()
gg <- gg + scale_fill_discrete(guide=FALSE)
gg <- gg + theme(plot.background = element_rect(fill="grey90"))
gg <- gg + labs(title="GDP per capita", 
                subtitle="Top 11 countries", 
                x="", y="$US/1000", 
                caption=paste0("Source: ",url))
## ggtext::element_markdown
gg <- gg + theme(axis.text.x  = element_markdown(color = "black", size = 7), 
                 axis.text.y  = element_text(size=14),
                 axis.title.x = element_blank())
gg
{ggtext} makes this easy!

Figure 1: {ggtext} makes this easy!

This is an even nicer solution than I’ve been using, not only because it’s shorter to use, but also more flexible - markdown can be processed (using element_markdown) wherever element_text is used, such as axis text, legends, titles, etc…

I think this finally closes this chapter, but now it’s time to make some really cool graphs with images.


devtools::session_info()

## ─ Session info ──────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 3.5.2 (2018-12-20)
##  os       Pop!_OS 19.04               
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language en_AU:en                    
##  collate  en_AU.UTF-8                 
##  ctype    en_AU.UTF-8                 
##  tz       Australia/Adelaide          
##  date     2019-08-13                  
## 
## ─ Packages ──────────────────────────────────────────────────────────────
##  package     * version   date       lib
##  assertthat    0.2.1     2019-03-21 [1]
##  backports     1.1.4     2019-04-10 [1]
##  bitops        1.0-6     2013-08-17 [1]
##  blogdown      0.14.1    2019-08-11 [1]
##  bookdown      0.12      2019-07-11 [1]
##  callr         3.3.1     2019-07-18 [1]
##  cli           1.1.0     2019-03-19 [1]
##  colorspace    1.4-1     2019-03-18 [1]
##  crayon        1.3.4     2017-09-16 [1]
##  curl          4.0       2019-07-22 [1]
##  desc          1.2.0     2018-05-01 [1]
##  devtools      2.1.0     2019-07-06 [1]
##  digest        0.6.20    2019-07-04 [1]
##  dplyr         0.8.3     2019-07-04 [1]
##  evaluate      0.14      2019-05-28 [1]
##  fs            1.3.1     2019-05-06 [1]
##  ggplot2     * 3.2.1     2019-08-10 [1]
##  ggtext      * 0.1.0     2019-08-13 [1]
##  glue          1.3.1     2019-03-12 [1]
##  gridtext      0.1.0     2019-08-13 [1]
##  gtable        0.3.0     2019-03-25 [1]
##  highr         0.8       2019-03-20 [1]
##  htmltools     0.3.6     2017-04-28 [1]
##  httr          1.4.1     2019-08-05 [1]
##  knitr         1.24      2019-08-08 [1]
##  labeling      0.3       2014-08-23 [1]
##  lazyeval      0.2.2     2019-03-15 [1]
##  magrittr      1.5       2014-11-22 [1]
##  markdown      1.1       2019-08-07 [1]
##  memoise       1.1.0     2017-04-21 [1]
##  munsell       0.5.0     2018-06-12 [1]
##  pillar        1.4.2     2019-06-29 [1]
##  pkgbuild      1.0.4     2019-08-05 [1]
##  pkgconfig     2.0.2     2018-08-16 [1]
##  pkgload       1.0.2     2018-10-29 [1]
##  png           0.1-7     2013-12-03 [1]
##  prettyunits   1.0.2     2015-07-13 [1]
##  processx      3.4.1     2019-07-18 [1]
##  ps            1.3.0     2018-12-21 [1]
##  purrr         0.3.2     2019-03-15 [1]
##  R6            2.4.0     2019-02-14 [1]
##  Rcpp          1.0.2     2019-07-25 [1]
##  RCurl         1.95-4.12 2019-03-04 [1]
##  remotes       2.1.0     2019-06-24 [1]
##  rlang         0.4.0     2019-06-25 [1]
##  rmarkdown     1.14      2019-07-12 [1]
##  rprojroot     1.3-2     2018-01-03 [1]
##  rvest       * 0.3.4     2019-05-15 [1]
##  scales        1.0.0     2018-08-09 [1]
##  selectr       0.4-1     2018-04-06 [1]
##  sessioninfo   1.1.1     2018-11-05 [1]
##  stringi       1.4.3     2019-03-12 [1]
##  stringr       1.4.0     2019-02-10 [1]
##  testthat      2.2.1     2019-07-25 [1]
##  tibble        2.1.3     2019-06-06 [1]
##  tidyselect    0.2.5     2018-10-11 [1]
##  usethis       1.5.1     2019-07-04 [1]
##  withr         2.1.2     2018-03-15 [1]
##  xfun          0.8       2019-06-25 [1]
##  xml2        * 1.2.2     2019-08-09 [1]
##  yaml          2.2.0     2018-07-25 [1]
##  source                              
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  Github (rstudio/blogdown@be4e91c)   
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  Github (clauswilke/ggtext@5c7cfa9)  
##  CRAN (R 3.5.2)                      
##  Github (clauswilke/gridtext@21b7198)
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.2)                      
##  CRAN (R 3.5.1)                      
## 
## [1] /home/jono/R/x86_64-pc-linux-gnu-library/3.5
## [2] /usr/local/lib/R/site-library
## [3] /usr/lib/R/site-library
## [4] /usr/lib/R/library


To leave a comment for the author, please follow the link and comment on their blog: rstats on Irregularly Scheduled Programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)