I came up with a weekly R challenge, as a way to “force” myself into learning new things. One figure, every week! The catch is that every time I should be trying something new… maybe I’ll use a new type of visualization for first time, or maybe I’ll explore a new dataset. Initially I thought it would be neat if there was some unifying topic, e.g. water, but then I decided to keep it open.

This is my first entry for the weekly R challenge! I really wanted to make it snow, if not here in Singapore, at least in R Studio!

How did I do it?

1. generated 1000 x and y coordinates (for my snowflakes)
2. generated some noise, because I wanted the snowflakes to jiggle a bit, but that wasn’t very successful at the end (I’ll try again another time)
1. made a plot to see how are my random snowflakes distributed (not a big surprise there). I used R Base, not ggplot2. Colors: grey for the background, ‘white’ for the snowflakes, darkgreen for the text. I was a bit lazy, so I used pch=8 instead of Unicode snowflake symbol.

1. made 50 png files where every next png file has some random snowflakes falling, whiles others are either not falling or falling very slowly. Initially I wanted them to jiggle and fall with random speed, but I think this is a good compromise. It’s not perfect, but it will do.
#set working directory
setwd('C:/Users/voutc/Documents/Personal/R_Blogs/HappyHolidays')
# function for naming/numbering the files (to start with 0), from https://davetang.org/muse/2015/02/12/animated-plots-using-r/
rename <- function(x){
if (x < 10) {
return(name <- paste('000',i,'plot.png',sep=''))
}
if (x < 100 && i >= 10) {
return(name <- paste('00',i,'plot.png', sep=''))
}
if (x >= 100) {
return(name <- paste('0', i,'plot.png', sep=''))
}
}
# producing the 50 png files
frames <- 50 #number of frames
name <- c()

for(i in 1:frames){
name <- rename(i)
png(name)
par(bg= "grey", mai=c(0,0,0,0))
plot(x, y, type="n", xaxt="n", yaxt="n", xlab="", ylab="", bty="n")
points(x+xn, y+0.1*i*xn, pch=8, col="white")
text(0.5, 0.9, "Happy Holidays!", font=2, cex=4, col="darkgreen", family="B")
text(0.93, 0, "@DenitzaV", font=3,  col="darkgreen", family="B")
dev.off()
}
1. loaded the png files, so I can make a gif. I used the magickv.2.0 package and image_animate(fps=5) and then saved the gif.
# code from https://ryanpeek.github.io/2016-10-19-animated-gif_maps_in_R/
list.files(path = "C:/Users/voutc/Documents/Personal/R_Blogs/HappyHolidays", pattern = "*.png", full.names = T) %>%
image_write("HappyHolidays18.gif") # write to current dir