diff --git a/NAMESPACE b/NAMESPACE index 48786492799f0a1684080fe90fb933856477cc99..5062eb849b1cb24e6bdf8bcdc26d14249b1a5443 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,7 +12,9 @@ export(creer_graphe_1_5) export(creer_graphe_2_3) export(creer_graphe_2_4) export(creer_graphe_2_5) +export(creer_graphe_3_4) export(format_fr_pct) +importFrom(COGiter,filtrer_cog) importFrom(attempt,stop_if_not) importFrom(dplyr,arrange) importFrom(dplyr,case_when) @@ -23,6 +25,7 @@ importFrom(dplyr,group_by) importFrom(dplyr,left_join) importFrom(dplyr,mutate) importFrom(dplyr,pull) +importFrom(dplyr,rename) importFrom(dplyr,select) importFrom(dplyr,summarise) importFrom(dplyr,ungroup) diff --git a/NEWS.md b/NEWS.md index 3f86cc79621c02d8c69b8931eb2385306ecd9c5f..0cce1a529b97834dc83b896a123ae712c8ba7ad0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # propre.artificialisation 0.0.0.9000 * Création site pkgdown -* Ajout fonctions d'illustration `creer_carte_1_3()`,`creer_carte_1_7()`, `creer_graphe_1_1()`, `creer_graphe_1_4()`, `creer_graphe_1_5()` et leurs test +* Ajout fonctions d'illustration `creer_carte_1_3()`,`creer_carte_2_2()`,`creer_carte_2_7()`,`creer_carte_3_2()`, `creer_graphe_1_1()`, `creer_graphe_1_4()`, `creer_graphe_1_5()`, `creer_graphe_2_3()`, `creer_graphe_2_4()`, `creer_graphe_2_5()`, `creer_graphe_3_4()` et leurs test * Ajout template de publication * Ajout fichier `NEWS.md` pour tracer les changements dans le package. diff --git a/R/creer_graphe_3_4.R b/R/creer_graphe_3_4.R new file mode 100644 index 0000000000000000000000000000000000000000..b0163cc5a6be6dec6fd82544aa5c7056b849e41f --- /dev/null +++ b/R/creer_graphe_3_4.R @@ -0,0 +1,117 @@ +#' Creation du graphique en barres par departement des surfaces artificialisees pour un nouvel habitant. +#' @description Graphique en barres par departement des surfaces artificialisees pour un nouvel habitant. +#' +#' @param millesime_obs_artif_gk3 une annee parmi les millesimes selectionnables par l'utilisateur, au format numerique. +#' @param millesime_population une annee parmi les millesimes selectionnables par l'utilisateur, au format numerique. +#' @param millesime_obs_artif une annee parmi les millesimes selectionnables par l'utilisateur, au format numerique. +#' @param code_reg code insee de la région sur laquelle construire le graphique +#' +#' @return Un graphique en barres +#' +#' @importFrom dplyr mutate filter select rename group_by summarise ungroup left_join arrange +#' @importFrom forcats fct_relevel fct_inorder +#' @importFrom ggplot2 ggplot aes geom_bar position_dodge geom_text coord_flip labs theme +#' @importFrom ggtext element_markdown +#' @importFrom glue glue +#' @importFrom gouvdown scale_fill_gouv_discrete +#' @importFrom lubridate year +#' @importFrom tidyr gather +#' @importFrom COGiter filtrer_cog +#' +#' @export +#' +#' @examples +#' creer_graphe_3_4(millesime_obs_artif_gk3 = 2019,millesime_obs_artif = 2019, +#' millesime_population = 2018, code_reg = '52') + +creer_graphe_3_4 <- function(millesime_obs_artif_gk3 = NULL,millesime_obs_artif = NULL,millesime_population = NULL, code_reg = NULL){ + + # # calcul des millesimes utilises + millesime_debut_flux <- millesime_obs_artif - 10 + millesime_fin_flux <- millesime_obs_artif - 1 + millesime_depart_gk3 <- millesime_obs_artif_gk3 - 9 + millesime_debut_population <- millesime_population - 10 + millesime_fin_population <- millesime_population + + if (is.numeric(code_reg)) { + code_reg = as.character(code_reg) + } + + # preparation donnees du stock + stock_artif_gk3 <- observatoire_artificialisation_gk3 %>% + dplyr::mutate(date=lubridate::year(.data$date)) %>% + COGiter::filtrer_cog(reg = code_reg) %>% + dplyr::filter(((.data$TypeZone == "D\u00e9partements") | (.data$TypeZone == "R\u00e9gions")), + .data$date == millesime_obs_artif_gk3 - 9) %>% + dplyr::mutate(surface_artificialisee = .data$surface_artificialisee) %>% + dplyr::select (-.data$surface_naf,-.data$date) %>% + dplyr::rename ("stock_gk3_depart"="surface_artificialisee") + + # preparation des donnees observatoires artificialisation + obs_artif <- observatoire_artificialisation %>% + dplyr::mutate(date = as.numeric(lubridate::year(.data$date))) %>% + COGiter::filtrer_cog(reg = code_reg) %>% + dplyr::filter(((.data$TypeZone == "D\u00e9partements") | (.data$TypeZone == "R\u00e9gions")), + .data$date <= millesime_obs_artif, + .data$date >= millesime_obs_artif - 9) %>% + dplyr::mutate(flux_naf_artificialisation_total = .data$flux_naf_artificialisation_total) %>% + dplyr::group_by(.data$CodeZone, .data$TypeZone, .data$Zone) %>% + dplyr::summarise(somme_artif_10ans = sum(.data$flux_naf_artificialisation_total, na.rm = T)) %>% + dplyr::ungroup() + + # preparation des donnees population + evol_popul <- population_legale %>% + dplyr::mutate(date=lubridate::year(.data$date)) %>% + COGiter::filtrer_cog(reg = code_reg) %>% + dplyr::filter(((.data$TypeZone == "D\u00e9partements") | (.data$TypeZone == "R\u00e9gions")), + .data$date == millesime_debut_population | .data$date == millesime_fin_population) + popul_debut <- evol_popul %>% + dplyr::filter(.data$date == millesime_debut_population) %>% + dplyr::rename ("population_debut"="population_municipale") %>% + dplyr::select (-.data$date) + evol_popul <- evol_popul %>% + dplyr::group_by(.data$TypeZone,.data$Zone,.data$CodeZone) %>% + dplyr::summarise(evolution_population=((sum(.data$population_municipale[which(date==millesime_fin_population)],na.rm=T) - sum(.data$population_municipale[which(date==millesime_debut_population)],na.rm=T))-1)) %>% + dplyr::ungroup() + + data <- stock_artif_gk3 %>% + dplyr::left_join(obs_artif) %>% + dplyr::left_join(evol_popul) %>% + dplyr::left_join(popul_debut) %>% + dplyr::mutate(Zone=as.factor(.data$Zone)) %>% + dplyr::mutate(surf_artif_par_hab_an_x=.data$stock_gk3_depart / .data$population_debut ) %>% + dplyr::mutate(surf_artif_par_nouv_hab_entre_x_y =.data$somme_artif_10ans / .data$evolution_population ) %>% + dplyr::select (.data$TypeZone,.data$Zone,.data$surf_artif_par_hab_an_x,.data$surf_artif_par_nouv_hab_entre_x_y) %>% + tidyr::gather(variable, valeur, 3:4) %>% + dplyr::arrange(desc(.data$TypeZone),desc(.data$Zone)) %>% + dplyr::mutate(variable=forcats::fct_relevel(.data$variable,"surf_artif_par_nouv_hab_entre_x_y","surf_artif_par_hab_an_x")) %>% + dplyr::mutate(Zone = forcats::fct_drop(.data$Zone) %>% + forcats::fct_inorder()) + + # creation du graphique + valeur_max_graphique <- max(data$valeur,na.rm=T)+500 + + graph_3_4 <- data %>% ggplot2::ggplot(ggplot2::aes(x=.data$Zone,y=.data$valeur,fill=.data$variable), width = 0.9) + + ggplot2::geom_bar(stat="identity",position = ggplot2::position_dodge())+ + ggplot2::coord_flip() + + ggplot2::geom_text(ggplot2::aes(label=format(round(.data$valeur,0), big.mark = " ")),position= ggplot2::position_dodge(width=1), hjust=-0.1)+ + gouvdown::scale_fill_gouv_discrete(palette = "pal_gouv_qual2") + + ggplot2::labs( + title = glue::glue("Surfaces cadastr\u00e9es artificialis\u00e9es (m2)", width = 60), + subtitle = glue::glue("<span style = 'color:#7D4E5B'>par habitant pr\u00e9sent en {millesime_debut_population}</span> et <span style = 'color:#169B62'>par nouvel habitant entre {millesime_debut_population} et {millesime_fin_population}</span> "), + x = "", y = "", + fill = "", + caption = glue::glue("Source : DGFip/Cerema - Insee - {millesime_obs_artif}") + ) + + ggplot2::scale_y_continuous( + limits = c(0, max(data$valeur) +500), + labels = scales::label_number(big.mark = " ", decimal.mark = ",") + ) + + ggplot2::theme( + legend.position = "none", + plot.subtitle = ggtext::element_markdown(size = 12, lineheight = 1.2) + ) + + return(graph_3_4) + +} diff --git a/R/data.R b/R/data.R index 577f68ab51cadf805babc307449fd2fa47086d5a..226b1233f8eb280776e0e26b01ca9d3c3920031f 100644 --- a/R/data.R +++ b/R/data.R @@ -88,3 +88,17 @@ #' } #' @source \url{https://agreste.agriculture.gouv.fr/agreste-web/disaron/W0020/detail/} "observatoire_artificialisation_gk3" + +#' Table contenant les différents indicateurs de la source population_legale utiles pour la publication. +#' +#' @encoding UTF-8 +#' @format Table de 25704 lignes et 5 colonnes: +#' \describe{ +#' \item{TypeZone}{Type de territoire} +#' \item{CodeZone}{Code du territoire} +#' \item{Zone}{Libellé du territoire} +#' \item{date}{Millésime de la source} +#' \item{population_municipale}{population minicipale} +#' } +#' @source \url{https://agreste.agriculture.gouv.fr/agreste-web/disaron/W0020/detail/} +"population_legale" diff --git a/R/globals.R b/R/globals.R index 714b0a4642fd247478c8673972fb75d8bfb62780..cb9335c66f5873059aaab4648a3df09347de1283 100644 --- a/R/globals.R +++ b/R/globals.R @@ -1,4 +1,4 @@ utils::globalVariables( c("teruti","result","observatoire_artificialisation",".data","variable",".", - "valeur","etalement_urbain","observatoire_artificialisation_gk3") + "valeur","etalement_urbain","observatoire_artificialisation_gk3","population_legale") ) diff --git a/data-raw/dataprep.R b/data-raw/dataprep.R index e142ca8bdd0fa8dd1a84439e6638bb7ff56bf93e..b901bac37996866a64f0b360d452aab3c1efc976 100644 --- a/data-raw/dataprep.R +++ b/data-raw/dataprep.R @@ -58,15 +58,20 @@ con_datamart <- dbConnect(drv, dbname="datamart", host="10.44.128.174", port=543 password=Sys.getenv("sgbd_pwd")) postgresqlpqExec(con_datamart, "SET client_encoding = 'windows-1252'") observatoire_artificialisation<-dbReadTable(con_datamart, - c("portrait_territoires","cogifiee_observatoire_artificialisation")) + c("portrait_territoires","cogifiee_observatoire_artificialisation")) # chargement des indicateurs d'etalement urbain depuis SGBD etalement_urbain<-dbReadTable(con_datamart, - c("portrait_territoires","indicateur_etalement_urbain")) + c("portrait_territoires","indicateur_etalement_urbain")) # chargement donnees observatoire artificialisation Geokit3 depuis SGBD observatoire_artificialisation_gk3<-dbReadTable(con_datamart, - c("portrait_territoires","indicateur_observatoire_artificialisation_geokit3")) + c("portrait_territoires","indicateur_observatoire_artificialisation_geokit3")) + +# chargement donnees population legale depuis SGBD +population_legale<-dbReadTable(con_datamart, + c("portrait_territoires","cogifiee_population_legale")) + # chargement des tables cogifiées d'indicateurs territoriaux load("extdata/cogifiee_chargement_ocsge.RData") @@ -76,8 +81,7 @@ load("extdata/cogifiee_chargement_pci.RData") pci <- data_cogifiee load("extdata/cogifiee_chargement_pop_rgp_insee.RData") pop_rgp_insee <- data_cogifiee -load("extdata/cogifiee_chargement_population_legale.RData") -population_legale <- data_cogifiee + load("extdata/indicateur_observatoire_artificialisation_geokit3.RData") load("extdata/indicateur_observatoire_artificialisation_population_legale_cogiter.RData") @@ -87,7 +91,6 @@ load("extdata/indicateur_pci_population_legale.RData") result <- bind_rows( ocsge, pci, pop_rgp_insee, - population_legale, indicateur_observatoire_artificialisation_geokit3, indicateur_observatoire_artificialisation_population_legale_cogiter, indicateur_pci_lgt_rgp_insee, @@ -96,7 +99,6 @@ result <- bind_rows( rm( data_cogifiee, ocsge, pci, pop_rgp_insee, - population_legale, indicateur_observatoire_artificialisation_population_legale_cogiter, indicateur_observatoire_artificialisation_geokit3, indicateur_pci_lgt_rgp_insee, @@ -106,8 +108,8 @@ rm( metadata_donnee <- read.csv2("extdata/indicateurs.csv", as.is = TRUE, encoding = "UTF-8") %>% mutate(source = str_replace(source, "chargement_", "") %>% - str_replace("indicateur_", "") %>% - str_replace("_cogiter", "")) + str_replace("indicateur_", "") %>% + str_replace("_cogiter", "")) # liste des zonages de la région 52 liste_52 <- liste_zone %>% @@ -136,6 +138,17 @@ observatoire_artificialisation_gk3 <- observatoire_artificialisation_gk3 %>% TypeZone = iconv(TypeZone,to = "UTF-8")) %>% mutate(across(where(is.character),as.factor)) +population_legale<- population_legale %>% + filter(CodeZone %in% liste_52 | TypeZone == "Régions"| TypeZone == "D\u00e9partements") %>% + mutate(Zone = iconv(Zone,to = "UTF-8"), + TypeZone = iconv(TypeZone,to = "UTF-8")) %>% + mutate(across(where(is.character),as.factor)) + + + + + + rm(liste_52) usethis::use_data(result, overwrite = TRUE, internal = FALSE) @@ -143,4 +156,6 @@ usethis::use_data(teruti, overwrite = TRUE, internal = FALSE) usethis::use_data(observatoire_artificialisation, overwrite = TRUE, internal = FALSE) usethis::use_data(observatoire_artificialisation_gk3, overwrite = TRUE, internal = FALSE) usethis::use_data(etalement_urbain, overwrite = TRUE, internal = FALSE) +usethis::use_data(population_legale, overwrite = TRUE, internal = FALSE) usethis::use_data(metadata_donnee, overwrite = TRUE, internal = FALSE) + diff --git a/data/population_legale.rda b/data/population_legale.rda new file mode 100644 index 0000000000000000000000000000000000000000..2fff93e5b317b65f1d700b9cb4d8fe260696d227 Binary files /dev/null and b/data/population_legale.rda differ diff --git a/data/result.rda b/data/result.rda index 69076b8413f0c5a32f292fde8db52ab677290742..ee6507087219ae9adb475f7249a4038cbce5c0dc 100644 Binary files a/data/result.rda and b/data/result.rda differ diff --git a/devstuff_history.R b/devstuff_history.R index b1de1e725d2488e9804d6ea04bc5589e9122f9ef..eaeb713f170917d6840fcf872ea67bf4281c530d 100644 --- a/devstuff_history.R +++ b/devstuff_history.R @@ -86,6 +86,11 @@ usethis::use_vignette("cb-ch3-2","cb- Chapitre 3 Carte 2") usethis::use_r("creer_carte_3_2") usethis::use_test("creer_carte_3_2") +##creer_graphe_3_4 +usethis::use_vignette("ce-ch3-4","ce- Chapitre 3 Graphe 4") +usethis::use_r("creer_graphe_3_4") +usethis::use_test("creer_graphe_3_4") + ## ajout news.md usethis::use_news_md() diff --git a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd index 8a1aaa2f489225317b1ee9874caaf9f0330f572d..8a0bf9cb685bf5583b871e72278aa7ddb03de6f2 100644 --- a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd @@ -11,8 +11,10 @@ params: millesime_obs_artif: 2019 millesime_etalement_urbain: 2018 millesime_obs_artif_gk3: 2019 + millesime_population: 2018 + code_reg: 52 -title: "L'artificialisation des sols en Pays de la loire en `r params$millesime_observatoire_artificialisation`" +title: "L'artificialisation des sols en Pays de la loire en `r params$millesime_obs_artif`" --- ```{r setup, include=FALSE} @@ -97,3 +99,7 @@ creer_graphe_2_5( params$millesime_obs_artif) creer_carte_3_2( params$millesime_etalement_urbain, police_annotation = "Marianne") ``` + +```{r graph surfaces artificialisees par habitant,fig.width=9,fig.height=7} +creer_graphe_3_4( params$millesime_obs_artif_gk3,params$millesime_obs_artif,params$millesime_population,params$code_reg) +``` diff --git a/man/creer_graphe_2_5.Rd b/man/creer_graphe_2_5.Rd index f51f9b82f32c9edc2c4065a26babc89a5219ccdb..61d50236563e87de56ad8a965ba353b521e346f7 100644 --- a/man/creer_graphe_2_5.Rd +++ b/man/creer_graphe_2_5.Rd @@ -7,7 +7,7 @@ creer_graphe_2_5(millesime_obs_artif) } \arguments{ -\item{millesime_obs_artif}{une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique.} +\item{millesime_obs_artif}{une année parmi les millésimes sélectionnables par l'utilisateur, au format numerique.} } \value{ cinq diagrammes en barres diff --git a/man/creer_graphe_3_4.Rd b/man/creer_graphe_3_4.Rd new file mode 100644 index 0000000000000000000000000000000000000000..57139a94d4d6cd5df7a457087eaa215f7d80efbd --- /dev/null +++ b/man/creer_graphe_3_4.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/creer_graphe_3_4.R +\name{creer_graphe_3_4} +\alias{creer_graphe_3_4} +\title{Creation du graphique en barres par departement des surfaces artificialisees pour un nouvel habitant.} +\usage{ +creer_graphe_3_4( + millesime_obs_artif_gk3 = NULL, + millesime_obs_artif = NULL, + millesime_population = NULL, + code_reg = NULL +) +} +\arguments{ +\item{millesime_obs_artif_gk3}{une annee parmi les millesimes selectionnables par l'utilisateur, au format numerique.} + +\item{millesime_obs_artif}{une annee parmi les millesimes selectionnables par l'utilisateur, au format numerique.} + +\item{millesime_population}{une annee parmi les millesimes selectionnables par l'utilisateur, au format numerique.} + +\item{code_reg}{code insee de la région sur laquelle construire le graphique} +} +\value{ +Un graphique en barres +} +\description{ +Graphique en barres par departement des surfaces artificialisees pour un nouvel habitant. +} +\examples{ +creer_graphe_3_4(millesime_obs_artif_gk3 = 2019,millesime_obs_artif = 2019, +millesime_population = 2018, code_reg = '52') +} diff --git a/man/population_legale.Rd b/man/population_legale.Rd new file mode 100644 index 0000000000000000000000000000000000000000..0b066fcd00a82fe605f1838cfbd00bdbc7346481 --- /dev/null +++ b/man/population_legale.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\encoding{UTF-8} +\name{population_legale} +\alias{population_legale} +\title{Table contenant les différents indicateurs de la source population_legale utiles pour la publication.} +\format{ +Table de 25704 lignes et 5 colonnes: +\describe{ +\item{TypeZone}{Type de territoire} +\item{CodeZone}{Code du territoire} +\item{Zone}{Libellé du territoire} +\item{date}{Millésime de la source} +\item{population_municipale}{population minicipale} +} +} +\source{ +\url{https://agreste.agriculture.gouv.fr/agreste-web/disaron/W0020/detail/} +} +\usage{ +population_legale +} +\description{ +Table contenant les différents indicateurs de la source population_legale utiles pour la publication. +} +\keyword{datasets} diff --git a/tests/testthat/test-creer_graphe_3_4.R b/tests/testthat/test-creer_graphe_3_4.R new file mode 100644 index 0000000000000000000000000000000000000000..b237b755c8126f9c13eb3ee4f63dc7e0c5efe158 --- /dev/null +++ b/tests/testthat/test-creer_graphe_3_4.R @@ -0,0 +1,7 @@ +test_that("creer_graphe_3_4 fonctionne", { + + # Test que le graphe est un ggplot + objet <- creer_graphe_3_4(millesime_obs_artif_gk3 = 2019,millesime_obs_artif = 2019,millesime_population = 2018) + testthat::expect_equal(attr(objet, "class"), c("gg","ggplot")) + +}) diff --git a/vignettes/ce-ch3-4.Rmd b/vignettes/ce-ch3-4.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..4904a9bfc7d0492ee3e370f3eb870ce721e91fb6 --- /dev/null +++ b/vignettes/ce-ch3-4.Rmd @@ -0,0 +1,30 @@ +--- +title: "ce- Chapitre 3 Graphe 4" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{ce- Chapitre 3 Graphe 4} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + echo = FALSE, + message = FALSE, + warning = FALSE, + error = FALSE, + comment = "#>" +) + +``` + +# Descriptif +La fonction `creer_graphe_3_4()` produit le graphique en barres par departement des surfaces artificialisees pour un nouvel habitant. + +```{r viz, fig.height=10,fig.width=5} +library(propre.artificialisation) +library(gouvdown) +ggplot2::theme_set(gouvdown::theme_gouv(plot_title_size = 14, subtitle_size = 12, base_size = 10, caption_size = 10,strip_text_size = 10)) +creer_graphe_3_4(millesime_obs_artif_gk3 = 2019,millesime_obs_artif = 2019,millesime_population = 2018, code_reg = 52) +``` diff --git a/vignettes/test.R b/vignettes/test.R index 94481ce394b84992697dabff20c3bccdc80ef56f..a7f1604d719efec4e426c7b41154920654ef8731 100644 --- a/vignettes/test.R +++ b/vignettes/test.R @@ -1,56 +1,114 @@ -#' Creation de la carte regionale de l evolution des surfaces cadastrees -#' @description carte regionale de l evolution des surfaces cadastrees +#' Creation du graphique en barres par departement des surfaces artificialisees pour un nouvel habitant. +#' @description Graphique en barres par departement des surfaces artificialisees pour un nouvel habitant. #' +#' @param millesime_obs_artif_gk3 une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. +#' @param millesime_population une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. #' @param millesime_obs_artif une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. #' -#' @return une carte regionale +#' @return Un graphique en barres #' -#' @importFrom dplyr filter select mutate arrange -#' @importFrom forcats fct_drop fct_relevel -#' @importFrom ggplot2 ggplot geom_bar aes geom_text labs facet_wrap theme element_text scale_y_continuous -#' @importFrom gouvdown scale_fill_gouv_discrete +#' @importFrom dplyr mutate filter select group_by ungroup bind_rows summarise +#' @importFrom ggplot2 labs theme +#' @importFrom ggradar ggradar +#' @importFrom ggtext element_markdown #' @importFrom glue glue +#' @importFrom tidyr spread #' @importFrom lubridate year -#' @importFrom stringr str_wrap -#' @importFrom mapfactory format_fr #' #' @export #' #' @examples -#' creer_carte_2_7(millesime_obs_artif = 2019) -creer_carte_2_7 <- function(millesime_obs_artif) { +#' creer_graphe_3_4(millesime_obs_artif_gk3 = 2019,millesime_obs_artif = 2019,millesime_population = 2018) - # calcul des millesimes de la carte - millesime_debut <- millesime_obs_artif - 10 - millesime_fin <- millesime_obs_artif - 1 +creer_graphe_3_4 <- function(millesime_obs_artif_gk3,millesime_obs_artif,millesime_population){ - # Creation de la table utile a la production du graphique - data <- observatoire_artificialisation %>% + # # calcul des millesimes utilises + millesime_debut_flux <- millesime_obs_artif - 10 + millesime_fin_flux <- millesime_obs_artif - 1 + millesime_depart_gk3 <- millesime_obs_artif_gk3 - 9 + millesime_debut_population <- millesime_population - 10 + millesime_fin_population <- millesime_population + + # preparation donnees du stock + stock_artif_gk3 <- observatoire_artificialisation_gk3 %>% + dplyr::mutate(date=lubridate::year(.data$date)) %>% + dplyr::filter((.data$TypeZone == "D\u00e9partements" & .data$CodeZone %in% c("44","49","53","72","85")) | + (.data$TypeZone == "R\u00e9gions" & .data$CodeZone =="52"), + .data$date == millesime_obs_artif_gk3 - 9) %>% + dplyr::mutate(surface_artificialisee = .data$surface_artificialisee) %>% + dplyr::select (-.data$surface_naf,-.data$date) %>% + dplyr::rename ("stock_gk3_depart"="surface_artificialisee") + + # preparation des donnees observatoires artificialisation +obs_artif <- observatoire_artificialisation %>% dplyr::mutate(date = as.numeric(lubridate::year(.data$date))) %>% - dplyr::filter(.data$TypeZone == "Communes", + dplyr::filter((.data$TypeZone == "D\u00e9partements" & .data$CodeZone %in% c("44","49","53","72","85")) | + (.data$TypeZone == "R\u00e9gions" & .data$CodeZone =="52"), .data$date <= millesime_obs_artif, .data$date >= millesime_obs_artif - 9) %>% - dplyr::mutate(flux_naf_artificialisation_total = .data$flux_naf_artificialisation_total / 10000) %>% + dplyr::mutate(flux_naf_artificialisation_total = .data$flux_naf_artificialisation_total) %>% dplyr::group_by(.data$CodeZone, .data$TypeZone, .data$Zone) %>% - dplyr::summarise(valeur = sum(.data$flux_naf_artificialisation_total, na.rm = T)) - - - fond_carto <- mapfactory::fond_carto(nom_reg = "Pays de la Loire") - bins <- stats::quantile(data$valeur,probs = c(0,0.1, 0.25, 0.5,0.75,0.9,1),na.rm=TRUE) - - carte_2_7 <- mapfactory::creer_carte_communes(data = data, - code_region=52, - carto = fond_carto, - indicateur = valeur, - bornes = bins, - decimales = 0, - titre = glue::glue("Evolution des surfaces cadastr\u00e9es\n entre {millesime_debut} et {millesime_fin}"), - sous_titre = NULL, - bas_de_page = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}"), - suffixe = "ha" + dplyr::summarise(somme_artif_10ans = sum(.data$flux_naf_artificialisation_total, na.rm = T)) %>% + dplyr::ungroup() + +# preparation des donnees population +evol_popul <- population_legale %>% + dplyr::mutate(date=lubridate::year(.data$date)) %>% + dplyr::filter((.data$TypeZone == "D\u00e9partements" & .data$CodeZone %in% c("44","49","53","72","85")) | + (.data$TypeZone == "R\u00e9gions" & .data$CodeZone =="52"), + .data$date == millesime_debut_population | .data$date == millesime_fin_population) +popul_debut <- evol_popul %>% + dplyr::filter(.data$date == millesime_debut_population) %>% + dplyr::rename ("population_debut"="population_municipale") %>% + dplyr::select (-.data$date) +evol_popul <- evol_popul %>% + dplyr::group_by(.data$TypeZone,.data$Zone,.data$CodeZone) %>% + dplyr::summarise(evolution_population=((sum(.data$population_municipale[which(date==millesime_fin_population)],na.rm=T) - sum(.data$population_municipale[which(date==millesime_debut_population)],na.rm=T))-1)) %>% + dplyr::ungroup() + +data <- stock_artif_gk3 %>% + dplyr::left_join(obs_artif) %>% + dplyr::left_join(evol_popul) %>% + dplyr::left_join(popul_debut) %>% + dplyr::mutate(surf_artif_par_hab_an_x=.data$stock_gk3_depart / .data$population_debut ) %>% + dplyr::mutate(surf_artif_par_nouv_hab_entre_x_y =.data$somme_artif_10ans / .data$evolution_population ) %>% + dplyr::select (.data$Zone,surf_artif_par_hab_an_x,surf_artif_par_nouv_hab_entre_x_y) %>% + tidyr::gather(variable, valeur, 2:3) %>% + dplyr::mutate(Zone=forcats::fct_relevel(Zone,"Pays de la Loire","Vendée","Sarthe","Mayenne","Maine-et-Loire","Loire-Atlantique")) %>% + dplyr::mutate(variable=forcats::fct_relevel(variable,"surf_artif_par_nouv_hab_entre_x_y","surf_artif_par_hab_an_x")) %>% + dplyr::arrange(Zone) + + + + + + # dplyr::mutate( + # variable = replace(.data$variable, .data$variable == "flux_naf_artificialisation_activite", "activit\u00e9"), + # variable = replace(.data$variable, .data$variable == "flux_naf_artificialisation_habitation", "habitation"), + # variable = replace(.data$variable, .data$variable == "flux_naf_artificialisation_mixte", "mixte") + # ) + +# creation du graphique +valeur_max_graphique <- max(data$valeur,na.rm=T)+200 + +graph_3_4 <- data %>% ggplot2::ggplot(ggplot2::aes(x=Zone,y=valeur,fill=variable), width = 0.9) + + ggplot2::geom_bar(stat="identity",position = ggplot2::position_dodge())+ + ggplot2::geom_text(ggplot2::aes(y=.data$valeur, ymax=valeur_max_graphique, label=round(.data$valeur,0)),position= ggplot2::position_dodge(width=1), hjust=-0.2)+ + ggplot2::coord_flip() + + gouvdown::scale_fill_gouv_discrete(palette = "pal_gouv_qual2") + + ggplot2::labs( + title = glue::glue("Surfaces cadastr\u00e9es artificialis\u00e9es (m2)", width = 60), + subtitle = glue::glue("<span style = 'color:#7D4E5B'>par habitant présent en {millesime_debut_population}</span> et <span style = 'color:#169B62'>par nouvel habitant entre {millesime_debut_population} et {millesime_fin_population}</span> "), + #subtitle = "<span style = 'color:#169B62'> par habitant pr\u00e9sent en </span><br><span style = 'color:#7D4E5B'> par nouvel habitant entre </span>", + x = "", y = "", + fill = "", + caption = glue::glue("Source : DGFip/Cerema - Insee - {millesime_obs_artif}") + ) + + ggplot2::theme( + legend.position = "none", + plot.subtitle = ggtext::element_markdown(size = 12, lineheight = 1.2) ) + return(graph_3_4) - return(carte_2_7) } -