A plot against the CatterPlots complot

February 17, 2017

(This article was first published on Maëlle, and kindly contributed to R-bloggers)

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:

gestation <- readr::read_csv2("data/2017-02-18-complot.csv")%>%
  arrange(gestation) %>%
  mutate(animal = fct_rev(fct_inorder(animal)))
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
##  [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.

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(),
        text = element_text(size=40),
  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)


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.

  rep(max(gestation$gestation), 3))%>%
  map(plot_one_gestation, gestation = gestation) %>%
  map(image_read) %>%
  image_join() %>%
  image_animate(fps=1) %>%

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)