Create an animated clock in R with ggplot2 (and ffmpeg)
[This article was first published on Zero Intelligence Agents » R, 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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Because it’s Friday—and I needed to create this for a separate visualization—here is how to create an animated clock in R using ggplot2
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate digitial clock face | |
first.nine <- c('00', '01', '02', '03', '04', '05', '06', '07', '08', '09') | |
hours <- c(first.nine, as.character(seq(10,23,1))) | |
mins <- c(first.nine, as.character(seq(10,59,1))) | |
time.chars.l <- lapply(hours, function(h) paste(h, ':', mins, sep='')) | |
time.chars <- do.call(c, time.chars.l) | |
# Generate analog clock face | |
hour.pos <- seq(0, 12, 12/(12*60))[1:720] | |
min.pos <-seq(0,12, 12/60)[1:60] | |
all.hours <- rep(hour.pos, 2) | |
all.times <- cbind(all.hours, min.pos, 24) | |
for(i in 1:nrow(all.times)) { | |
cur.time <- data.frame(list(times=c(all.times[i,1], all.times[i,2]), hands=c(.5, 1))) | |
clock <- ggplot(cur.time, aes(xmin=times, xmax=times+0.03, ymin=0, ymax=hands))+ | |
geom_rect(aes(alpha=0.5))+ | |
scale_x_continuous(limits=c(0,all.hours[length(all.hours)]), breaks=0:11, | |
labels=c(12, 1:11))+ | |
scale_y_continuous(limits=c(0,1.1))+scale_alpha(legend=FALSE)+theme_bw()+ | |
coord_polar()+ | |
opts(axis.text.y=theme_blank(), axis.ticks=theme_blank(), | |
panel.grid.major=theme_blank(), | |
strip.background = theme_rect(colour = 'white'), | |
title=time.chars[i]) | |
# Save images to a folder names 'clocks' | |
ggsave(plot=clock, filename=paste('clocks/', i, '.png', sep=''), height=5, width=5) | |
} | |
# To create animation, use ffmpeg | |
# ffmpeg -f image2 -r 15 -i clocks/%d.png -b 800k ggplot2_clock.mp4 |
In just about 20 lines of code! And here is the clock…
I think this is a nifty way to show time elapse, rather than the www.drewconway.com/zia/?p=2741″ target=”_blank”>windowed timelines I had used previously. Enjoy.
P.S. I will be officially off the grid starting now until mid-September. See you on the other side!
To leave a comment for the author, please follow the link and comment on their blog: Zero Intelligence Agents » R.
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.