Last active
May 2, 2020 18:54
-
-
Save Joaobazzo/70910d5578d04a3f34f1f31ae924bd50 to your computer and use it in GitHub Desktop.
Google maps GPS locations due to COVID in Brazil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rm(list=ls()) | |
| library(data.table) | |
| library(dplyr) | |
| library(ggplot2) | |
| library(viridis) | |
| library(stringr) | |
| library(patchwork) | |
| google <- data.table::fread("data-raw/Global_Mobility_Report_google.csv") | |
| google <- google[country_region %in% "Brazil",] | |
| j_list <- c("retail_and_recreation","grocery_and_pharmacy","parks","transit_stations","workplaces","residential") | |
| for(j in j_list){ # j = j_list[1] | |
| state <- stringr::str_remove_all(unique(google$sub_region_1),"State of ");state <- state[-1]; | |
| df <- lapply(state, function(i){ # i = state[1] | |
| #vec <- unlist(google[,6]) %>% t() %>% as.vector() | |
| google1 <- google[sub_region_1 %like% i,] | |
| google2 <- data.table::data.table("mode" = rep(names(google1)[6:11],each = nrow(google1)), | |
| "data" = rep(1:63,6), | |
| "dia_mes" = gsub("2020-","",rep(google1$date,6)), | |
| "rate" = c(google1[,retail_and_recreation_percent_change_from_baseline], | |
| google1[,grocery_and_pharmacy_percent_change_from_baseline], | |
| google1[,parks_percent_change_from_baseline], | |
| google1[,transit_stations_percent_change_from_baseline], | |
| google1[,workplaces_percent_change_from_baseline], | |
| google1[,residential_percent_change_from_baseline]), | |
| "state" = i) | |
| return(google2) | |
| }) %>% data.table::rbindlist() | |
| df[,mode:= gsub("_percent_change_from_baseline","",mode)] | |
| df1 <- df[mode %in% j,] | |
| df2 <- df1 | |
| df2 <- df2[,sum(rate),by = state][order(V1),] | |
| p1 <- ggplot(df1, aes( data,state)) + | |
| geom_tile(aes(fill = rate),colour = "white") + | |
| viridis::scale_fill_viridis(option = "A",direction = -1) + | |
| scale_x_continuous(breaks = seq(1,63,2) - 0.5, | |
| labels = df1$dia_mes[seq(1,63,2)])+ | |
| guides(fill=guide_legend(title="Percent change \n from baseline")) + | |
| labs(title = paste0(stringr::str_to_title(j)," destination"), | |
| x = NULL, y = "State") + | |
| theme_bw() + theme_minimal() + | |
| theme(axis.text.x = element_text(angle = 90, hjust = 0,size=8), | |
| axis.text.y = element_text(angle = 0, hjust = 1,size=8), | |
| panel.grid.major = element_blank(), | |
| panel.grid.minor = element_blank() | |
| )+ | |
| coord_cartesian(xlim = c(df1$data[3], df1[.N,data]-2)) | |
| p1 | |
| p2 <- ggplot(df1, aes(data,rate, | |
| group = data, fill = rate)) + | |
| geom_boxplot() + | |
| viridis::scale_fill_viridis(option = "A",direction = -1) + | |
| labs(x = NULL, | |
| y = "Percentage change \n from baseline") + | |
| scale_x_continuous(breaks = seq(1,63,2) - 0.5, | |
| labels = df1$dia_mes[seq(1,63,2)]) + | |
| theme(axis.text.x = element_text(angle = 90, hjust = 0,size=8)) + | |
| coord_cartesian(xlim = c(df1$data[3], df1[.N,data]-2)) | |
| #p2 | |
| #theme_bw() + | |
| #theme_minimal() | |
| message(j) | |
| #p2 | |
| pf <- p1/p2 | |
| ggsave(paste0("plot/",j,".jpg"), | |
| units = "cm",scale = 2.50, | |
| width = 10, | |
| height = 8) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment