Last active
October 20, 2024 20:50
-
-
Save FelipeSBarros/491938b8e54f71ec3090603d320a9b35 to your computer and use it in GitHub Desktop.
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
| # instalando pacotes necessários ---- | |
| # install.packages("patchwork") | |
| library(readr) | |
| library(dplyr) | |
| library(tidyr) | |
| library(ggplot2) | |
| library(patchwork) | |
| library(stringr) | |
| # lendo dados | |
| lista_csvs <- list.files(path='./Dados', full.names = T) | |
| lista_csvs | |
| for (csv in lista_csvs){ | |
| # csv = lista_csvs[2] | |
| nome_bacia <- stringr::str_split(csv, pattern='_')[[1]][2] | |
| nome_bacia <- stringr::str_split(nome_bacia, pattern = '.csv')[[1]][1] | |
| csv_bacia <- readr::read_delim(csv, delim = ';') | |
| csv_bacia <- csv_bacia |> | |
| rename_all( | |
| funs( | |
| stringr::str_replace_all(., ' \\(hectares\\)', '') | |
| ) | |
| ) | |
| csv_bacia_long <- pivot_longer(csv_bacia, | |
| cols=colnames(csv_bacia)[2:8], | |
| names_to='Classe', | |
| values_to='Area_ha') | |
| csv_bacia_long <- csv_bacia_long |> | |
| group_by(Ano, Classe) |> | |
| summarise(area_total = sum(Area_ha)) |> | |
| mutate( | |
| porcentagem = round((area_total / sum(area_total))*100, 1) | |
| # sum(area_total) | |
| ) | |
| # plot usos ---- | |
| usos <- csv_bacia_long |> | |
| filter( | |
| Classe %in% c( | |
| "Área de outras classes", | |
| "Área de massas de Água", | |
| "Área de irrigação", | |
| "Área de cultivo de Soja") | |
| ) |> | |
| ggplot() + geom_line(aes(x=Ano, y=porcentagem, group=Classe, color=Classe)) + | |
| labs(title=paste("Uso do solo", nome_bacia), | |
| x ="", y = "% área total") + | |
| # theme(legend.position=c(0.25, 0.78))+#c("bottom")) + | |
| theme(legend.title=element_blank(), | |
| legend.position=c("bottom")) | |
| ggsave(paste0("./Graficos/", nome_bacia, "_uso.png"), | |
| plot = last_plot(), | |
| width=7, height = 5) | |
| # plot massa de agua ---- | |
| massa_agua <- csv_bacia_long |> | |
| filter( | |
| Classe %in% c( | |
| "Área de massas de Água" | |
| )) |> | |
| ggplot() + geom_area(aes(x=Ano, y=porcentagem, group=Classe, fill=4, alpha=0.5)) + | |
| labs(title=paste("Área de massas de Água (hectares)", nome_bacia), | |
| x ="", y = "% área total") + | |
| theme(legend.position="none") + | |
| theme(legend.title=element_blank()) | |
| ggsave(paste0("./Graficos/", nome_bacia, "_MassaÁgua.png"), | |
| plot = last_plot(), | |
| width=7, height = 5) | |
| # plot Vegetacao nativa desmatamento ---- | |
| veg_desmatamento <- csv_bacia_long |> | |
| filter( | |
| Classe %in% c( | |
| "Área de cultivo de Soja", | |
| "Área de vegetação nativa", | |
| "Área desmatada sem Soja", | |
| "Área desmatada com Soja" | |
| )) |> | |
| ggplot() + geom_line( | |
| aes(x=Ano, y=porcentagem, group=Classe, color=Classe)) + | |
| labs(title=paste("Vegetação nativa, desmatamento e cultivo de soja (hectares)", nome_bacia), | |
| x ="", y = "% da área total") + | |
| # theme(legend.position=c(0.81, 0.85)) + | |
| theme(legend.title=element_blank(), | |
| legend.position=c("bottom")) | |
| ggsave(paste0("./Graficos/", nome_bacia, "_VegNatDesmatSoja.png"), | |
| plot = last_plot(), | |
| width=9, height = 5) | |
| # plot area desmatada ---- | |
| desmatamento <- csv_bacia_long |> | |
| filter( | |
| Classe %in% c( | |
| "Área desmatada sem Soja", | |
| "Área desmatada com Soja" | |
| )) |> | |
| ggplot() + geom_line( | |
| aes(x=Ano, y=porcentagem, group=Classe, color=Classe)) + | |
| labs(title=paste("Área desmatada", nome_bacia), | |
| x ="", y = "% área total") + | |
| # theme(legend.position=c(0.21, 0.85)) + | |
| theme(legend.title=element_blank(), | |
| legend.position=c("bottom")) | |
| ggsave(paste0("./Graficos/", nome_bacia, "_AreaDesmatada.png"), | |
| plot = last_plot(), | |
| width=9, height = 5) | |
| # Plot desmatamento ---- | |
| agua <- csv_bacia_long |> | |
| filter( | |
| Classe %in% c( | |
| "Área de massas de Água" | |
| )) | |
| desmatamento_agua <- csv_bacia_long |> | |
| filter( | |
| Classe %in% c( | |
| "Área desmatada sem Soja", | |
| "Área desmatada com Soja" | |
| )) |> | |
| ggplot() + geom_col( | |
| aes(x=Ano, y=porcentagem, group=Classe, color=Classe, fill=Classe), position = 'stack') + | |
| labs(title=paste("Área desmatada e massas de água", nome_bacia), | |
| x ="", y = "% área total") + | |
| geom_line(data=agua, aes(x=Ano, y=porcentagem*100), color='blue', size=2) + | |
| scale_y_continuous( | |
| name="% Área desmatada", | |
| sec.axis = sec_axis(~./100, name="% Área de massas de Água"), | |
| # expand = c(0,0)) | |
| ) + | |
| theme( | |
| legend.position=c("bottom"), | |
| legend.title=element_blank(), | |
| axis.text.y = element_text(angle = 90), | |
| axis.title.y.right = element_text(color = 'blue'), | |
| axis.line.y.right = element_line(color = "blue"), | |
| axis.ticks.y.right = element_line(color = "blue"), | |
| axis.text.y.right = element_text(color = "blue") | |
| ) | |
| ggsave(paste0("./Graficos/", nome_bacia, "_AreaDesmatadaMassasAgua.png"), | |
| plot = last_plot(), | |
| width=9, height = 5) | |
| usos / veg_desmatamento / desmatamento + plot_annotation(tag_levels = "I") | |
| ggsave(paste0("./Graficos/", nome_bacia, "_graficos_usos.png"), | |
| plot = last_plot(), | |
| width=10, height = 7) | |
| desmatamento_agua / massa_agua + plot_annotation(tag_levels = "I") | |
| ggsave(paste0("./Graficos/", nome_bacia, "_graficos_agua.png"), | |
| plot = last_plot(), | |
| width=10, height = 7) | |
| } |
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
| # aragauaia | |
| csv_bacia_long |> filter(Classe== | |
| "Área de cultivo de Soja" | |
| # "Área de vegetação nativa" | |
| # "Área desmatada com Soja" | |
| ) |> mutate("%"=(Area_ha/32518772)*100) |> | |
| print(n=100) | |
| # parana | |
| csv_bacia_long |> filter(Classe== | |
| # "Área de cultivo de Soja" | |
| "Área de vegetação nativa" | |
| # "Área desmatada com Soja" | |
| ) |> mutate("%"=(Area_ha/22274698)*100) |> | |
| print(n=100) | |
| # parnaiba | |
| csv_bacia_long |> filter(Classe== | |
| "Área de cultivo de Soja" | |
| # "Área de vegetação nativa" | |
| ) |> mutate("%"=(Area_ha/32708808)*100) |> | |
| select(Ano, Area_ha, '%') |> | |
| print(n=100) | |
| # Sao Francisco | |
| csv_bacia_long |> filter(Classe== | |
| # "Área de cultivo de Soja" | |
| "Área de vegetação nativa" | |
| ) |> mutate("%"=(Area_ha/42533767)*100) |> | |
| select(Ano, Area_ha, '%') |> | |
| print(n=100) | |
| # Taquari | |
| csv_bacia_long |> filter(Classe== | |
| # "Área de cultivo de Soja" | |
| "Área de vegetação nativa" | |
| ) |> mutate("%"=(Area_ha/2815059)*100) |> | |
| select(Ano, Area_ha, '%') |> | |
| print(n=100) | |
| # Tocantins | |
| csv_bacia_long |> filter(Classe== | |
| # "Área de cultivo de Soja" | |
| "Área de vegetação nativa" | |
| ) |> mutate("%"=(Area_ha/30137352)*100) |> | |
| select(Ano, Area_ha, '%') |> | |
| print(n=100) | |
| # Tocantins | |
| csv_bacia_long |> filter(Classe== | |
| # "Área de cultivo de Soja" | |
| "Área de vegetação nativa" | |
| ) |> mutate("%"=(Area_ha/30137352)*100) |> | |
| select(Ano, Area_ha, '%') |> | |
| print(n=100) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment