Is it wine o’clock?

[This article was first published on Maëlle, 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.

Emojis were again quite en vogue this week on Twitter with Romain François doing some awesome stuff for the emo package, in particular this teeny tiny animated clock. It reminded me of my own emoji animated clock that I had done a while ago for representing time-use data. Time for me to present its genesis!

I’m actually not a Quantified Self person, but at my work time-use data was collected for an epidemiology project: information about people activities and locations throughout one day can help unraveling sources of exposure to air pollution. I’ve therefore spent some time thinking about how to represent such data. In particular, my colleague Margaux directed a fantastic video about our project. We introduced some real data from our project in it, including an animated clock that I made with emoji-coding of indoor-outdoor location. I’ll present code and data for producing a similar clock with my own agenda on some Wednesday evenings of last year.

Loading the time-use data

I logged data from my Wednesday in a rather ok format.

date <- "2016-03-10"
activities <- readr::read_csv2("data/2017-08-12-wineoclock-timeuse.csv",
                               col_types = "ccc") %>%
  dplyr::mutate(start = lubridate::ymd_hms(paste0(date, start, ":00")),
         end = lubridate::ymd_hms(paste0(date, end, ":00")))
2016-03-10 00:00:002016-03-10 06:30:00sleeping
2016-03-10 06:30:002016-03-10 07:00:00coffee
2016-03-10 07:00:002016-03-10 08:00:00running
2016-03-10 08:00:002016-03-10 09:30:00train
2016-03-10 09:30:002016-03-10 17:00:00computer
2016-03-10 17:00:002016-03-10 18:00:00train
2016-03-10 18:00:002016-03-10 20:00:00school
2016-03-10 20:00:002016-03-10 20:30:00pizza
2016-03-10 20:30:002016-03-10 22:00:00dancer
2016-03-10 22:00:002016-03-10 22:30:00wine_glass
2016-03-10 22:30:002016-03-10 23:59:00sleeping

When I prepared this visualization ggimage didn’t exist so I knew I’d have to use emojis from emojifont to represent my activities, therefore I directly entered the activities as emojis. My Wednesdays at that time were quite varied, I started the day with breakfast (how original), then some time at the gym, followed by a rather short workday due to my taking a Catalan class at the end of the afternoon. The evening was spent enjoying a quick and cheap pizza dinner with my husband before our salsa class and sometimes treating ourselves to a wine glass at the small and rather shabby-looking bar at the end of our street, whose name really was Small bar.

Making the animated clock

I didn’t have to think about how to draw and animated a clock with ggplot2 because somebody already had: I used code from this gist of Drew Conway’s.

# 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 <-, 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]
hour_pos <- rep(hour_pos, 2)
all_times <- dplyr::tbl_df(cbind(hour_pos, min_pos)) %>%
  dplyr::mutate(index = time_chars) %>%
  dplyr::mutate(time = lubridate::ymd_hms(paste0(date, index, ":00"))) 

I then needed to join the all_times table, containing many snapshots of the time, with my time-use table made of intervals (start and end of each activity). Another package born in the meantime, fuzzyjoin, would have allowed me to write more efficient code, and I was so sad when looking at the old code that I re-wrote it.

all_times <- dplyr::mutate(all_times, start = time, end = time)
all_times  <- fuzzyjoin::interval_left_join(all_times, activities)
0.00000000.000:002016-03-10 00:00:002016-03-10 00:00:002016-03-10 00:00:002016-03-102016-03-10 06:30:00sleeping
0.01666670.200:012016-03-10 00:01:002016-03-10 00:01:002016-03-10 00:01:002016-03-102016-03-10 06:30:00sleeping
0.03333330.400:022016-03-10 00:02:002016-03-10 00:02:002016-03-10 00:02:002016-03-102016-03-10 06:30:00sleeping
0.05000000.600:032016-03-10 00:03:002016-03-10 00:03:002016-03-10 00:03:002016-03-102016-03-10 06:30:00sleeping
0.06666670.800:042016-03-10 00:04:002016-03-10 00:04:002016-03-10 00:04:002016-03-102016-03-10 06:30:00sleeping
0.08333331.000:052016-03-10 00:05:002016-03-10 00:05:002016-03-10 00:05:002016-03-102016-03-10 06:30:00sleeping
0.10000001.200:062016-03-10 00:06:002016-03-10 00:06:002016-03-10 00:06:002016-03-102016-03-10 06:30:00sleeping
0.11666671.400:072016-03-10 00:07:002016-03-10 00:07:002016-03-10 00:07:002016-03-102016-03-10 06:30:00sleeping
0.13333331.600:082016-03-10 00:08:002016-03-10 00:08:002016-03-10 00:08:002016-03-102016-03-10 06:30:00sleeping
0.15000001.800:092016-03-10 00:09:002016-03-10 00:09:002016-03-10 00:09:002016-03-102016-03-10 06:30:00sleeping

Then I could continue using the code from the gist, this step generating stuff for the two clock hands.

all_times <- all_times %>% 
  dplyr::select(- start.x, - end.x, - start.y, - end.y) %>%
  tidyr::gather(name,, 1:2) %>%
  dplyr::arrange(index) %>%
  dplyr::mutate(hands = ifelse(name == "hour_pos", 0.5, 1))

I decided to only plot a subset of my exciting day.

all_times <- dplyr::filter(all_times, 
                           time > lubridate::ymd_hms("2016-03-10 15:00:00"),
                           time < lubridate::ymd_hms("2016-03-10 23:00:00") )

Last but not least, I plotted the clock. Nowadays I’d probably stick to magick for animating the plot but gganimate is awesome too anyway.

clock <- ggplot(all_times, aes(xmin =,
                               xmax = + 0.03, 
                               ymin = 0,
                               ymax = hands,
                              frame = index))
clock <- clock + geom_rect()
clock <- clock + theme_bw()
clock <- clock + coord_polar()
clock <- clock + scale_x_continuous(limits = c(0,12), 
                                    breaks = 0:11,
                     labels = c(12, 1:11))
clock <- clock + scale_y_continuous(limits=c(0,1.1)) 
clock <- clock + coord_polar()
clock <- clock + geom_text(aes(x = 0,
                               y = 0,
                               label = emojifont::emoji(activity)),
clock <- clock + theme(legend.position="none",
                      axis.text.y = element_blank(),
                      axis.ticks.y = element_blank(),
                      axis.title.y = element_blank(),
                      axis.title.x = element_blank(),
                      panel.background = element_blank(),
                      panel.border = element_blank(),
                      panel.grid.major = element_blank(),
                      panel.grid.minor = element_blank(),
                      plot.background = element_blank())

animation::ani.options(interval = 0.025)
gganimate::gganimate(clock, "clock.gif")

I ended up with a very useful visualization as you can see… at least, I have no trouble finding out when wine o’clock is!

To leave a comment for the author, please follow the link and comment on their blog: Maëlle. 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.

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)