class: center, middle, inverse, title-slide # Visualizing time:
time series --- ## NASA data - a data set in the `GGally` package - consists of atmospheric measurements across a grid of locations ```r data(nasa, package = "GGally") head(nasa) ``` ``` ## time y x lat long date cloudhigh cloudlow cloudmid ozone pressure surftemp ## 1 1 1 1 -21.2 -113.8000 1995-01-01 0.5 31.0 2.0 260 1000 297.4 ## 2 1 1 2 -21.2 -111.2957 1995-01-01 1.5 31.5 2.5 260 1000 297.4 ## 3 1 1 3 -21.2 -108.7913 1995-01-01 1.5 32.5 3.5 260 1000 297.4 ## 4 1 1 4 -21.2 -106.2870 1995-01-01 1.0 39.0 4.0 258 1000 296.9 ## 5 1 1 5 -21.2 -103.7826 1995-01-01 0.5 48.0 4.5 258 1000 296.5 ## 6 1 1 6 -21.2 -101.2783 1995-01-01 0.0 50.0 2.5 258 1000 296.5 ## temperature id day month year ## 1 296.9 1-1 0 1 1995 ## 2 296.5 2-1 0 1 1995 ## 3 296.0 3-1 0 1 1995 ## 4 296.5 4-1 0 1 1995 ## 5 295.5 5-1 0 1 1995 ## 6 295.0 6-1 0 1 1995 ``` --- ## Time series For each observational unit, we have multiple measurements ```r nasa %>% dplyr::filter(x == 1, y == 1) %>% ggplot(aes(x = time, y = temperature)) + geom_point() ``` ![](02_time-series_files/figure-html/unnamed-chunk-2-1.png)<!-- --> --- ## Time series (2) For each observational unit, we have multiple measurements which we connect by a line ```r nasa %>% dplyr::filter(x == 1, y == 1) %>% ggplot(aes(x = time, y = temperature)) + geom_line() ``` ![](02_time-series_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- ## Time series (3) Each observational unit forms a group, we only connect points <br>within a group by a line. ```r nasa %>% dplyr::filter(x == 1, y %in% c(1, 10)) %>% ggplot(aes(x = time, y = temperature, group=id)) + geom_line() ``` ![](02_time-series_files/figure-html/unnamed-chunk-4-1.png)<!-- --> --- class: yourturn # Your Turn - Load the `nasa` data from the package `GGally` - for one location, draw a time line of Ozone over the time frame (`time`). - Plot separate lines for each of the years, i.e. put `month` on the x-axis and `ozone` on the y-axis for the same location. Is there a seasonal pattern apparent? - Pick locations with x in 1:10 and y in 7:10. Plot temperature over time. Comment on the result. --- ## Box office gross Variables encoded as dates and times work with `ggplot2` ```r data(box, package="classdata") box %>% dplyr::filter(Movie == "Star Wars Ep. VII: The Forc...") %>% ggplot(aes(x = Date, y = Total.Gross)) + geom_line() ``` ![](02_time-series_files/figure-html/unnamed-chunk-5-1.png)<!-- --> --- ## Box office gross (2) We would like to label some of the highest grossing movies ```r box %>% ggplot(aes(x = Date, y = Total.Gross, group = interaction(Movie, Distributor))) + geom_line() ``` ![](02_time-series_files/figure-html/unnamed-chunk-6-1.png)<!-- --> --- class: yourturn # Your Turn - Load the `box` data from the package `classdata` - For each movie and distributor, find: (1) the highest total gross, (2) the last date (and week) the movie was shown in theaters, (3) the gross the movie made in the first week it was featured on the box office list. (4) the number of times the movie appears on the box office list --- ## Labelling outliers `geom_text()` needs aesthetics `x`, `y` and `label` ```r box %>% ggplot(aes(x = Date, y = Total.Gross, group = interaction(Movie, Distributor))) + geom_line() + geom_text(aes(x = Date, y = Total.Gross, label=Movie), data = box_summary %>% dplyr::filter(Total.Gross > 5e8)) ``` ![](02_time-series_files/figure-html/unnamed-chunk-8-1.png)<!-- --> --- ## Labelling outliers - less overlap - `geom_text_repel()` from the `ggrepel` package ```r box %>% ggplot(aes(x = Date, y = Total.Gross, group = interaction(Movie, Distributor))) + geom_line() + ggrepel::geom_text_repel(aes(x = Date, y = Total.Gross, label=Movie), data = box_summary %>% dplyr::filter(Total.Gross > 5e8), colour="grey50") ``` ![](02_time-series_files/figure-html/unnamed-chunk-9-1.png)<!-- --> <!-- ## Box office gross (3) ```r box %>% filter(Week <= 5) %>% mutate(Year = year(Date)) %>% ggplot(aes(x = Week, y = Total.Gross, group = interaction(Movie, Distributor))) + geom_line() + facet_grid(.~Year) ``` --> --- ## Resources - reference/document: https://lubridate.tidyverse.org/index.html - RStudio cheat sheet for [lubridate](https://rawgit.com/rstudio/cheatsheets/master/lubridate.pdf) - RStudio cheat sheet for [ggplot2](https://github.com/rstudio/cheatsheets/blob/master/data-visualization-2.1.pdf)