Creating a cracked egg plot using {ggplot2} in R
Inspired by a Visual Capitalist chart, this blog post will show you how to utilise spatial data R packages in a slightly unusual way to create an infographic in the shape of a cracked egg in R.
May 4, 2023
You can’t make an R plot without breaking eggs… that’s how the saying goes, right? A recent #TidyTuesday data set explored US egg production and, thanks to an excellent suggestion from Tanya Shapiro to create a plot in the style of the Visual Capitalist US egg prices chart, and Deepali Kank’s incredible version of it, I decided to figure out how to crack an egg in R.
Making the egg
First things first, we need to make an egg! The equation of an egg, looks a bit like this:
We can then get the $x$ and $y$ coordinates of the outline using:
We can do this in R using the following code:
|
|
Let’s check it looks egg-shaped by plotting it using geom_path()
from {ggplot2}:
|
|
It’s squashed, but you can still tell it looks like an egg shape. Changing to using coord_fixed()
should help with how squashed at looks, and you can see what different values of a
, and b
do in the different versions below:
|
|
The one on the left is the most egg-shaped of the values I tried so I’ll stick with a = 1
and b = 3
. Ideally, I’d also like it to be upside down, with the smaller pointy end at the top, and the easiest way to do that is to multiply the $y$ coordinate by -1
:
|
|
There are other ways to flip the axis, including by changing the scales in {ggplot2}, but this approach will make the next steps a little bit easier.
Transforming the egg
Now we need to decide where the egg shape will be cracked into two. There’s a couple of things we need to think about here:
- creating a line chart that shows our data
- transforming the data such that the egg and the line chart can overlap
Let’s start with the line chart, and read in the data:
|
|
And then calculate the average production per quarter for cage-free organic eggs, and change the units to millions. I also decided to keep the information about quarters, as I’ll use this for annotations later on:
|
|
I tried different levels of aggregation besides quarterly, including monthly and annually but decided that quarterly looked the most cracked-egg-like. We can plot the aggregated data as a line chart using geom_line()
:
|
|
Now let’s plot the egg and our line chart together:
|
|
You can see that technically they’re both on the same plot but the line is very squashed in the top right corner. We need to transform the coordinates of either the egg or the line to make them intersect. We can add an constant and a multiplier to the x and y co-ordinates of the egg data, as well as changing the value of k
to rescale the egg if need be:
|
|
and let’s plot the egg and line again:
|
|
This involved a lot of trial and error to pick the values I was happy with. You can see the process in this making of gif recorded with the {camcorder} package:
Cracking the egg
Now comes the slightly tricky part: we need to crack the egg. There are two things we want here:
- to retain only the bottom part of the cracked egg.
- to be able to colour in the egg
For this, we’re going to use two different packages: {sf} and {lwgeom}. These packages were probably designed for making maps and doing spatial analysis, but it turns out they’re also good for (graphical) egg-making purposes.
The first thing I’m going to do, is convert the line and the egg into spatial objects. The egg will be converted to a polygon, and the line will be converted to a linestring. I’ve applied a further transformation to the y-axis here, to making plotting easier later on (since specific aspect ratios are enforced when we plot spatial data). I probably should have re-factored my initial transformation of the x and y coordinates earlier, but I didn’t for reasons of pure laziness.
|
|
Finally, we can split the egg into two pieces using the st_split()
function from {lwgeom}. This function returns a list of polygons created by an intersecting line. Here, we get two polygons: the first for the bottom part of the egg, and the second for the top part of the egg.
|
|
We only want to keep the bottom part of the egg, so we can simply plot the first polygon using geom_sf()
:
|
|
We can add some points and annotations onto the line at the top if we want to make the data easier to read:
|
|
And with a little bit of styling using the theme()
function, we have a cracked egg!
Show code: colour and theme
|
|
It’s not identical to the Visual Capitalist version, but you should now have all the ingredients you need to crack an egg with R!
A word of caution
Although this was a fun exercise in figuring out how to make this type of chart from a technical perspective, I don’t think it’s an easy to interpret infographic for a couple of reasons. I didn’t include an x or y axis here (although the Visual Capitalist original does), so it’s hard to figure out where the zero line is. It’s not clear which element of the chart is representing the data: the height of the line above the bottom of the egg? the area of the egg shape? If I simply wanted to clearly communicate the data, the line chart alone probably would have sufficed.
You can view the complete code I used to create this visualisation on GitHub.
For attribution, please cite this work as:
Creating a cracked egg plot using {ggplot2} in R.
Nicola Rennie. May 4, 2023.
nrennie.rbind.io/blog/cracked-egg-plot-ggplot2
BibLaTeX Citation
@online{rennie2023, author = {Nicola Rennie}, title = {Creating a cracked egg plot using {ggplot2} in R}, date = {2023-05-04}, url = {https://nrennie.rbind.io/blog/cracked-egg-plot-ggplot2} }
Licence: creativecommons.org/licenses/by/4.0