Skip to content

Instantly share code, notes, and snippets.

@juanchiem
Last active April 10, 2022 13:15
Show Gist options
  • Save juanchiem/b7ead20da859df164bef824a9df35389 to your computer and use it in GitHub Desktop.
Save juanchiem/b7ead20da859df164bef824a9df35389 to your computer and use it in GitHub Desktop.
```{r}
pacman::p_load(tidyverse)
```
```{r, eval=FALSE}
dataURL <- "http://coopnecochea.com/web/download/lluvias/Lluvias-2021.xls"
download.file(dataURL, destfile=data)
```
```{r}
prec21_raw <- tibble(
mes = "data/Lluvias-2021.xls" %>% readxl::excel_sheets()) %>% filter(mes!="TOTAL AÑO 2021") %>%
mutate(
data = map(mes, ~readxl::read_excel("data/Lluvias-2021.xls",
sheet = .x,
n_max=15) %>%
mutate_all(as.character))) %>%
unnest(cols = c(data))
prec21 <- prec21_raw %>%
select(-TOTAL) %>%
replace(is.na(.), "0") %>%
mutate(across(everything(), ~str_replace(., "-----", "0"))) %>%
# mutate(across(matches("[1-9]"), as.numeric)) %>%
mutate_at(vars(`1`:`31`), as.numeric) %>%
left_join(
prec21 %>% distinct(mes) %>% add_column(month=1:12)
) %>%
relocate(month) %>%
pivot_longer(cols = c(`1`:`31`),
names_to = "dia",
values_to = "mm") %>%
mutate(day = lubridate::parse_date_time(paste0("2021/", month, "/", dia), "y / m / d")) %>%
mutate(mm=replace_na(mm, 0))
# prec21 %>% filter(is.na(mes))
```
```{r}
prec21 %>%
group_by(Lugar,month) %>%
summarise(mm=sum(as.numeric(mm))) %>%
ggplot()+
aes(x = month, y=mm)+
geom_col(fill = "steelblue")+
facet_wrap("Lugar")+
cowplot::theme_minimal_hgrid(font_size=9)+
scale_x_continuous(breaks=seq(1,12,1), labels=1:12)+
labs(title = "Precipitaciones 2021",
x = "",
subtitle = "Datos obtenidos de coopnecochea.com") +
geom_text(data = . %>% group_by(Lugar) %>% summarise(mm = sum(mm)),
aes(label = paste0(mm, "mm"), x = Inf, y = Inf, vjust= 1, hjust=1),
size =2)
```
@juanchiem
Copy link
Author

imagen

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