Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
For some reason, using other than the default in plots has been a major problem for me in R.
Supposedly, one can use the extra
package to manage all of that but I found it too cumbersome.
Instead, I found out that the showtext
package can make my life easier.
Even though working with text in plot is not yet completely free of troubles, showtext
has made many things easier.
Now, I can finally choose s freely and even use icons.
This blogposts gives you a how-to so that you can do that too.
Import and Use Fonts with showtext
A great source for s is Google’s page. What is great abut this page is that it can display texts in many different s.
Once we found a nice , we can use its name to make it available within R.
This is done with showtext
’s helpful _add_google()
function.
Let’s import a couple of random s.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Packages that we will use in this post setwd(here::here('content/en/post/2022-03-04-s-and-icons/')) library(tidyverse) library(showtext) library(ggtext) library(gghighlight) # Import s # First argument = google name, # Secont name = name in R _add_google('Lora', 'lora') _add_google('Lobster', 'lobster') _add_google('Anton', 'anton') _add_google('Fira Sans', 'firasans') _add_google('Syne Mono', 'syne') # Important step to enable showtext rendering! showtext_auto() |
Notice that we have also used showtext_auto()
.
This is necessary for showtext
to take over the show.
Otherwise, the new s would not be usable.
Now, let’s take a look at our new s.
1 2 3 4 5 6 7 8 9 10 11 12 |
tib <- tibble( family = c('firasans', 'lora', 'lobster', 'anton', 'syne'), x = 0, y = seq(0.0, 1, length.out = 5), label = 'Showtext shows text. Wow. What an insight.' ) tib %>% ggplot(aes(x, y, label = label)) + geom_text(family = tib$family, size = 13, hjust = 0, col = 'dodgerblue4') + coord_cartesian(xlim = c(0, 1), ylim = c(0, 1)) + theme_void() |
You may wonder why we have used coord_cartesian()
here.
We did this in order to ensure that the x-axis is not centered at 0 and our example texts won’t be outside of the plot.
Personally, I find this somewhat tedious but this can’t be helped, I guess.
With text elements we always run at the risk of writing outside of the plot area.
Next, let’s make our use of s somewhat more practical. In my last blog post, I stressed the use of highlighting a few important things instead of using many colors. Combine this with direct labels instead of a legend and you get this plot I created using the Fira Sans .
Now, see what it would look like had I used the Lobster instead.
Feels different doesn’t it? And this is still different than the Anton .
Import and Use Icon Fonts with showtext
We can not only use regular text s but also icons with showtext
.
For example, we may want to use one of the free Fontawesome icons.
To do so, download the newest version and extract the .otf-files into your working directory.
These contain the information that you need.
Importing these (and any other for that matter) works with _add()
and the path to the .otf-files.
1 2 3 4 5 |
# First argument = name in R # Second argument = path to .otf-file _add('fa-reg', 's/Font Awesome 6 Free-Regular-400.otf') _add('fa-brands', 's/Font Awesome 6 Brands-Regular-400.otf') _add('fa-solid', 's/Font Awesome 6 Free-Solid-900.otf') |
Now that we imported the s, we can use ggtext
’s geom_richtext()
and some HTML wizardry to add icons to our previously imported s from Google.
But first, what we need is an icon’s unicode identifier?
Uni-what?
The easiest way to find that is to stroll through the Fontawesome icons online. Then, find one that matches the you want to use, e.g. free and solid. Finally, find it’s unicode character in the corresponding popup menu.
Once you got this, you can add &#x
in front of the unicode and wrap <span>
tags around it.
Within these tags, you will have to specify -family
so that the icon is rendered.
1 2 3 4 5 6 7 8 9 10 11 12 |
tib <- tibble( family = c('firasans', 'lora', 'lobster', 'anton', 'syne'), x = 0, y = seq(0.0, 1, length.out = 5), label = "Let's talk cash <span style='-family:fa-solid'></span>" ) tib %>% ggplot(aes(x, y, label = label)) + geom_richtext(family = tib$family, size = 16, hjust = 0, col = 'dodgerblue4', label.colour = NA) + coord_cartesian(xlim = c(0, 1), ylim = c(-0.1, 1.1)) + theme_void() |
This way, you can also use icons in scatter plots.
Though, make sure to set fill=NA
if you do not want to have white boxes around the icons.
1 2 3 4 |
tibble(x = runif(25), y = runif(25)) %>% ggplot(aes(x, y, label = "<span style='-family:fa-solid;'></span>")) + geom_richtext(size = 12, label.colour = NA, fill = NA, col = 'dodgerblue4',) + theme_minimal() |
You will notice that using the two previous code chunks will generate a lot of warnings about “native encoding”. So far, I have always been able to ignore these without any trouble. I really don’t know why they appear. And if you know, please let me know in the comments below.
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.