A plot against the CatterPlots complot

[This article was first published on Maëlle, 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.

In these terrible times, we R people have more important subjects to debate/care about than ggplot2 vs. base R graphics (isn’t even worth discussing anyway, ggplot2 is clearly the best alternative). Or so I thought until I saw CatterPlots trending on Twitter this week and even being featured on Revolutions blog. It was cool because plots with cats are cool, but looking more closely at the syntax of CatterPlots, I couldn’t but realize it was probably a complot to make us all like base R graphics syntax again! So let me show you how to make a cute plot with the awesome ggplot2 extension emojifont.

The plot I’m going to re-make in this post is an old one already that I designed for celebrating my PhD thesis submission in March last year. I decided to compare the gestation time of my thesis, 1246 days, to the gestation time of some common mammals.

Getting data

I’m a bit ashamed because I forgot to write down where I took my data from but I’m pretty sure it was from reading Wikipedia pages and taking a more or less random numbers in the gestation time range indicated for each animals. I also chose animals I could find an emoji for. As you can imagine, it was quite an agreeable data collection. Here is what I end up with:

library("dplyr")
library("forcats")
gestation <- readr::read_csv2("data/2017-02-18-complot.csv")%>%
  arrange(gestation) %>%
  mutate(animal = fct_rev(fct_inorder(animal)))
knitr::kable(gestation)
animal gestation label color
mouse 19 mouse grey
dog 61 dog grey
cat 64 cat grey
wolf 68 wolf grey
tiger 105 tiger grey
pig 112 pig grey
sheep 144 sheep grey
bear 220 bear grey
human 280 baby grey
whale 590 whale grey
elephant 617 elephant grey
my thesis 1246 closed_book gold
levels(gestation$animal)
##  [1] "my thesis" "elephant"  "whale"     "human"     "bear"     
##  [6] "sheep"     "pig"       "tiger"     "wolf"      "cat"      
## [11] "dog"       "mouse"

I used forcats::fct_inorder and forcats::fct_rev because in such a post not using forcats would probably have been a deadly sin (and I don’t have 7 lives), but also because I needed the levels to be ordered from longest to shortest gestation time, otherwise the plot would have looked ugly.

Making the plot

At the time I used gganimate which I still love, but in the meantime I also fell in love with magick in particular since reading Bob’s post. So I decided to re-write my code with magick. On this blog I already published an article featuring both emojifont and gganimate.

library("ggplot2")
library("emojifont")
load.emojifont('OpenSansEmoji.ttf')
library("magick")
plot_one_gestation <- function(gestation_time, gestation){
  now_data <- filter_(gestation, ~ gestation <= gestation_time)
  p <- ggplot(now_data) 
  p <- p + geom_col(aes(x = animal,
               y = gestation,
               fill = color))
  p <- p +
  scale_fill_manual(values = c("grey" = "grey30",
                               "gold" = "darkgoldenrod1")) 
  p <- p + geom_text(aes(x = animal, 
                y = gestation + 45,
                label = emoji(label)),
            family="OpenSansEmoji", size=30) 
  p <- p + theme(axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        text = element_text(size=40),
        legend.position="none")
  p <- p + ggtitle(gestation_time)
  p <- p + scale_x_discrete(limits = levels(gestation$animal))
  p <- p + ylim(c(0, max(gestation$gestation) + 50)) 
  p <- p +  coord_flip() 
  p <- p + xlab("Animal") 
  p <- p + ylab("Gestation in days") 
  
  outfil <- paste0("figs/animals_", gestation_time, ".png")
  ggsave(outfil, p, width=5, height=5)

  outfil
}

I wanted the last image to be repeated 4 times so that everyone might have time to ponder over my accomplishment. I was celebrating and boasting with this graph.

library("purrr")
c(unique(gestation$gestation),
  rep(max(gestation$gestation), 3))%>%
  map(plot_one_gestation, gestation = gestation) %>%
  map(image_read) %>%
  image_join() %>%
  image_animate(fps=1) %>%
  image_write("gestation.gif")

Aren’t these animal emojis adorable? At least as adorable as CatterPlotscats? You can also bring emojis to ggplot2 with emoGG (check out Lucy’s recent post) so you don’t need to ever leave the comfort of ggplot2 for the sake of “c[au]t[e]?ness”.

Disclaimer: I actually starred CatterPlots on Github because I’m an open-minded cat person (married to a cat allergic person!).

To leave a comment for the author, please follow the link and comment on their blog: Maëlle.

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)