How to use emoji in ggplot2 scatter plot

Yuanxuan Yang
5 min readMar 14, 2021

--

This is a tutorial of using emoji (or other images) in ggplot2 scatter plot. Our task is to create a map (scatter plot) to show the “most popular vegetable by state in US”, where each state is represented by vegetable emoji. Here is a sneak peek of the final figure. Let’s get started!

Use emoji for scatter plot in ggplot2

1. Get the data

The most popular vegetable data is obtained from https://www.thedailymeal.com/eat/popular-vegetable-us-states, I have tidied up and stored the information in “data/state-fav-veg.csv” in my github repository.

Now let’s have a look at our data records.

## name vegetable 
## 1 Alabama Broccoli
## 2 Alaska Tomato
## 3 Arizona Broccoli
## 4 Arkansas Carrot
## 5 California Broccoli
## 6 Colorado Broccoli

Each record in the “state_veg” contains the name and most popular vegetable of a state in the US.

The next thing to do is to get the coordinates/location of each state. So I followed the design of Katie Park’s work in this twitter post, and the coordinates are stored in the “data/state-location-grid.csv”.

## code name x y 
## 1 AL Alabama 7 2
## 2 AK Alaska 1 8
## 3 AZ Arizona 2 3
## 4 AR Arkansas 5 3
## 5 CA California 1 4
## 6 CO Colorado 3 4

Each record contains the code (Two-Letter Abbreviations), name and x, y coordinate of a state in the US. we can have a quick look at their location:

US state map — grid location

We then join the state_veg and state_location, and make a basic scatter plot using ggpplot2. The dot’s color represent different vegetable types.

Most Popular Vegetable by State in US (normal scatter plot with ggplot2)

2. Emoji Time

The scatter plot above Looks good so far, but why not replace the boring dots with some emoji vegetables? Let’s do it!

Broccoli emoji

I have tried different ways of using emoji in ggplot, although there are approaches like emojifont andemoGG, they tend to use the flat version of emoji. As a Apple Fanboy (not really), i prefer the classic apple version a bit more. The ggimage::geom_image is another potential solution to add img into ggplot. However, I am not into its tricky setup for img aspect ratio.

So today I will go for an alternative and still very simple method — using the geom_richtext provided in ggtext package. This approach is inspired by Emil Hvitfeldt’s brilliant blog post.

2.1 Get the emoji

You can find and download all emoji from https://emojipedia.org/. Once downloaded, we can create a dataframe to store the links/address (at your local machine) to them, as shown below

## vegetable emoji_link 
## 1 Broccoli img/broccoli_1f966.png
## 2 Tomato img/tomato_1f345.png
## 3 Carrot img/carrot_1f955.png
## 4 Cucumber img/cucumber_1f952.png
## 5 Corn img/ear-of-corn_1f33d.png

2.2 Join the dataframe

We then need to join state_veg and the emoji_pic data by the common variable of “vegetable”, as a result, each record will contain the string of link to the emoji.

Once joined, our data is finally ready, let’s have a look

## name vegetable code x y emoji_link 
## 1 Alabama Broccoli AL 7 2 img/broccoli_1f966.png
## 2 Alaska Tomato AK 1 8 img/tomato_1f345.png
## 3 Arizona Broccoli AZ 2 3 img/broccoli_1f966.png
## 4 Arkansas Carrot AR 5 3 img/carrot_1f955.png
## 5 California Broccoli CA 1 4 img/broccoli_1f966.png
## 6 Colorado Broccoli CO 3 4 img/broccoli_1f966.png

2.3 Define a function to provide more infomation for your emoji images

We will use geom_richtext to draw the data point in a scatter chart, and replace the point shape with emoji. geom_richtext draws labels similar to ggplot::geom_label, but formatted using basic markdown/html. Therefore, it is possible to embed images in the richtext labels. Here we are going to define a function so that the emoji can be added and formatted as a resource in markdown/html.

2.4 Moment of Truth

Now let’s try to replace the geom_point with geom_richtext using emooooji

Hurray, it works! So we can make some further tweaks, add a vintage-paper look background color, change the font style, add title and data source info. Hope you like this imporved version!

3. Final note

Making this map does not mean that I agree with everything presented in it. First of all, we could argue all day about “Is a Tomato a Fruit or Vegetable?”

Here is the answer from Google

A benefit of the presented method (using the geom_richtext), is that we can use any images we want for plotting the dots, while emojifont and emoGG packages can only provide us the choice of emoji. So I replaced the corn with an copyright-free image of popcorn. :)

--

--