Skip to content

Instantly share code, notes, and snippets.

@juanchiem
Last active February 1, 2021 14:36
Show Gist options
  • Save juanchiem/e26e6b600c54ba5b06184ba424c3deed to your computer and use it in GitHub Desktop.
Save juanchiem/e26e6b600c54ba5b06184ba424c3deed to your computer and use it in GitHub Desktop.
library(tidyverse)
coop_dir = tempfile(fileext = ".xls")
dataURL <- "http://coopnecochea.com/web/download/lluvias/Lluvias-2021.xls"
download.file(dataURL, destfile=coop_dir)
ene_2021 <- tibble(mes = coop_dir %>% readxl::excel_sheets()) %>%
mutate(data = map(mes, ~readxl::read_excel(coop_dir, sheet = .x) %>%
mutate_all(as.character))) %>%
filter(mes=="ENE") %>%
unnest(cols = c(data)) %>%
drop_na(Lugar) %>%
mutate(across(everything(), ~replace_na(.x, 0))) %>%
mutate(across(everything(), ~str_replace(., "-----", "0")))
ene_2021 %>%
select(-TOTAL,-mes) %>%
pivot_longer(cols = c(`1`:`31`),
names_to = "dia",
values_to = "mm") %>%
mutate(semana = lubridate::week(lubridate::parse_date_time(paste0(dia, "/1"), "d / m"))) %>%
group_by(Lugar,semana) %>%
summarise(mm=sum(as.numeric(mm))) %>%
ggplot()+
aes(x = semana, y=mm)+
geom_col(fill = "steelblue")+
facet_wrap("Lugar")+
cowplot::theme_minimal_hgrid(font_size=9)+
theme(axis.text.x=element_text(angle=60, hjust=1),
plot.subtitle = element_text(face= "italic"))+
labs(title = "Precipitaciones acumuladas enero 2021",
x = "",
subtitle = "Datos obtenidos de coopnecochea.com\n(@juanchiem)")+
geom_text(data = . %>% group_by(Lugar) %>% summarise(semana = first(semana),
mm = sum(mm)),
aes(label = paste0(mm, "mm"), x = Inf, y = 80, hjust= 1.2),
size =3)
# https://git.io/JtleL
@juanchiem
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment