class: center, middle, inverse, title-slide # Calendar-based graphics
for visualizing people’s daily schedules ### Earo Wang ###
30 July 2018
slides at
http://slides.earo.me/jsm18
--- class: center .pull-left[
] .pull-right[ ## .blue[Melbourne <br> pedestrian counting sensors] <br> <img src="img/sensor.png" height="450px"> ] --- .left-column[ <br> <br> <br> <br> <br> <br> ## What is a typical working day like at Southern Cross Station? ] .right-column[ <br> <br> ![](img/day1.gif) ] ??? * One normal working day in last Jan * --- .left-column[ <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> ## More and more ] .right-column[ <br> <br> <br> <video autoplay loop controls> <source src="img/sx17.mp4" type="video/mp4"> </video> ] --- .left-column[ <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> ## Alternative display ] .right-column[ <img src="figure/sx-wrap-1.svg" style="display: block; margin: auto;" /> ] --- class: inverse middle ## <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-swimmer"></i> <i class="fas fa-swimmer"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-swimmer"></i> <i class="fas fa-swimmer"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> <i class="fas fa-walking"></i> --- background-image: url(img/calendar.png) background-size: cover --- background-image: url(figure/sx-hol-1.svg) background-size: 100% 100% --- ### `sugrrants::frame_calendar()` .pull-left[ <br> <br> <br> <br> * It's not a plotting function, but provides a data-restructuring tool. * Its name draws inspiration from `tibble::frame_data()`, but the origin has shifted to `tibble::tribble()`. * It needs a better meaning: frame the series into the calendar canvas. ] .pull-right[ <br> <br> <br> <br> <img src="figure/sx-oct-1.svg" style="display: block; margin: auto;" /> ] --- ## Construction .center[<img src="img/month.png" width=450>] <br> The grid position for any day in the month is given by `$$\begin{align} i &= \lceil (g \mod 35) / 7\rceil \\ j &= g \mod 7. \end{align}$$` Let `\(h\)` and `\(c\)` be the scaled hour and count, respectively, then the final coordinates are given by: `$$\begin{align} x &= j + h \\ y &= i - c. \end{align}$$` --- ## Map over each month ```r *mday_x <- mday(x) *month_x <- unique(x - mday_x + 1) *if (sunday) { # Weekday starts with Sunday * first_wday <- wday(month_x, week_start = 7) # k *} else { # starts with Monday * first_wday <- wday(month_x, week_start = 1) # k *} counter <- map2( # g .x = first_wday, .y = days_x, function(.x, .y) .x + 0:(.y - 1) ) row_idx <- map( # i counter, function(x) ifelse(x == ncells, max_wks, ceiling((x %% ncells) / ndays)) ) col_idx <- map( # j counter, function(x) ifelse(x %% ndays == 0, ndays, x %% ndays) ) ``` --- .left-column[ ## Easy to use ### - frame_calendar() ] .right-column[ ```r sx_cal <- sx %>% frame_calendar(x = Time, y = Count, date = Date) sx_cal ``` ``` #> # A tibble: 8,760 x 6 #> Date_Time Date Time Count .Time .Count #> * <dttm> <date> <int> <int> <dbl> <dbl> #> 1 2017-01-01 00:00:00 2017-01-01 0 1335 0.258 0.986 #> 2 2017-01-01 01:00:00 2017-01-01 1 463 0.260 0.972 #> 3 2017-01-01 02:00:00 2017-01-01 2 219 0.261 0.968 #> 4 2017-01-01 03:00:00 2017-01-01 3 122 0.263 0.967 #> 5 2017-01-01 04:00:00 2017-01-01 4 21 0.264 0.965 #> 6 2017-01-01 05:00:00 2017-01-01 5 28 0.266 0.965 #> # ... with 8,754 more rows ``` ] --- .left-column[ ## Easy to use ### - frame_calendar() ### - ggplot2 ] .right-column[ ```r p_sx <- ggplot(sx_cal, aes(.Time, .Count, group = Date)) + geom_line() ``` <img src="figure/sx-2-1.svg" style="display: block; margin: auto;" /> ] --- .left-column[ ## Easy to use ### - frame_calendar() ### - ggplot2 ### - prettify() ] .right-column[ ```r prettify(p_sx) ``` <img src="figure/sx-3-1.svg" style="display: block; margin: auto;" /> ] --- .left-column[ ## Easy to use ### - frame_calendar() ### - ggplot2 ### - prettify() ### - plotly ] .right-column[ ```r library(plotly) pp_sx <- sx_cal %>% group_by(Date) %>% plot_ly(x = ~ .Time, y = ~ .Count) %>% add_lines() prettify(pp_sx) ```
] --- ## Options: calendar type .pull-left[ `calendar = "weekly"` <br> <br> <img src="figure/weekly-1.svg" style="display: block; margin: auto;" /> ] .pull-right[ `calendar = "daily"` <br> <br> <img src="figure/daily-1.svg" style="display: block; margin: auto;" /> ] --- ## Options: polar .pull-left[ `polar = FALSE` <br> <br> <img src="figure/linear-1.svg" style="display: block; margin: auto;" /> ] .pull-right[ `polar = TRUE` <br> <br> <img src="figure/polar-1.svg" style="display: block; margin: auto;" /> ] --- ## Multiple calendars <img src="figure/multiple-1.svg" style="display: block; margin: auto;" /> --- ## Showcase .pull-left[ Energy consumption ![elec](img/elec-consumption.png) ] .pull-right[ Bus delay <br> <br> <br> <br> <br> ![bus](img/bus-delay.jpg) .red[.footnote[left: Roach, C (forthcoming 2018). Assessing the impact of building characteristics on energy consumption using semiparametric mixed models. <br> right: [Tom Elliott's tweet](https://twitter.com/tomelliottnz/status/958887861943640064)]] ] --- class: middle inverse ### The full range of plotting capabilities in ggplot2 is essentially available, from geom_line() to geom_ribbon(). -- ### Patterns on special events, like Australian Day, or Australian Open, more easily pop out to the viewer as public holidays. -- ### Localize the calendar: other languages are supported for labels, like CJK. .pull-left[ <img src="figure/chn-1.svg" style="display: block; margin: auto;" /> ] .pull-right[ <img src="figure/fr-1.svg" style="display: block; margin: auto;" /> ] --- class: inverse middle center ### Joint work with
[Di Cook](http://dicook.org) & [Rob J Hyndman](http://robjhyndman.com) ### More on sugrrants
<http://pkg.earo.me/sugrrants> ### Slides created via xaringan <i class="fas fa-user-ninja"></i> <http://slides.earo.me/jsm18> ### Open source
[earowang/jsm18](https://github.com/earowang/jsm18) ### This work is under licensed [
BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/).