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) %>% 
  map(image_read) %>% # reads each path file
  image_join() %>% # joins image
  image_animate(fps=5) %>% # animates, can opt for number of loops
  image_write("HappyHolidays18.gif") # write to current dir

And… that’s it!

Merry Christmas and Happy New Year!

LS0tDQp0aXRsZTogIkhhcHB5IEhvbGlkYXlzISINCmF1dGhvcjogIkRlbml0emEgRC4gVm91dGNoa292YSINCmRhdGU6ICIyMSBEZWMgMjAxOCAoIzEpIg0Kb3V0cHV0OiANCiAgaHRtbF9ub3RlYm9vazogDQogICAgaGlnaGxpZ2h0OiBrYXRlDQotLS0NCg0KX0kgY2FtZSB1cCB3aXRoIGEgd2Vla2x5IF9fUl9fIGNoYWxsZW5nZSwgYXMgYSB3YXkgdG8gImZvcmNlIiBteXNlbGYgaW50byBsZWFybmluZyBuZXcgdGhpbmdzLiBPbmUgZmlndXJlLCBldmVyeSB3ZWVrISBUaGUgY2F0Y2ggaXMgdGhhdCBldmVyeSB0aW1lIEkgc2hvdWxkIGJlIHRyeWluZyBzb21ldGhpbmcgbmV3Li4uIG1heWJlIEknbGwgdXNlICBhIG5ldyB0eXBlIG9mIHZpc3VhbGl6YXRpb24gZm9yIGZpcnN0IHRpbWUsIG9yIG1heWJlIEknbGwgZXhwbG9yZSBhIG5ldyBkYXRhc2V0LiBJbml0aWFsbHkgSSB0aG91Z2h0IGl0IHdvdWxkIGJlIG5lYXQgaWYgdGhlcmUgd2FzIHNvbWUgdW5pZnlpbmcgdG9waWMsIGUuZy4gd2F0ZXIsIGJ1dCB0aGVuIEkgZGVjaWRlZCB0byBrZWVwIGl0IG9wZW4uXw0KDQpUaGlzIGlzIG15IGZpcnN0IGVudHJ5IGZvciB0aGUgd2Vla2x5IFIgY2hhbGxlbmdlISBJIHJlYWxseSB3YW50ZWQgdG8gbWFrZSBpdCBzbm93LCBpZiBub3QgaGVyZSBpbiBTaW5nYXBvcmUsIGF0IGxlYXN0IGluIFIgU3R1ZGlvIQ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