# Install if you haven't already install.packages("plotly") install.packages("dplyr") install.packages("gridExtra") install.packages("ggplot2") install.packages("reshape2") # Libraries library(plotly) library(dplyr) library(gridExtra) library(ggplot2) library(reshape2) # PREPROCESSING (run this first) # ---------------------------------------------- # Preprocessing Exercise 3 covid_cases <- read.csv(url("https://covid19-dashboard.ages.at/data/CovidFaelle_Timeline.csv"), sep = ";") covid_cases$Time <- as.POSIXct(covid_cases$Time, format = "%d.%m.%Y %H:%M:%S") covid_cases$DateWeek <- factor(strftime(covid_cases$Time, format = "%G-W%V")) covid_cases <- covid_cases[!duplicated(covid_cases[c("BundeslandID", "DateWeek")]),] # function for frame building accumulate_by <- function(dat, var) { var <- lazyeval::f_eval(var, dat) lvls <- plotly:::getLevels(var) dats <- lapply(seq_along(lvls), function(x) { cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]]) }) dplyr::bind_rows(dats) } covid_animation_data <- covid_cases %>% accumulate_by(~Time) covid_animation_data$frame <- factor(covid_animation_data$frame) # filter by salzburg and austria for a smaller data frame covid_animation_data <- covid_animation_data %>% filter(BundeslandID == 5 | BundeslandID == 10) # Preprocessing Exercise 4 covid_cases_districts <- read.csv(url("https://covid19-dashboard.ages.at/data/CovidFaelle_Timeline_GKZ.csv"), sep = ";") covid_cases_districts$Time <- as.POSIXct(covid_cases_districts$Time, format = "%d.%m.%Y %H:%M:%S") covid_cases_districts$DateWeek <- factor(strftime(covid_cases_districts$Time, format = "%G-W%V")) covid_cases_districts <- covid_cases_districts[!duplicated(covid_cases_districts[c("GKZ", "DateWeek")]),] # EXERCISE 1: 3D RANDOM WALK # ---------------------------------------------- # Plot 3D random walk similar to the previous example. # - Use `add_trace()`. # - Look up the right type and mode on https://plotly.com/r/3d-line-plots/. ... # EXERCISE 2: GGPLOTLY AND FACETS # ---------------------------------------------- # Create a ggplot boxplot using the data set `tips` grouped by day. # 1. Convert it to an interactive graph and find the median. # 2. Add a title (and maybe more) to the resulting ggplotly object. # Create an interactive facet histogram using the tips data set. Group by `size` and use `tip` as x-axis. ... # EXERCISE 3: ANIMATIONS # ---------------------------------------------- # Animate the number of covid cases in Salzburg and Austria (as a whole) over time. # - Use the data set `covid_animation_data` (already accumulated by time). # - Do not forget to split by "Bundesland". # - Use the `scatter` type and mode `lines`. ... %>% ... %>% ... %>% layout(title = "Covid Cases in Austria since February 2020", xaxis = list(range = c("2020-03-01", "2022-05-09"), title = "") ) # EXERCISE 4: HIGHLIGHTING # ---------------------------------------------- # Plot the number of covid cases over time for all districts in Austria as a time-series. # - Add a highlighting functionality for selecting districts and (optionally) add a drop down menu. # - Don't forget to group the data by *Bezirk* or *GKZ* (same result) when plotting. # Step 1: Create shared data object # Step 2: Create the plot