diff --git a/DESCRIPTION b/DESCRIPTION index 53da080c34058e4156ac775d094f40ea8b699080..abcbb44797bf1582033df00eb6a2f8fc80d22fbd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,6 +26,7 @@ Imports: forcats, ggiraph, ggplot2, + ggradar, ggtext, glue, gouvdown (>= 0.0.0.9000), @@ -49,6 +50,7 @@ Remotes: gitlab::dreal-datalab/mapfactory, maeltheuliere/COGiter, pachevalier/tricky, + github::ricardo-bion/ggradar, spyrales/gouvdown Config/testthat/edition: 3 Encoding: UTF-8 diff --git a/NAMESPACE b/NAMESPACE index 48786492799f0a1684080fe90fb933856477cc99..267e93be1842ad9087e6d062a90d8d13fc6669a4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ export(creer_graphe_1_5) export(creer_graphe_2_3) export(creer_graphe_2_4) export(creer_graphe_2_5) +export(creer_graphe_3_1) export(format_fr_pct) importFrom(attempt,stop_if_not) importFrom(dplyr,arrange) @@ -19,7 +20,9 @@ importFrom(dplyr,case_when) importFrom(dplyr,desc) importFrom(dplyr,filter) importFrom(dplyr,first) +importFrom(dplyr,full_join) importFrom(dplyr,group_by) +importFrom(dplyr,lag) importFrom(dplyr,left_join) importFrom(dplyr,mutate) importFrom(dplyr,pull) @@ -38,16 +41,21 @@ importFrom(ggplot2,annotate) importFrom(ggplot2,arrow) importFrom(ggplot2,coord_flip) importFrom(ggplot2,coord_sf) +importFrom(ggplot2,element_blank) importFrom(ggplot2,element_rect) importFrom(ggplot2,element_text) importFrom(ggplot2,facet_wrap) importFrom(ggplot2,geom_bar) importFrom(ggplot2,geom_col) +importFrom(ggplot2,geom_label) +importFrom(ggplot2,geom_linerange) +importFrom(ggplot2,geom_point) importFrom(ggplot2,geom_sf) importFrom(ggplot2,geom_text) importFrom(ggplot2,ggplot) importFrom(ggplot2,labs) importFrom(ggplot2,position_dodge) +importFrom(ggplot2,scale_color_manual) importFrom(ggplot2,scale_fill_manual) importFrom(ggplot2,scale_size_area) importFrom(ggplot2,scale_x_discrete) @@ -67,6 +75,8 @@ importFrom(mapfactory,creer_carte_communes) importFrom(mapfactory,creer_carte_communes_prop) importFrom(mapfactory,fond_carto) importFrom(mapfactory,format_fr) +importFrom(mapfactory,format_fr_pct) +importFrom(scales,label_number) importFrom(scales,number_format) importFrom(sf,read_sf) importFrom(sf,st_bbox) @@ -79,5 +89,6 @@ importFrom(sf,st_point_on_surface) importFrom(stats,quantile) importFrom(stringr,str_wrap) importFrom(tidyr,gather) +importFrom(tidyr,pivot_longer) importFrom(tidyr,spread) importFrom(tricky,set_standard_names) diff --git a/NEWS.md b/NEWS.md index 3f86cc79621c02d8c69b8931eb2385306ecd9c5f..ddc6934c3aa66d5df125e84cb86a339c88750a13 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_1_7()`,`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_1()` 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_1.R b/R/creer_graphe_3_1.R new file mode 100644 index 0000000000000000000000000000000000000000..2dcf67c4e89623ef227e6a187a27662a43701b8d --- /dev/null +++ b/R/creer_graphe_3_1.R @@ -0,0 +1,97 @@ +#' Création du graphique lollipop de l'étalement urbain pour la région et le départements sur 10 ans. +#' @description Graphique lollipop de l'étalement urbain pour la région et les départements sur 10 ans. +#' +#' @param millesime_obs_artif_gk3 une année parmi les millésimes sélectionnables par l'utilisateur, au format numerique. +#' @param millesime_population une année parmi les millésimes sélectionnables par l'utilisateur, au format numerique. +#' @param code_reg code insee de la région sur laquelle construire le graphique +#' @param police police des titres et sous titre +#' +#' @return Un graphique lollipop +#' +#' @importFrom dplyr mutate filter select arrange group_by ungroup full_join lag +#' @importFrom forcats fct_drop fct_inorder +#' @importFrom ggplot2 ggplot aes geom_point position_dodge geom_linerange geom_label coord_flip scale_y_continuous labs theme element_blank element_text scale_color_manual +#' @importFrom ggtext element_markdown +#' @importFrom glue glue +#' @importFrom lubridate year +#' @importFrom mapfactory format_fr_pct +#' @importFrom scales label_number +#' @importFrom tidyr pivot_longer +#' @export +#' +#' @examples +#' creer_graphe_3_1(millesime_obs_artif_gk3 = 2019, millesime_population = 2018, code_reg = '52') +creer_graphe_3_1 <- function(millesime_obs_artif_gk3 = NULL, millesime_population = NULL, code_reg = NULL, police = "sans") { + millesime_debut <- millesime_obs_artif_gk3 - 9 + if (is.numeric(code_reg)) { + code_reg = as.character(code_reg) + } + # Creation de la table utile a la production du graphique + evol_artif <- observatoire_artificialisation_gk3 %>% + dplyr::mutate(date = lubridate::year(.data$date)) %>% + COGiter::filtrer_cog(reg = code_reg) %>% + dplyr::filter(.data$TypeZone %in% c("France","R\u00e9gions", "D\u00e9partements"), + .data$date == millesime_obs_artif_gk3 | .data$date == millesime_obs_artif_gk3 - 9) %>% + dplyr::select(-.data$surface_naf) %>% + dplyr::arrange(.data$TypeZone, .data$Zone, .data$CodeZone, .data$date) %>% + dplyr::group_by(.data$TypeZone, .data$Zone, .data$CodeZone) %>% + dplyr::mutate(evolution_artificialisation = round(.data$surface_artificialisee * 100 / dplyr::lag(.data$surface_artificialisee) - 100, 1)) %>% + dplyr::ungroup() %>% + dplyr::filter(.data$date == millesime_obs_artif_gk3) %>% + dplyr::select(.data$TypeZone, .data$CodeZone, .data$Zone, .data$evolution_artificialisation) + + + evol_popul <- population_legale %>% + dplyr::mutate(date = lubridate::year(.data$date)) %>% + COGiter::filtrer_cog(reg = code_reg) %>% + dplyr::filter(.data$TypeZone %in% c("R\u00e9gions", "D\u00e9partements"), + .data$date == millesime_population | .data$date == millesime_population - 9) %>% + dplyr::arrange(.data$TypeZone, .data$Zone, .data$CodeZone, .data$date) %>% + dplyr::group_by(.data$TypeZone, .data$Zone, .data$CodeZone) %>% + dplyr::mutate(evolution_population = round(.data$population_municipale * 100 / dplyr::lag(.data$population_municipale) - 100, 1)) %>% + dplyr::ungroup() %>% + dplyr::filter(.data$date == millesime_population) %>% + dplyr::select(.data$TypeZone, .data$CodeZone, .data$Zone, .data$evolution_population) + + + data <- dplyr::full_join(evol_artif, evol_popul) %>% + tidyr::pivot_longer(cols = .data$evolution_artificialisation:.data$evolution_population, names_to = "indicateur", values_to = "valeur") + gg <- data %>% + dplyr::arrange(.data$TypeZone, desc(.data$Zone)) %>% + dplyr::mutate(Zone = forcats::fct_drop(.data$Zone) %>% + forcats::fct_inorder()) %>% + ggplot2::ggplot() + + ggplot2::aes(x = .data$Zone, y = .data$valeur, group = .data$indicateur, color = .data$indicateur) + + ggplot2::geom_point(size = 2, position = ggplot2::position_dodge(width = 0.5)) + + ggplot2::aes(x = .data$Zone, ymin = 0, ymax = .data$valeur, group = .data$indicateur, color = .data$indicateur) + + ggplot2::geom_linerange(size = 1.2, position = ggplot2::position_dodge(width = 0.5)) + + ggplot2::aes(label = mapfactory::format_fr_pct(.data$valeur, dec = 1)) + + ggplot2::geom_label( + position = ggplot2::position_dodge(width = 0.5), + vjust = 0.5, hjust = -0.5 + ) + + ggplot2::coord_flip() + + ggplot2::scale_y_continuous( + breaks = seq(0, max(data$valeur) * 1.1, by = 2.5), + minor_breaks = NULL, + limits = c(0, max(data$valeur) * 1.1), + labels = scales::label_number(big.mark = " ", decimal.mark = ",", suffix = " %"), + expand = c(0,0) + ) + + ggplot2::labs( + title = "Taux de croissance compar\u00e9s des <span style = 'color:#FF9940'>surfaces artificialis\u00e9es</span> et de la <span style = 'color:#5770BE'>population municipale</span>", + subtitle = glue::glue("Entre {millesime_debut} et {millesime_obs_artif_gk3}, en %"), + caption = glue::glue("Source : Observatoire artificialisation {millesime_obs_artif_gk3} / Insee, calculs DREAL + Surfaces artificialis\u00e9es hors routes"), + x = NULL, y = NULL + ) + + ggplot2::theme( + panel.grid.major.y = ggplot2::element_blank(), + legend.position = "none", + plot.title = ggtext::element_markdown(size = 14, lineheight = 1.8, hjust = 0, family = police), + plot.subtitle = ggplot2::element_text(size = 12, family = police), + plot.caption = ggplot2::element_text(size = 10, family = police), + ) + + ggplot2::scale_color_manual(values = c("#FF9940", "#5770BE")) + return(gg) +} 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..510d2204d038b51172c6958797612541abf236cc 100644 --- a/data-raw/dataprep.R +++ b/data-raw/dataprep.R @@ -68,6 +68,11 @@ etalement_urbain<-dbReadTable(con_datamart, observatoire_artificialisation_gk3<-dbReadTable(con_datamart, 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") ocsge <- data_cogifiee %>% @@ -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, @@ -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,5 @@ 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..68b4e01eb430931b8b25529baabbef62037f2b1f 100644 --- a/devstuff_history.R +++ b/devstuff_history.R @@ -18,6 +18,7 @@ usethis::use_package("glue") usethis::use_package("lubridate") usethis::use_package("mapfactory") usethis::use_package("sf") +usethis::use_package("ggradar") usethis::use_dev_package("COGiter",remote = "maeltheuliere/COGiter") usethis::use_dev_package("gouvdown",remote = "spyrales/gouvdown") usethis::use_dev_package("mapfactory",remote = "gitlab::dreal-datalab/mapfactory") @@ -81,6 +82,11 @@ usethis::use_vignette("be-ch2-5","bd- Chapitre 2 Graphe 5") usethis::use_r("creer_graphe_2_5") usethis::use_test("creer_graphe_2_5") +##creer_graphe_3_1 +usethis::use_vignette("ca-ch3-1","ca- Chapitre 3 Graphe 1") +usethis::use_r("creer_graphe_3_1") +usethis::use_test("creer_graphe_3_1") + ##creer_carte_3_2 usethis::use_vignette("cb-ch3-2","cb- Chapitre 3 Carte 2") usethis::use_r("creer_carte_3_2") diff --git a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd index 8a1aaa2f489225317b1ee9874caaf9f0330f572d..123cd89ea4875ac67658f5fa52bebccd3f86d7fa 100644 --- a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd @@ -11,6 +11,7 @@ params: millesime_obs_artif: 2019 millesime_etalement_urbain: 2018 millesime_obs_artif_gk3: 2019 + millesime_population: 2018 title: "L'artificialisation des sols en Pays de la loire en `r params$millesime_observatoire_artificialisation`" --- @@ -93,6 +94,11 @@ creer_graphe_2_5( params$millesime_obs_artif) ## Chapitre 3 +```{r graph araignee etalement urbain par departements 10 ans,fig.width=9,fig.height=7} +creer_graphe_3_1( params$millesime_obs_artif, params$millesime_population) +``` + + ```{r carte communale de l etalement urbain,fig.width=9,fig.height=7} creer_carte_3_2( params$millesime_etalement_urbain, police_annotation = "Marianne") 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_1.Rd b/man/creer_graphe_3_1.Rd new file mode 100644 index 0000000000000000000000000000000000000000..229b201b4b762aa04013edc2fe66bdc2e7b08081 --- /dev/null +++ b/man/creer_graphe_3_1.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/creer_graphe_3_1.R +\name{creer_graphe_3_1} +\alias{creer_graphe_3_1} +\title{Création du graphique lollipop de l'étalement urbain pour la région et le départements sur 10 ans.} +\usage{ +creer_graphe_3_1( + millesime_obs_artif_gk3, + millesime_population, + code_reg, + police = "sans" +) +} +\arguments{ +\item{millesime_obs_artif_gk3}{une année parmi les millésimes sélectionnables par l'utilisateur, au format numerique.} + +\item{millesime_population}{une année parmi les millésimes sélectionnables par l'utilisateur, au format numerique.} + +\item{code_reg}{code insee de la région sur laquelle construire le graphique} + +\item{police}{police des titres et sous titre} +} +\value{ +Un graphique lollipop +} +\description{ +Graphique lollipop de l'étalement urbain pour la région et les départements sur 10 ans. +} +\examples{ +creer_graphe_3_1(millesime_obs_artif_gk3 = 2019, millesime_population = 2018) +} 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_1.R b/tests/testthat/test-creer_graphe_3_1.R new file mode 100644 index 0000000000000000000000000000000000000000..68b5baac4ba85602060cc18585afe676fa05525f --- /dev/null +++ b/tests/testthat/test-creer_graphe_3_1.R @@ -0,0 +1,7 @@ +test_that("creer_graphe_3_1 fonctionne", { + + # Test que le graphe est un ggplot + objet <- creer_graphe_3_1(millesime_obs_artif_gk3 = 2019,millesime_population = 2018, code_reg = '52') + testthat::expect_equal(attr(objet, "class"), c("gg","ggplot")) + +}) diff --git a/vignettes/ca-ch3-1.Rmd b/vignettes/ca-ch3-1.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..818e1d20a0b9b8e105da0df557f3d69e1ce77ba5 --- /dev/null +++ b/vignettes/ca-ch3-1.Rmd @@ -0,0 +1,33 @@ +--- +title: "ca- Chapitre 3 Graphe 1" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{ca- Chapitre 3 Graphe 1} + %\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_1()` produit le graphique de de l etalement urbain pour la région et les département sur 10 ans + + +```{r viz, fig.height=10,fig.width=10} +library(propre.artificialisation) +library(gouvdown) +creer_graphe_3_1(millesime_obs_artif_gk3 = 2019, + millesime_population = 2018, + code_reg = '52', + police = "Marianne") +``` diff --git a/vignettes/test.R b/vignettes/test.R index 94481ce394b84992697dabff20c3bccdc80ef56f..6d4e0101ea8b9072d9ee0f5950f4e3dadf3e45d8 100644 --- a/vignettes/test.R +++ b/vignettes/test.R @@ -1,56 +1,93 @@ -#' Creation de la carte regionale de l evolution des surfaces cadastrees -#' @description carte regionale de l evolution des surfaces cadastrees +#' Creation du graphique araignee de l etalement urbain par departement sur 10 ans. +#' @description Graphique araignee de l etalement urbain par departement sur 10 ans. #' -#' @param millesime_obs_artif une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. +#' @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. #' -#' @return une carte regionale +#' @return Un graphique araignee #' -#' @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 +#' @importFrom ggplot2 labs theme +#' @importFrom ggtext element_markdown #' @importFrom glue glue -#' @importFrom lubridate year -#' @importFrom stringr str_wrap -#' @importFrom mapfactory format_fr +#' @importFrom tidyr spread #' #' @export #' #' @examples -#' creer_carte_2_7(millesime_obs_artif = 2019) -creer_carte_2_7 <- function(millesime_obs_artif) { +#' creer_graphe_3_1(millesime_obs_artif_gk3 = 2019,millesime_population = 2018) + + +creer_graphe_3_1 <- function(millesime_obs_artif_gk3,millesime_population){ + + + + millesime_debut <- millesime_obs_artif_gk3 - 9 - # calcul des millesimes de la carte - millesime_debut <- millesime_obs_artif - 10 - millesime_fin <- millesime_obs_artif - 1 # Creation de la table utile a la production du graphique - data <- observatoire_artificialisation %>% - dplyr::mutate(date = as.numeric(lubridate::year(.data$date))) %>% - dplyr::filter(.data$TypeZone == "Communes", - .data$date <= millesime_obs_artif, - .data$date >= millesime_obs_artif - 9) %>% - dplyr::mutate(flux_naf_artificialisation_total = .data$flux_naf_artificialisation_total / 10000) %>% - 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" - ) - - - return(carte_2_7) -} + evol_artif <- observatoire_artificialisation_gk3 %>% + dplyr::mutate(date=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 | .data$date == millesime_obs_artif_gk3 - 9) %>% + dplyr::select (-.data$surface_naf) %>% + dplyr::group_by(.data$TypeZone,.data$Zone,.data$CodeZone) %>% + summarise(evolution_artificialisation=round(((sum(.data$surface_artificialisee[which(date==millesime_obs_artif_gk3)],na.rm=T) / sum(.data$surface_artificialisee[which(date==millesime_obs_artif_gk3-9)],na.rm=T))-1)*100,2)) %>% + dplyr::ungroup() %>% + dplyr::mutate(CodeZone= as.character(CodeZone), + CodeZone = replace(CodeZone, CodeZone=="52","Région")) %>% + dplyr::select (CodeZone,evolution_artificialisation) + + evol_artif <- evol_artif %>% + tidyr::spread(key=CodeZone,value=evolution_artificialisation,fill=0) + cat <- data.frame(x = "surfaces artificialisees (hors routes)") + evol_artif <- cbind(cat,evol_artif) + evol_popul <- population_legale %>% + dplyr::mutate(date=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_population | .data$date == millesime_population - 9) %>% + dplyr::group_by(.data$TypeZone,.data$Zone,.data$CodeZone) %>% + summarise(evolution_population=round(((sum(.data$population_municipale[which(date==millesime_population)],na.rm=T) / sum(.data$population_municipale[which(date==millesime_population-9)],na.rm=T))-1)*100,2)) %>% + dplyr::ungroup() %>% + dplyr::mutate(CodeZone= as.character(CodeZone), + CodeZone = replace(CodeZone, CodeZone=="52","Région")) %>% + dplyr::select (CodeZone,evolution_population) + + evol_popul <- evol_popul %>% + tidyr::spread(key=CodeZone,value=evolution_population,fill=0) + cat <- data.frame(x = "population municipale") + evol_popul <- cbind(cat,evol_popul) + + data <- bind_rows(evol_artif,evol_popul) + + graph_3_1<-ggradar::ggradar( + data, + base.size = 1, + values.radar = c("0%", "10%", "20%"), + grid.min = 0, grid.mid = 10, grid.max = 20, + group.line.width = 1, + group.point.size = 5, + background.circle.colour = "blue", + background.circle.transparency = 0.1, + group.colours = c("#00AFBB", "#E7B800"), + axis.line.colour = "gray60", + gridline.min.colour = "gray60", + gridline.mid.colour = "gray60", + gridline.max.colour = "gray60", + legend.position = "top" +)+ + ggplot2::labs( + title = glue::glue("Taux de croissance compar\u00e9s\n{millesime_debut}/{millesime_obs_artif_gk3} (%)"), + subtitle = "<span style = 'color:#00AFBB'>surfaces artificialis\u00e9es (hors routes)</span> et <span style = 'color:#E7B800'>population municipale</span>", + caption = glue::glue("Source : Observatoire artificialisation {millesime_obs_artif_gk3} / Insee"))+ + ggplot2::theme( + legend.position = "none", + plot.subtitle = ggtext::element_markdown(size = 16, lineheight = 1.8), + plot.caption = ggtext::element_markdown(size = 12, lineheight = 1.8)) + + return(graph_3_1) + +}