Using Base R to create Cat images for Day 04-Waffle themed plot for the 2024 30 Day Chart Challenge
The 30 Day Chart Challenge is an annual data visualisation challenge that takes place every April with a daily plot theme that particiants create. Started by Cédric Scherer and Dr. Dominic Royé, The first challenge started in 2021 and has grown from strength to strength with over 5000 followers on social media and many more data visualisation enthusiats sharing their creations. Participants can use any data visualisation tool of their choice and share their designs using the ‘#30DayChartChallenge’ on X(formerly Twitter ), Linkedin and Mastodon.
This year, I wanted to participate to learn new data visualisation catergories, styles and improve my lot generation using R rogramming skills. I decided to use R first before transitioning to Python or any other tool such as D3.js by Observable.
2024 Day 04 -Waffle
Since the start of the challenge, I had been using gglot and other R packages to create the plots, sndaba/2024_30DayChartChallenge. The waffle theme made me think about how I could show the tiles in a waffle differently apart from the usual squares.
1. Install packages
I first installed the tidyverse package which has dplyr, forcats, ggplot and readr packages. I also installed the png package to read the images saved in the folder.
#install necessary packages
install.packages("tidyverse")
install.packages("png")
2. UK Pet Cats dataset
I used the 2023 Tidy Tuesday week 5 dataset on the small home ranges and large local ecological impacts of pet cats in the United Kingdom. I read the dataset from the Tidy Tuesday github repository and saved it in cats_uk_reference variable.
#read the dataset from the Tidy Tuesday github repository
cats_uk_reference <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-01-31/cats_uk_reference.csv')
3. Finding unique hours indoors to show
After going through the dataset, I wanted to see the distinct number of hours cats stay indoors in the hrs_indoors column.
hrs <- unique(cats_uk_reference$hrs_indoors)
4. Using different Cat images
I wanted to find different images of cats that represent the distinct hours instaed of using tiles for the waffle plot. I found five cartoon images from the web, saved them into my directory folder and allocated variables to each of them.
#cat images saved in each variable
catpic = readPNG("cat.png")
grey = readPNG("greyCat.png")
pillow = readPNG("pillow.png")
world = readPNG("world.png")
purple = readPNG("purple.png")
5. Using Base R
Using base R, I created a blank plot (15, 5) to place the images in places of the plot. I also added a title and a caption as the basis for my plot.
## Make a 15 bt 5 blank plot
plot(NULL ,main = "2023 UK average number of hours Cats were indoors per day",
sub="source:2023 Tidy Tuesday Week 05 | Graphic: Simisani Ndaba",
xaxt='n',yaxt='n',bty='n',ylab='',xlab='', xlim=c(0,15), ylim=c(0,5))
6. Using rasterImage
In order to place an image at a certain place, I had to use rasterImage. The rasterImage function uses an image by the variable name, then place the image at (xleft, ybottom, xright, ytop).
I had to make the different cat images show for every distinct hour cats stay indoors, i.e , 12, 7, 17, 2 and 22 hours.
# Add the 12 hours
rasterImage(catpic,0,0,1,1)
rasterImage(catpic,0,1,1,2)
rasterImage(catpic,0,2,1,3)
rasterImage(catpic,0,3,1,4)
rasterImage(catpic,0,4,1,5)
rasterImage(catpic,1,0,2,1)
rasterImage(catpic,1,1,2,2)
rasterImage(catpic,1,2,2,3)
rasterImage(catpic,1,3,2,4)
rasterImage(catpic,1,4,2,5)
rasterImage(catpic,2,0,3,1)
rasterImage(catpic,2,1,3,2)
#add 7 hours
rasterImage(grey,2,2,3,3)
rasterImage(grey,2,3,3,4)
rasterImage(grey,2,4,3,5)
rasterImage(grey,3,0,4,1)
rasterImage(grey,3,1,4,2)
rasterImage(grey,3,2,4,3)
rasterImage(grey,3,3,4,4)
#add 17 hours
rasterImage(pillow,3,4,4,5)
rasterImage(pillow,4,0,5,1)
rasterImage(pillow,4,1,5,2)
rasterImage(pillow,4,2,5,3)
rasterImage(pillow,4,3,5,4)
rasterImage(pillow,4,4,5,5)
rasterImage(pillow,5,0,6,1)
rasterImage(pillow,5,1,6,2)
rasterImage(pillow,5,2,6,3)
rasterImage(pillow,5,3,6,4)
rasterImage(pillow,5,4,6,5)
rasterImage(pillow,6,0,7,1)
rasterImage(pillow,6,1,7,2)
rasterImage(pillow,6,2,7,3)
rasterImage(pillow,6,3,7,4)
rasterImage(pillow,6,4,7,5)
rasterImage(pillow,7,0,8,1)
#add 2 hours
rasterImage(world,7,1,8,2)
rasterImage(world,7,2,8,3)
#add 22 hours
rasterImage(purple,7,3,8,4)
rasterImage(purple,7,4,8,5)
rasterImage(purple,8,0,9,1)
rasterImage(purple,8,1,9,2)
rasterImage(purple,8,2,9,3)
rasterImage(purple,8,3,9,4)
rasterImage(purple,8,4,9,5)
rasterImage(purple,9,0,10,1)
rasterImage(purple,9,1,10,2)
rasterImage(purple,9,2,10,3)
rasterImage(purple,9,3,10,4)
rasterImage(purple,9,4,10,5)
rasterImage(purple,10,0,11,1)
rasterImage(purple,10,1,11,2)
rasterImage(purple,10,2,11,3)
rasterImage(purple,10,3,11,4)
rasterImage(purple,10,4,11,5)
rasterImage(purple,11,0,12,1)
rasterImage(purple,11,1,12,2)
rasterImage(purple,11,2,12,3)
rasterImage(purple,11,3,12,4)
rasterImage(purple,11,4,12,5)
7. Customized Legend
The legend was place within the far left in the plot with the different cat images representing the hours. The text annotation only used the x and y position as seen below.
#annotation
text(15,4.5,"12.5")
rasterImage(catpic,13,4,14,5)
text(15,3.4,"7.5")
rasterImage(grey,13,3,14,4)
text(15,2.5,"17")
rasterImage(pillow,13,2,14,3)
text(15,1.5,"2")
rasterImage(world,13,1,14,2)
text(15,0.5,"22")
rasterImage(purple,13,0,14,1)
I thought using Baser R would be difficult, on the contrary, Base R creates beautiful looking plots. Merging Base R plots with ggplots is also possible but I would rather use one to create a plot at a time for consistency.
The contents of external submissions are not necessarily reflective of the opinions or work of Maven Analytics or any of its team members.
We believe in fostering lifelong learning and our intent is to provide a platform for the data community to share their work and seek feedback from the Maven Analytics data fam.
Submit your own writing here if you'd like to become a contributor.
Happy learning!
-Team Maven