From 2ab8c320d6fb290ab3b7cad821e9d40f57b75d80 Mon Sep 17 00:00:00 2001 From: Daniel Kalioudjoglou <daniel.kalioudjoglou@developpement-durable.gouv.fr> Date: Fri, 28 May 2021 08:44:14 +0200 Subject: [PATCH 1/3] realisation du graphique 2_5 ticket 37 --- DESCRIPTION | 3 +- NAMESPACE | 2 + R/creer_graphe_2_5.R | 61 ++++ R/globals.R | 2 +- devstuff_history.R | 5 + .../publication/skeleton/skeleton.Rmd | 6 + man/creer_graphe_2_5.Rd | 20 ++ tests/testthat/test-creer_graphe_2_5.R | 7 + vignettes/be-ch2-5.Rmd | 30 ++ vignettes/test.R | 282 ++++++------------ 10 files changed, 216 insertions(+), 202 deletions(-) create mode 100644 R/creer_graphe_2_5.R create mode 100644 man/creer_graphe_2_5.Rd create mode 100644 tests/testthat/test-creer_graphe_2_5.R create mode 100644 vignettes/be-ch2-5.Rmd diff --git a/DESCRIPTION b/DESCRIPTION index 6c4c1d7..53da080 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -38,8 +38,7 @@ Imports: stringr, tidyr, tricky, - utils, - sf + utils Suggests: knitr, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index 624b64a..81095c9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(creer_graphe_1_1) export(creer_graphe_1_4) export(creer_graphe_1_5) export(creer_graphe_2_4) +export(creer_graphe_2_5) export(format_fr_pct) importFrom(attempt,stop_if_not) importFrom(dplyr,arrange) @@ -44,6 +45,7 @@ importFrom(ggplot2,geom_sf) importFrom(ggplot2,geom_text) importFrom(ggplot2,ggplot) importFrom(ggplot2,labs) +importFrom(ggplot2,position_dodge) importFrom(ggplot2,scale_fill_manual) importFrom(ggplot2,scale_size_area) importFrom(ggplot2,scale_x_discrete) diff --git a/R/creer_graphe_2_5.R b/R/creer_graphe_2_5.R new file mode 100644 index 0000000..f133868 --- /dev/null +++ b/R/creer_graphe_2_5.R @@ -0,0 +1,61 @@ +#' Creation du graphique de l artificialisation par destination en ha sur 10 ans +#' @description Graphique de l artificialisation par destination en ha sur 10 ans +#' +#' @param millesime_obs_artif une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. +#' +#' @return cinq diagrammes en barres +#' +#' @importFrom gouvdown scale_fill_gouv_discrete +#' @importFrom dplyr filter select mutate arrange +#' @importFrom forcats fct_drop fct_relevel +#' @importFrom ggplot2 ggplot geom_bar aes position_dodge labs facet_wrap theme +#' @importFrom glue glue +#' @importFrom lubridate year +#' @importFrom tidyr gather +#' +#' @export +#' +#' @examples +#' creer_graphe_2_5(millesime_obs_artif = 2019) + +creer_graphe_2_5 <- function(millesime_obs_artif) { + + # calcul des millesimes extremes du graphique + 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::filter(.data$TypeZone == "D\u00e9partements", .data$CodeZone %in% c("44","49","53","72","85")) %>% + dplyr::select(.data$Zone, .data$date, .data$flux_naf_artificialisation_activite, .data$flux_naf_artificialisation_habitation, .data$flux_naf_artificialisation_mixte) %>% + dplyr::mutate( + Zone = forcats::fct_drop(.data$Zone) %>% + forcats::fct_relevel(c("Loire-Atlantique", "Maine-et-Loire", "Mayenne", "Sarthe", "Vend\u00e9e")) + ) %>% + dplyr::arrange(.data$Zone) %>% + dplyr::mutate(date = as.character(lubridate::year(.data$date - 1))) %>% + dplyr::filter(.data$date < millesime_obs_artif, .data$date > millesime_obs_artif - 11) %>% # conserve les 10 derniers millesimes + tidyr::gather(variable,valeur,.data$flux_naf_artificialisation_activite : .data$flux_naf_artificialisation_mixte) %>% + dplyr::mutate(valeur = .data$valeur / 10000) %>% + + 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 + graph_2_5 <- data %>% + ggplot2::ggplot() + + ggplot2::geom_bar(ggplot2::aes(x = .data$date, y = .data$valeur, fill = .data$variable), + width = 0.6, stat = "identity", position=ggplot2::position_dodge() + ) + + ggplot2::labs( + title = glue::glue("Artificialisation par destination, par d\u00e9partement de {millesime_debut} \u00e0 {millesime_fin}", width = 60), + x = "", y = "", + fill = "", + caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}") + ) + + ggplot2::facet_wrap(Zone ~ ., ncol = 1) + + ggplot2::theme(legend.position = "right") + + return(graph_2_5) +} diff --git a/R/globals.R b/R/globals.R index 4fe0d75..d0aa799 100644 --- a/R/globals.R +++ b/R/globals.R @@ -1,4 +1,4 @@ utils::globalVariables( - c("teruti","result","observatoire_artificialisation",".data","variable", + c("teruti","result","observatoire_artificialisation",".data","variable",".", "valeur","etalement_urbain") ) diff --git a/devstuff_history.R b/devstuff_history.R index bb1d14d..27ef9ae 100644 --- a/devstuff_history.R +++ b/devstuff_history.R @@ -65,6 +65,11 @@ usethis::use_vignette("bd-ch2-4","bd- Chapitre 2 Graphe 4") usethis::use_r("creer_graphe_2_4") usethis::use_test("creer_graphe_2_4") +##creer_graphe_2_5 +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_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 6d18f1b..ca3554b 100644 --- a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd @@ -77,6 +77,12 @@ creer_carte_2_2( params$millesime_obs_artif) ```{r graph conso espace departements 10 ans,fig.width=5,fig.height=7} creer_graphe_2_4( params$millesime_obs_artif) ``` + +```{r graph artificialisation par destination departements 10 ans,fig.width=9,fig.height=7} +creer_graphe_2_5( params$millesime_obs_artif) +``` + + ## Chapitre 3 ```{r carte communale de l etalement urbain,fig.width=9,fig.height=7} diff --git a/man/creer_graphe_2_5.Rd b/man/creer_graphe_2_5.Rd new file mode 100644 index 0000000..f51f9b8 --- /dev/null +++ b/man/creer_graphe_2_5.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/creer_graphe_2_5.R +\name{creer_graphe_2_5} +\alias{creer_graphe_2_5} +\title{Creation du graphique de l artificialisation par destination en ha sur 10 ans} +\usage{ +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.} +} +\value{ +cinq diagrammes en barres +} +\description{ +Graphique de l artificialisation par destination en ha sur 10 ans +} +\examples{ +creer_graphe_2_5(millesime_obs_artif = 2019) +} diff --git a/tests/testthat/test-creer_graphe_2_5.R b/tests/testthat/test-creer_graphe_2_5.R new file mode 100644 index 0000000..20d04c8 --- /dev/null +++ b/tests/testthat/test-creer_graphe_2_5.R @@ -0,0 +1,7 @@ +test_that("creer_graphe_2_5 fonctionne", { + + # Test que le graphe est un ggplot + objet <- creer_graphe_2_5(millesime_obs_artif=2019) + testthat::expect_equal(attr(objet, "class"), c("gg","ggplot")) + +}) diff --git a/vignettes/be-ch2-5.Rmd b/vignettes/be-ch2-5.Rmd new file mode 100644 index 0000000..7ac7cea --- /dev/null +++ b/vignettes/be-ch2-5.Rmd @@ -0,0 +1,30 @@ +--- +title: "bd- Chapitre 2 Graphe 5" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{bd- Chapitre 2 Graphe 5} + %\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_2_5()` produit le graphique de l artificialisation par destination en ha sur 10 ans + +```{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_2_5(millesime_obs_artif=2019) +``` diff --git a/vignettes/test.R b/vignettes/test.R index 76da186..e4e7ad5 100644 --- a/vignettes/test.R +++ b/vignettes/test.R @@ -1,66 +1,27 @@ -#' Creation de la carte de l evolution de l artificialisation sur 10 ans en ha par departement -#' @description Carte de l evolution de l artificialisation sur 10 ans en ha par departement +#' Creation du graphique de l artificialisation par destination en ha sur 10 ans +#' @description Graphique de l artificialisation par destination en ha sur 10 ans #' #' @param millesime_obs_artif une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. #' -#' @return Une carte +#' @return cinq diagrammes en barres #' +#' @importFrom gouvdown scale_fill_gouv_discrete #' @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 ggplot2 ggplot geom_bar aes position_dodge labs facet_wrap theme #' @importFrom glue glue #' @importFrom lubridate year -#' @importFrom stringr str_wrap -#' @importFrom mapfactory format_fr +#' @importFrom tidyr gather #' #' @export #' #' @examples -#' creer_carte_2_2(millesime_obs_artif = 2019) - -millesime_etalement_urbain=2018 - - data <- etalement_urbain %>% - dplyr::filter(.data$TypeZone == "Communes", - .data$date == lubridate::make_date(millesime_etalement_urbain,"01","01"))%>% - dplyr::mutate(indicateur_etalement_urbain = forcats::fct_rev(.data$indicateur_etalement_urbain)) - - fond_carte <- mapfactory::fond_carto(nom_reg = "Pays de la Loire") - - localisation_classe6 <- fond_carte$communes %>% - dplyr::filter(DEPCOM=="53125") %>% - sf::st_centroid() %>% - sf::st_geometry() %>% - sf::st_coordinates() - - localisation_classe2a <- fond_carte$communes %>% - dplyr::filter(DEPCOM=="44072") %>% - sf::st_point_on_surface() %>% - sf::st_geometry() %>% - sf::st_coordinates() - - # # reflexions sur automatisation du choix communes de référence - # ideal_class3_x <- 390000 #coordonnées du point ideal - # ideal_class3_y <- 6630000 - # choix_class3 <- data %>% - # dplyr::filter(indicateur_etalement_urbain == "classe 3") #on ne conserve que les class 3 - # # ajouter dans cette table une colonne avec les coordonnéesdu centre de la commune - # #et une autre avec celles du point ideal - # # creer une colonne avec une fonction qui calcule la distance entre les 2 colonnes précedentes - # # prendre la valeur la plus faible de cette colonne et extraire le code commune - # # utiliser ce code dans localisation_class_3 - - localisation_classe3 <- fond_carte$communes %>% - dplyr::filter(DEPCOM=="85289") %>% - sf::st_point_on_surface() %>% - sf::st_geometry() %>% - sf::st_coordinates() - -millesime_obs_artif=2019 -library(COGiter) - -creer_graphe_2_2 <- function(millesime_obs_artif) { +#' creer_graphe_2_5(millesime_obs_artif = 2019) + +creer_graphe_2_5 <- function(millesime_obs_artif) { + + + # calcul des millesimes extremes du graphique millesime_debut <- millesime_obs_artif - 10 @@ -68,156 +29,79 @@ creer_graphe_2_2 <- function(millesime_obs_artif) { # Creation de la table utile a la production du graphique data <- observatoire_artificialisation %>% - dplyr::filter(.data$TypeZone == "D\u00e9partements") %>% - dplyr::filter( !(.data$CodeZone %in% c("971","972","973","974","975","976") )) %>% - dplyr::select(.data$CodeZone,.data$TypeZone, .data$Zone, .data$date, .data$flux_naf_artificialisation_total) %>% - dplyr::mutate(flux_naf_artificialisation_total = .data$flux_naf_artificialisation_total / 10000) %>% + dplyr::filter(.data$TypeZone == "D\u00e9partements", .data$CodeZone %in% c("44","49","53","72","85")) %>% + dplyr::select(.data$Zone, .data$date, .data$flux_naf_artificialisation_activite, .data$flux_naf_artificialisation_habitation, .data$flux_naf_artificialisation_mixte) %>% + dplyr::mutate( + Zone = forcats::fct_drop(.data$Zone) %>% + forcats::fct_relevel(c("Loire-Atlantique", "Maine-et-Loire", "Mayenne", "Sarthe", "Vend\u00e9e")) + ) %>% + dplyr::arrange(.data$Zone) %>% dplyr::mutate(date = as.character(lubridate::year(.data$date - 1))) %>% dplyr::filter(.data$date < millesime_obs_artif, .data$date > millesime_obs_artif - 11) %>% # conserve les 10 derniers millesimes - dplyr::arrange(.data$Zone) %>% - dplyr::group_by(.data$CodeZone,.data$TypeZone, .data$Zone) %>% - dplyr::summarise(`total en hectares`= sum(.data$flux_naf_artificialisation_total,na.rm=T)) %>% - dplyr::left_join(COGiter::departements_geo,by=c("CodeZone"="DEP")) - data <- sf::st_as_sf(data) - - # creation de la carte - monde_file <- system.file("maps","countries_voisins-10m.gpkg",package = "mapfactory") - monde <- sf::read_sf(monde_file) %>% - dplyr::select(.data$name) - - bbox_reg <- sf::st_bbox(sf::st_buffer(data,50000)) - - y_min = bbox_reg$ymin - y_max = bbox_reg$ymax - x_min = bbox_reg$xmin - x_max = bbox_reg$xmax - - carte_2_2 <- ggplot2::ggplot(data)+ - ggplot2::geom_sf(data = monde,fill="light grey")+ - ggplot2::geom_sf(fill="white")+ - ggplot2::stat_sf_coordinates(ggplot2::aes(size=.data$`total en hectares`, - fill=.data$`total en hectares`), - color="black", - shape=21, - alpha = 0.8)+ - ggplot2::coord_sf( - xlim = c(x_min, x_max), - ylim = c(y_min, y_max), - expand = FALSE, - crs = sf::st_crs(data), - datum = NA - )+ - ggplot2::theme( - panel.background = ggplot2::element_rect(fill = "light blue"))+ - gouvdown::scale_fill_gouv_continuous(palette = "pal_gouv_o", reverse = TRUE, name="")+ - # ggplot2::scale_size_area(name="")+ - # ggplot2::scale_size(range = c(0,15)) - ggplot2::scale_size_continuous(range = c(0,20))+ - ggplot2::labs(title=glue::glue("Surfaces artificialis\u00e9es de {millesime_debut} \u00e0 {millesime_fin}"), - subtitle="", - y="", - x="", - caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}")) - - - - - - - - - - return(carte_2_2) -} - - localisation_classe5 <- fond_carte$communes %>% - dplyr::filter(DEPCOM=="49224") %>% - sf::st_point_on_surface() %>% - sf::st_geometry() %>% - sf::st_coordinates() - - carte_3_2 <- mapfactory::creer_carte_categorie_communes(data = data, - code_region=52, - carto = fond_carte, - palette = "pal_gouv_o", - inverse = FALSE, - indicateur = .data$indicateur_etalement_urbain, - titre = glue::glue("Etalement urbain en Pays de la Loire par commune \n au 1er janvier {millesime_etalement_urbain}"), - bas_de_page = glue::glue("Source : {millesime_etalement_urbain}"), - suffixe = NULL - ) + - ggplot2::annotate("curve", - x = 350000, - y=6818000, - xend = localisation_classe6[1], - yend = localisation_classe6[2], - color = "#D66C58", - arrow = ggplot2::arrow(length = ggplot2::unit(0.05, "inches")) + tidyr::gather(variable,valeur,.data$flux_naf_artificialisation_activite : .data$flux_naf_artificialisation_mixte) %>% + dplyr::mutate(valeur = .data$valeur / 10000) %>% + + 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 + graph_2_5 <- data %>% + ggplot2::ggplot() + + ggplot2::geom_bar(ggplot2::aes(x = .data$date, y = .data$valeur, fill = .data$variable), + width = 0.6, stat = "identity", position=ggplot2::position_dodge() ) + - ggplot2::annotate("label", - x = 350000, - y=6818000, - label="classe 6 :\nTerritoires pour lesquels \nla population décroit et \nl'artificialisation progresse", - label.r = ggplot2::unit(0,"lines"), - size = 3, - family = "Marianne", - fill = "#D66C58", - color = "white") + - ggplot2::annotate("curve", - x = 320000, - y=6760000, - xend = localisation_classe2a[1], - yend = localisation_classe2a[2], - color = "black", - arrow = ggplot2::arrow(length = ggplot2::unit(0.05, "inches")) + # ggplot2::geom_text(ggplot2::aes(x = .data$date, y = .data$variable - 120, label = mapfactory::format_fr(.data$variable, 0), group = .data$Zone), color = "white", size = 3) + + ggplot2::labs( + title = glue::glue("Artificialisation par destination, par d\u00e9partement de {millesime_debut} \u00e0 {millesime_fin}", width = 60), + x = "", y = "", + fill = "", + caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}") ) + - ggplot2::annotate("label", - x = 320000, - y=6760000, - label="classe 2a :\nTerritoires pour lesquels \nla population croit moins vite \nque l'artificialisation", - label.r = ggplot2::unit(0,"lines"), - size = 3, - family = "Marianne", - fill = "#FFF2F1", - color = "black") + - - ggplot2::annotate("curve", - x = 470000, - y=6603000, - xend = localisation_classe3[1], - yend = localisation_classe3[2], - color = "black", - arrow = ggplot2::arrow(length = ggplot2::unit(0.05, "inches")) - ) + - ggplot2::annotate("label", - x = 470000, - y=6603000, - label="classe 3 :\nTerritoires pour lesquels \nla population croit moins vite \nque l'artificialisation", - label.r = ggplot2::unit(0,"lines"), - size = 3, - family = "Marianne", - fill = "#FFC9C2", - color = "black") + - ggplot2::annotate("curve", - x = 500000, - y=6643000, - xend = localisation_classe5[1], - yend = localisation_classe5[2], - color = "black", - arrow = ggplot2::arrow(length = ggplot2::unit(0.05, "inches")) - ) + - ggplot2::annotate("label", - x = 500000, - y=6643000, - label="classe 5 :\nTerritoires pour lesquels \nla population croit moins vite \nque l'artificialisation", - label.r = ggplot2::unit(0,"lines"), - size = 3, - family = "Marianne", - fill = "#F66E4E", - color = "black") - - - - return(carte_3_2) - + ggplot2::facet_wrap(Zone ~ ., ncol = 1) + + # gouvdown::scale_fill_gouv_discrete(palette = "pal_gouv_qual2") + + ggplot2::theme(legend.position = "right") + + + + + + + # data %>% + # ggplot2::ggplot() + + # ggplot2::geom_bar(ggplot2::aes(x = .data$date, y = .data$valeur, fill = .data$variable), + # width = 0.6, stat = "identity", position=ggplot2::position_dodge() + # ) + + # # ggplot2::geom_text(ggplot2::aes(x = .data$date, y = .data$variable - 120, label = mapfactory::format_fr(.data$variable, 0), group = .data$Zone), color = "white", size = 3) + + # ggplot2::labs( + # title = glue::glue("Artificialisation par destination, par d\u00e9partement de {millesime_debut} \u00e0 {millesime_fin}", width = 60), + # x = "", y = "", + # fill = "", + # caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}") + # ) + + # ggplot2::facet_wrap(Zone ~ ., ncol = 3) + + # # gouvdown::scale_fill_gouv_discrete(palette = "pal_gouv_qual2") + + # ggplot2::theme(legend.position = "bottom") + + + # data %>% + # ggplot2::ggplot() + + # ggplot2::geom_bar(ggplot2::aes(x = .data$date, y = .data$valeur, fill = .data$variable), + # width = 0.6, stat = "identity", position=ggplot2::position_dodge() + # ) + + # # ggplot2::geom_text(ggplot2::aes(x = .data$date, y = .data$variable - 120, label = mapfactory::format_fr(.data$variable, 0), group = .data$Zone), color = "white", size = 3) + + # ggplot2::labs( + # title = glue::glue("Artificialisation par destination, par d\u00e9partement de {millesime_debut} \u00e0 {millesime_fin}", width = 60), + # x = "", y = "", + # fill = "", + # caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}") + # ) + + # ggplot2::facet_grid( Zone ~ .)+ + # # ggplot2::facet_wrap(Zone ~ ., ncol = 1) + + # # gouvdown::scale_fill_gouv_discrete(palette = "pal_gouv_qual2") + + # ggplot2::theme(legend.position = "right") + + + return(graph_2_5) } + -- GitLab From 96ac0450ddb4cdc96897463aa2135e7be14708e4 Mon Sep 17 00:00:00 2001 From: Daniel Kalioudjoglou <daniel.kalioudjoglou@developpement-durable.gouv.fr> Date: Fri, 28 May 2021 17:06:58 +0200 Subject: [PATCH 2/3] modification des couleurs --- R/creer_graphe_2_5.R | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/R/creer_graphe_2_5.R b/R/creer_graphe_2_5.R index f133868..1fe5cf8 100644 --- a/R/creer_graphe_2_5.R +++ b/R/creer_graphe_2_5.R @@ -43,19 +43,41 @@ creer_graphe_2_5 <- function(millesime_obs_artif) { variable = replace(.data$variable, .data$variable=="flux_naf_artificialisation_mixte","mixte")) # creation du graphique + + # graph_2_5 <- data %>% + # ggplot2::ggplot() + + # ggplot2::geom_bar(ggplot2::aes(x = .data$date, y = .data$valeur, fill = .data$variable), + # width = 0.6, stat = "identity", position=ggplot2::position_dodge() + # ) + + # ggplot2::labs( + # title = glue::glue("Artificialisation par destination, par d\u00e9partement de {millesime_debut} \u00e0 {millesime_fin}", width = 60), + # subtitle="<span style = 'color:#F8766D'> activit\u00e9</span>,<span style = 'color:#00BA38'> habitation</span> et <span style = 'color:#619CFF'> mixte</span>", + # x = "", y = "", + # fill = "", + # caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}") + # ) + + # ggplot2::facet_wrap(Zone ~ ., ncol = 1) + + # ggplot2::theme(legend.position = "none", + # plot.subtitle = ggtext::element_markdown(size = 12, lineheight = 1.2)) + graph_2_5 <- data %>% ggplot2::ggplot() + ggplot2::geom_bar(ggplot2::aes(x = .data$date, y = .data$valeur, fill = .data$variable), width = 0.6, stat = "identity", position=ggplot2::position_dodge() ) + + # ggplot2::geom_text(ggplot2::aes(x = .data$date, y = .data$variable - 120, label = mapfactory::format_fr(.data$variable, 0), group = .data$Zone), color = "white", size = 3) + ggplot2::labs( title = glue::glue("Artificialisation par destination, par d\u00e9partement de {millesime_debut} \u00e0 {millesime_fin}", width = 60), + subtitle="<span style = 'color:#169B62'> activit\u00e9</span>,<span style = 'color:#FF8D7E'> habitation</span> et <span style = 'color:#7D4E5B'> mixte</span>", x = "", y = "", fill = "", caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}") ) + - ggplot2::facet_wrap(Zone ~ ., ncol = 1) + - ggplot2::theme(legend.position = "right") + ggplot2::facet_grid( Zone ~ .)+ + # ggplot2::facet_wrap(Zone ~ ., ncol = 1) + + gouvdown::scale_fill_gouv_discrete(palette = "pal_gouv_qual2") + + ggplot2::theme(legend.position = "none", + plot.subtitle = ggtext::element_markdown(size = 12, lineheight = 1.2)) return(graph_2_5) } -- GitLab From 11ea61da05f07ef641ed9176f84ff6121215d414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Theuliere?= <mael.theuliere@developpement-durable.gouv.fr> Date: Mon, 31 May 2021 11:19:45 +0200 Subject: [PATCH 3/3] toilettage typo --- R/creer_graphe_2_5.R | 49 +++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/R/creer_graphe_2_5.R b/R/creer_graphe_2_5.R index 1fe5cf8..6b0c192 100644 --- a/R/creer_graphe_2_5.R +++ b/R/creer_graphe_2_5.R @@ -1,7 +1,7 @@ #' Creation du graphique de l artificialisation par destination en ha sur 10 ans #' @description Graphique de l artificialisation par destination en ha 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 une année parmi les millésimes sélectionnables par l'utilisateur, au format numerique. #' #' @return cinq diagrammes en barres #' @@ -17,7 +17,6 @@ #' #' @examples #' creer_graphe_2_5(millesime_obs_artif = 2019) - creer_graphe_2_5 <- function(millesime_obs_artif) { # calcul des millesimes extremes du graphique @@ -26,7 +25,7 @@ creer_graphe_2_5 <- function(millesime_obs_artif) { # Creation de la table utile a la production du graphique data <- observatoire_artificialisation %>% - dplyr::filter(.data$TypeZone == "D\u00e9partements", .data$CodeZone %in% c("44","49","53","72","85")) %>% + dplyr::filter(.data$TypeZone == "D\u00e9partements", .data$CodeZone %in% c("44", "49", "53", "72", "85")) %>% dplyr::select(.data$Zone, .data$date, .data$flux_naf_artificialisation_activite, .data$flux_naf_artificialisation_habitation, .data$flux_naf_artificialisation_mixte) %>% dplyr::mutate( Zone = forcats::fct_drop(.data$Zone) %>% @@ -34,50 +33,36 @@ creer_graphe_2_5 <- function(millesime_obs_artif) { ) %>% dplyr::arrange(.data$Zone) %>% dplyr::mutate(date = as.character(lubridate::year(.data$date - 1))) %>% - dplyr::filter(.data$date < millesime_obs_artif, .data$date > millesime_obs_artif - 11) %>% # conserve les 10 derniers millesimes - tidyr::gather(variable,valeur,.data$flux_naf_artificialisation_activite : .data$flux_naf_artificialisation_mixte) %>% + dplyr::filter(.data$date < millesime_obs_artif, .data$date > millesime_obs_artif - 11) %>% + # conserve les 10 derniers millesimes + tidyr::gather(variable, valeur, .data$flux_naf_artificialisation_activite:.data$flux_naf_artificialisation_mixte) %>% dplyr::mutate(valeur = .data$valeur / 10000) %>% - - 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")) + 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 - # graph_2_5 <- data %>% - # ggplot2::ggplot() + - # ggplot2::geom_bar(ggplot2::aes(x = .data$date, y = .data$valeur, fill = .data$variable), - # width = 0.6, stat = "identity", position=ggplot2::position_dodge() - # ) + - # ggplot2::labs( - # title = glue::glue("Artificialisation par destination, par d\u00e9partement de {millesime_debut} \u00e0 {millesime_fin}", width = 60), - # subtitle="<span style = 'color:#F8766D'> activit\u00e9</span>,<span style = 'color:#00BA38'> habitation</span> et <span style = 'color:#619CFF'> mixte</span>", - # x = "", y = "", - # fill = "", - # caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}") - # ) + - # ggplot2::facet_wrap(Zone ~ ., ncol = 1) + - # ggplot2::theme(legend.position = "none", - # plot.subtitle = ggtext::element_markdown(size = 12, lineheight = 1.2)) - graph_2_5 <- data %>% ggplot2::ggplot() + ggplot2::geom_bar(ggplot2::aes(x = .data$date, y = .data$valeur, fill = .data$variable), - width = 0.6, stat = "identity", position=ggplot2::position_dodge() + width = 0.6, stat = "identity", position = ggplot2::position_dodge() ) + - # ggplot2::geom_text(ggplot2::aes(x = .data$date, y = .data$variable - 120, label = mapfactory::format_fr(.data$variable, 0), group = .data$Zone), color = "white", size = 3) + ggplot2::labs( title = glue::glue("Artificialisation par destination, par d\u00e9partement de {millesime_debut} \u00e0 {millesime_fin}", width = 60), - subtitle="<span style = 'color:#169B62'> activit\u00e9</span>,<span style = 'color:#FF8D7E'> habitation</span> et <span style = 'color:#7D4E5B'> mixte</span>", + subtitle = "<span style = 'color:#169B62'> activit\u00e9</span>,<span style = 'color:#FF8D7E'> habitation</span> et <span style = 'color:#7D4E5B'> mixte</span>", x = "", y = "", fill = "", caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}") ) + - ggplot2::facet_grid( Zone ~ .)+ - # ggplot2::facet_wrap(Zone ~ ., ncol = 1) + + ggplot2::facet_grid(Zone ~ .) + gouvdown::scale_fill_gouv_discrete(palette = "pal_gouv_qual2") + - ggplot2::theme(legend.position = "none", - plot.subtitle = ggtext::element_markdown(size = 12, lineheight = 1.2)) + ggplot2::theme( + legend.position = "none", + plot.subtitle = ggtext::element_markdown(size = 12, lineheight = 1.2) + ) return(graph_2_5) } -- GitLab