library(ggplot)
library(magick)
library(beepr)
GIF fanciness!
This bonus VizW(h)iz lesson is a shout-out to R-Ladies Sydney cofounder Lisa Williams. Back in May, she tweeted:
I vote for this as a tutorial at an upcoming @RLadiesSydney meetup! @djnavarro @JenRichmondPhD @StephdeSilva! https://t.co/bBQ4HmbCYW
— Dr Lisa A. Williams (@williamslisaphd) May 16, 2018
Your wish is our command, Lisa! [pun intended!]
I could not possibly love this more! pic.twitter.com/lQyQlRNVZl
— Dr Lisa A. Williams (@williamslisaphd) December 18, 2018
We are quite certain that you’d appreciate knowing how to perform this whizzy wizardry, so here you are: a step-by-step guide to adding a gif to a ggplot!
A step-by-step guide to adding a gif to a ggplot
- Install the
magick
package and load it with ggplot. While you’re at it, install thebeepr
package (you’ll appreciate this at Step 5!).
- Plot your graph and use ggsave to save it as a .png file.
%>%
raintemp na.omit() %>%
filter(beachbugs > 500) %>%
ggplot(aes(x = rain_mm, y = beachbugs, color = temp_airport)) +
geom_point() +
geom_smooth() +
theme_classic() +
scale_colour_distiller(name = "Temp (C)", palette = "RdYlBu") +
labs(title = "Mean enterococci bacteria levels at Eastern Suburbs \nbeaches as a function of rainfall and temperature",
subtitle = "only day > 500",
caption = "data from https://www.environment.nsw.gov.au/beachapp/report_enterococci.aspx",
x = "Rainfall (mm)",
y = "Mean enterococci levels")
ggsave("beaches.png")
- Find a gif you like and save it as a .gif file. Read the image files into Rstudio.
<- image_read("beaches.png")
beachplot <- image_read("ladywiz.gif") wizgif
- Make a composite image that puts the beach plot and wiz gif together. Use the offset to move the gif.
<- image_composite(beachplot, wizgif, offset = "+600+200") frames
- Animate the frames and write to a new gif. It will take ages, so you might like to install the
beepr
package and add a beep to alert you when it is done. The default sounds is a “ping” but if you like mario, try beep(sound = 8).
<- image_animate(frames, fps = 10)
animation image_write(animation, "beachwiz.gif")
beep()
Voilà!
Next up- MarkyMark