diff --git a/DESCRIPTION b/DESCRIPTION index c4c14e6ac3082afbba75d5e7f6c0e69bcc36f7b0..53da080c34058e4156ac775d094f40ea8b699080 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Depends: R (>= 2.10) Imports: attempt, + COGiter, dplyr, forcats, ggiraph, @@ -32,6 +33,7 @@ Imports: magrittr, mapfactory (>= 0.0.0.9000), scales, + sf, stats, stringr, tidyr, @@ -45,6 +47,7 @@ VignetteBuilder: knitr Remotes: gitlab::dreal-datalab/mapfactory, + maeltheuliere/COGiter, pachevalier/tricky, spyrales/gouvdown Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 6226ea47718611849f81d2f5fefccb6a84fc9a2b..b4d84c2bb7dec9412822e63b8494fa6d4a659129 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export("%>%") export(creer_carte_1_3) export(creer_carte_1_7) +export(creer_carte_2_2) export(creer_graphe_1_1) export(creer_graphe_1_4) export(creer_graphe_1_5) @@ -15,6 +16,7 @@ importFrom(dplyr,desc) importFrom(dplyr,filter) importFrom(dplyr,first) importFrom(dplyr,group_by) +importFrom(dplyr,left_join) importFrom(dplyr,mutate) importFrom(dplyr,pull) importFrom(dplyr,select) @@ -28,19 +30,25 @@ importFrom(ggiraph,geom_point_interactive) importFrom(ggiraph,ggiraph) importFrom(ggplot2,aes) importFrom(ggplot2,coord_flip) +importFrom(ggplot2,coord_sf) +importFrom(ggplot2,element_rect) importFrom(ggplot2,element_text) importFrom(ggplot2,facet_wrap) importFrom(ggplot2,geom_bar) importFrom(ggplot2,geom_col) +importFrom(ggplot2,geom_sf) importFrom(ggplot2,geom_text) importFrom(ggplot2,ggplot) importFrom(ggplot2,labs) importFrom(ggplot2,scale_fill_manual) +importFrom(ggplot2,scale_size_area) importFrom(ggplot2,scale_x_discrete) importFrom(ggplot2,scale_y_continuous) +importFrom(ggplot2,stat_sf_coordinates) importFrom(ggplot2,theme) importFrom(ggtext,element_markdown) importFrom(glue,glue) +importFrom(gouvdown,scale_fill_gouv_continuous) importFrom(gouvdown,scale_fill_gouv_discrete) importFrom(lubridate,make_date) importFrom(lubridate,year) @@ -50,6 +58,10 @@ importFrom(mapfactory,creer_carte_communes_prop) importFrom(mapfactory,fond_carto) importFrom(mapfactory,format_fr) importFrom(scales,number_format) +importFrom(sf,read_sf) +importFrom(sf,st_bbox) +importFrom(sf,st_buffer) +importFrom(sf,st_crs) importFrom(stats,quantile) importFrom(stringr,str_wrap) importFrom(tidyr,gather) diff --git a/R/creer_carte_2_2.R b/R/creer_carte_2_2.R new file mode 100644 index 0000000000000000000000000000000000000000..e99286d95319f083133ed452a961b1d0dbe45637 --- /dev/null +++ b/R/creer_carte_2_2.R @@ -0,0 +1,85 @@ +#' 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 +#' +#' @param millesime_obs_artif une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. +#' +#' @return Une carte +#' +#' @importFrom dplyr filter select mutate arrange group_by summarise left_join +#' @importFrom ggplot2 ggplot geom_sf stat_sf_coordinates aes coord_sf theme element_rect scale_size_area labs +#' @importFrom gouvdown scale_fill_gouv_continuous +#' @importFrom lubridate year +#' @importFrom glue glue +#' @importFrom sf read_sf st_bbox st_buffer st_crs +#' +#' @export +#' +#' @examples +#' creer_carte_2_2(millesime_obs_artif = 2019) +creer_carte_2_2 <- 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") %>% + 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::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::right_join(COGiter::departements_geo,., by = c("DEP" = "CodeZone")) + + # 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 = "", labels = function(x) format_fr(x, dec = 0)) + + # ggplot2::scale_size_area(name="")+ + ggplot2::scale_size_continuous(range = c(0, 20), labels = function(x) format_fr(x, dec = 0)) + + 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) +} diff --git a/R/creer_graphe_2_4.R b/R/creer_graphe_2_4.R index bbbce1dc8baf333d2cb837277209821b80b3bb04..51730017b9884506f878bd39f4be10c5b3ffb207 100644 --- a/R/creer_graphe_2_4.R +++ b/R/creer_graphe_2_4.R @@ -26,7 +26,7 @@ creer_graphe_2_4 <- 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$TypeZone == "D\u00e9partements", .data$CodeZone %in% c("44","49","53","72","85")) %>% dplyr::select(.data$Zone, .data$date, .data$flux_naf_artificialisation_total) %>% dplyr::mutate( Zone = forcats::fct_drop(.data$Zone) %>% diff --git a/data-raw/dataprep.R b/data-raw/dataprep.R index 29dec11539b0be965ebc78f6022edde805b1a5f0..1fb42910812bff30082982080fc6f78855c3b46b 100644 --- a/data-raw/dataprep.R +++ b/data-raw/dataprep.R @@ -112,7 +112,7 @@ result <- result %>% filter(CodeZone %in% liste_52 | TypeZone == "Régions") observatoire_artificialisation <- observatoire_artificialisation %>% - filter(CodeZone %in% liste_52 | TypeZone == "Régions") %>% + 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)) diff --git a/data/observatoire_artificialisation.rda b/data/observatoire_artificialisation.rda index 39f0842715cada950518407cf9585dcd9f8d8753..31b362548fbe41af3fe63dfb653c00b6af7e5eb7 100644 Binary files a/data/observatoire_artificialisation.rda and b/data/observatoire_artificialisation.rda differ diff --git a/devstuff_history.R b/devstuff_history.R index 245da718d9caa10f80a44cdef6838b8b7b71ee04..31b0811f6cf3e80c6f24017c0c50560b323c2bd9 100644 --- a/devstuff_history.R +++ b/devstuff_history.R @@ -54,6 +54,11 @@ usethis::use_vignette("af-ch1-7","af- Chapitre 1 Carte 7") usethis::use_r("creer_carte_1_7") usethis::use_test("creer_carte_1_7") +##creer_carte_2_2 +usethis::use_vignette("bb-ch2-2","bb- Chapitre 2 Carte 2") +usethis::use_r("creer_carte_2_2") +usethis::use_test("creer_carte_2_2") + ##creer_graphe_2_4 usethis::use_vignette("bd-ch2-4","bd- Chapitre 2 Graphe 4") usethis::use_r("creer_graphe_2_4") diff --git a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd index bdfb7f9425abfe1d73e0453de9981643ac5fc5e7..dbb06d0be068c57adb21096826c76098cc5209cc 100644 --- a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd @@ -65,6 +65,14 @@ creer_carte_1_7( params$millesime_ocsge) ## Chapitre 2 +```{r carte france evolution departements 10 ans,fig.width=9,fig.height=7} +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) ``` diff --git a/man/creer_carte_2_2.Rd b/man/creer_carte_2_2.Rd new file mode 100644 index 0000000000000000000000000000000000000000..060e197b59c856bc4fe93dc3d227566b89b285ae --- /dev/null +++ b/man/creer_carte_2_2.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/creer_carte_2_2.R +\name{creer_carte_2_2} +\alias{creer_carte_2_2} +\title{Creation de la carte de l evolution de l artificialisation sur 10 ans en ha par departement} +\usage{ +creer_carte_2_2(millesime_obs_artif) +} +\arguments{ +\item{millesime_obs_artif}{une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique.} +} +\value{ +Une carte +} +\description{ +Carte de l evolution de l artificialisation sur 10 ans en ha par departement +} +\examples{ +creer_carte_2_2(millesime_obs_artif = 2019) +} diff --git a/tests/testthat/test-creer_carte_2_2.R b/tests/testthat/test-creer_carte_2_2.R new file mode 100644 index 0000000000000000000000000000000000000000..b534271c2130a1c4a2da1eb09d568a4e326abd72 --- /dev/null +++ b/tests/testthat/test-creer_carte_2_2.R @@ -0,0 +1,7 @@ +test_that("creer_carte_2_2 fonctionne", { + + # Test que la carte est un ggplot + objet <- creer_carte_2_2(millesime_obs_artif = 2019) + testthat::expect_equal(attr(objet, "class"), c("gg","ggplot")) + +}) diff --git a/vignettes/bb-ch2-2.Rmd b/vignettes/bb-ch2-2.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..4ffe273ba08356809c39fc0df9f60751b3dd44a8 --- /dev/null +++ b/vignettes/bb-ch2-2.Rmd @@ -0,0 +1,30 @@ +--- +title: "bb- Chapitre 2 Carte 2" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{bb- Chapitre 2 Carte 2} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + echo = FALSE, + message = FALSE, + warning = FALSE, + error = FALSE, + comment = "#>", + fig.width = 9, + fig.height = 7 +) + +``` + +# Descriptif +La fonction `creer_carte_2_2()` produit la carte de l evolution de l artificialisation sur 10 ans en ha par departement. + +```{r setup} +library(propre.artificialisation) +creer_carte_2_2(millesime_obs_artif = 2019) +``` diff --git a/vignettes/test.R b/vignettes/test.R index a502a18bd34b397527cc479643153e4b92e41774..f148e5ae22d284d43da439f9cd80ad33e221929f 100644 --- a/vignettes/test.R +++ b/vignettes/test.R @@ -1,67 +1,96 @@ -#' Creation du graphique des surfaces Naf cadastrees nouvellement artificialisées entre 2009 et 2019 en ha -#' @description Graphique en barres des surfaces Naf cadastrees nouvellement artificialisées par departement en ha +#' 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 #' #' @param millesime_obs_artif une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. #' -#' @return Un diagramme en barres +#' @return Une carte #' -#' @importFrom dplyr filter select mutate group_by desc arrange -#' @importFrom forcats fct_inorder -#' @importFrom ggplot2 ggplot aes geom_bar labs scale_y_continuous theme geom_text geom_col scale_fill_manual -#' @importFrom scales number_format +#' @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 glue glue -#' @importFrom lubridate make_date -#' @importFrom tidyr spread gather -#' @importFrom tricky set_standard_names +#' @importFrom lubridate year +#' @importFrom stringr str_wrap #' @importFrom mapfactory format_fr #' #' @export #' #' @examples -#' creer_graphe_2_4(millesime_obs_artif=2019) - -library(propre.artificialisation) -millesime_obs_artif <- 2019 -# Encoding(data$Zone) <- "UTF-8" -# Encoding(data$TypeZone) <- "UTF-8" -# guess_encoding(data, n_max = 1000) - -#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") %>% - dplyr::select(Zone,date,flux_naf_artificialisation_total) %>% - dplyr::mutate(Zone = forcats::fct_drop(.data$Zone) %>% - forcats::fct_relevel(c("Loire-Atlantique", "Maine-et-Loire", "Mayenne", "Sarthe", "Vendée")), - flux_naf_artificialisation_total = flux_naf_artificialisation_total / 10000) %>% - dplyr::arrange(.data$Zone) %>% - dplyr::mutate(date = as.character(year(.data$date - 1))) %>% - dplyr::filter(.data$date < millesime_obs_artif, .data$date > millesime_obs_artif - 11) #conserve les 10 derniers millesimes - -# creation du graphique -graph_2_4<-data %>% - ggplot2::ggplot() + - ggplot2::geom_bar(ggplot2::aes(x=.data$date,y=.data$flux_naf_artificialisation_total,fill = .data$Zone), - width = 0.6, stat="identity")+ - # ggplot2::geom_bar(ggplot2::aes(x=.data$date,y=.data$flux_naf_artificialisation_total,fill = .data$Zone), - # width = 0.6, stat="identity",position=ggplot2::position_dodge(width=5))+ - ggplot2::geom_text(ggplot2::aes(x= .data$date,y = .data$flux_naf_artificialisation_total-120,label = mapfactory::format_fr(.data$flux_naf_artificialisation_total,0), group =.data$Zone), color = "black", size=3)+ - ggplot2::labs(title= glue::glue("Consommation d'espaces naturels,\n agricoles et forestiers (Naf)"), - subtitle=glue::glue("de {millesime_debut} \u00e0 {millesime_fin}"), - x="",y="surface (ha)", - fill="", - caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}"))+ - ggplot2::facet_wrap(Zone ~ ., ncol = 1)+ - ggplot2::theme(legend.position = "none", - strip.text.x = ggplot2::element_text(size=8, color="brown", - face="bold.italic"))+ - ggplot2::scale_fill_manual(values = gouvdown::gouv_palettes[["pal_gouv_qual2"]][1:5]) - -return(graph_1_5) +#' creer_carte_2_2(millesime_obs_artif = 2019) +millesime_obs_artif=2019 +library(COGiter) + +creer_graphe_2_2 <- 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") %>% + 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::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) +}