knitr::opts_chunk$set(fig.width = 7, fig.height = 7, fig.align = 'center', out.width='95%', dpi = 200, collapse = TRUE, comment = "#>")
Because of low temperatures education authorities decided to suspend classes in public and private schools in some parts of Mexico City on Wednesday January 31, 2018. We will download and graph the daily morning hourly temperature and compare the days of January 30 and 31 with the rest of the month.
We use the get_station_month_data
function to download hourly temperature data for the month of January.
## Auto-install required R packages if (!require("pacman")) install.packages("pacman") pacman::p_load(aire.zmvm, dplyr, ggplot2, lubridate) # Download hourky (HORARIOS) temperature (TMP) data for January 2018 jan <- get_station_month_data("HORARIOS", "TMP", 2018, 1)
Here's what the data we downloaded looks like:
knitr::kable(head(jan, 10))
After acquiring the data all that's left to do is to clean the data. Note that since it is winter daylight saving time doesn't apply.
temp_morning <- jan %>% mutate(day = day(date)) %>% # Kids were on vaction until the 6th and only go to school during weekdays filter(hour %in% c(5:11) & day > 6 & wday(date)) %>% mutate(hour = factor(hour, levels = c(5:11))) # The school suspension was announced on the afternoon of January 30 temp_morning$color <- "other school days" temp_morning$color[which(temp_morning$date == "2018-01-30")] <- "Jan 30-31" temp_morning$color[which(temp_morning$date == "2018-01-31")] <- "Jan 30-31" # remove the stations that didn't report data during January 30-31 temp_morning <- temp_morning %>% group_by(station_code) %>% filter(sum(value[date == "2018-01-30"], na.rm = TRUE) > 0 & sum(value[date == "2018-01-31"], na.rm = TRUE) > 0) %>% ungroup() %>% na.omit() # reorder the data so the facets with the lowest temperatures # come first temp_morning <- mutate(temp_morning, station_code = reorder(station_code, value, min, na.rm = TRUE))
ggplot(temp_morning, aes(hour, value, group = day, color = color)) + geom_line(alpha = .8, size = .7) + facet_wrap(~ station_code) + scale_colour_manual("day", values = c("#542788", "#fdb863")) + scale_x_discrete(breaks = c(5, 8, 11), labels = c("5:00 AM", "8:00 AM", "11:00 AM")) + xlab("hour") + scale_y_continuous(breaks = c(0, 10, 20), minor_breaks = c(5, 15)) + ylab(expression(paste("temperature [",degree,"C]"))) + ggtitle("Daily morning temperatures in Mexico City during January 2018, by station", subtitle = "Data only includes school days during January\nSource: SEDEMA") + theme_bw() + theme(axis.text.x = element_text(angle = 80, hjust = 1), legend.position="top")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.