diff --git a/DESCRIPTION b/DESCRIPTION index e6202da82f58a6de9ff22851e7dca3240638b2be..04ee8486402fcaef3598b2a144c5defae47e4b52 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,7 +29,9 @@ Imports: gouvdown (>= 0.0.0.9000), lubridate, magrittr, + mapfactory (>= 0.0.0.9000), scales, + stats, tidyr, tricky, utils @@ -40,6 +42,7 @@ Suggests: VignetteBuilder: knitr Remotes: + gitlab::dreal-datalab/mapfactory, pachevalier/tricky, spyrales/gouvdown Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 059c749a6b0d30d0c925c4b4b1bc1825dec9d90e..000337823a6aed7647c62740ccbbded8fb6800c4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export("%>%") +export(creer_carte_1_7) export(creer_graphe_1_1) export(creer_graphe_1_3) export(format_fr_pct) @@ -13,6 +14,7 @@ importFrom(dplyr,group_by) importFrom(dplyr,mutate) importFrom(dplyr,pull) importFrom(dplyr,select) +importFrom(dplyr,summarise) importFrom(dplyr,ungroup) importFrom(forcats,fct_drop) importFrom(forcats,fct_inorder) @@ -32,7 +34,10 @@ importFrom(ggplot2,theme) importFrom(glue,glue) importFrom(lubridate,make_date) importFrom(magrittr,"%>%") +importFrom(mapfactory,creer_carte_communes) +importFrom(mapfactory,fond_carto) importFrom(scales,number_format) +importFrom(stats,quantile) importFrom(tidyr,gather) importFrom(tidyr,spread) importFrom(tricky,set_standard_names) diff --git a/R/creer_carte_1_7.R b/R/creer_carte_1_7.R new file mode 100644 index 0000000000000000000000000000000000000000..4c4335693758acce645161c06f1756c7d7ce521c --- /dev/null +++ b/R/creer_carte_1_7.R @@ -0,0 +1,53 @@ +#' Creation de la Carte de la part du territoire communal artificialise selon OCSGE +#' @description Carte de la part du territoire communal artificialise selon OCSGE +#' +#' @param millesime_ocsge une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. +#' +#' @return Une carte +#' +#' @importFrom dplyr filter mutate arrange group_by summarise ungroup select +#' @importFrom glue glue +#' @importFrom lubridate make_date +#' @importFrom stats quantile +#' @importFrom mapfactory fond_carto creer_carte_communes +#' +#' @export +#' +#' @examples +#' creer_carte_1_7(millesime_ocsge=2017) + +creer_carte_1_7 <- function(millesime_ocsge){ + + data <- result %>% + dplyr::mutate(valeur=as.numeric(.data$valeur)) %>% + dplyr::filter(.data$TypeZone == "Communes", + .data$date == lubridate::make_date(millesime_ocsge,"01","01"), + .data$variable %in% c("a_definir","autre_surface_naturelle","espace_agricole", + "espace_artificialise","surface_en_eau","surface_naturelle_boisee")) %>% + dplyr::arrange(.data$CodeZone) %>% + dplyr::group_by(.data$TypeZone,.data$Zone,.data$CodeZone,.data$date) %>% + dplyr::summarise(variable=.data$variable,valeur=.data$valeur / sum(.data$valeur,na.rm=T)) %>% + dplyr::filter(.data$variable == "espace_artificialise") %>% + dplyr::ungroup() %>% + dplyr::select(.data$TypeZone,.data$CodeZone,.data$Zone,.data$date,.data$valeur) %>% + dplyr::mutate(valeur=.data$valeur * 100) + + 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_1_7 <- mapfactory::creer_carte_communes(data = data, + code_region=52, + carto = fond_carto, + indicateur = valeur, + bornes = bins, + pourcent = TRUE, + decimales = 1, + titre = "Part du territoire communal artificialis\u00e9", + sous_titre = NULL, + bas_de_page = glue::glue("Source : OCSGE {millesime_ocsge}"), + suffixe = NULL + ) + + return(carte_1_7) + +} diff --git a/R/globals.R b/R/globals.R index b79bd5bc31aa4f9b045d6cf7844b73b0ed996e43..593318a087c36b0f35c2885325ea49bfc224906c 100644 --- a/R/globals.R +++ b/R/globals.R @@ -1,3 +1,3 @@ utils::globalVariables( - c("teruti",".data","variable","valeur") + c("teruti","result",".data","variable","valeur") ) diff --git a/data-raw/dataprep.R b/data-raw/dataprep.R index bd3d04a1f36d492eb69c2c19170a993100ea780d..eb9d5ac970e0da3b30063e39e3a8507bd0170d4f 100644 --- a/data-raw/dataprep.R +++ b/data-raw/dataprep.R @@ -51,7 +51,8 @@ teruti <- read.csv2("extdata/FDS_W0020_Moy-2017-2018-2019.csv", as.is = TRUE, en # chargement des tables cogifiées d'indicateurs territoriaux load("extdata/cogifiee_chargement_ocsge.RData") -ocsge <- data_cogifiee +ocsge <- data_cogifiee %>% + mutate(variable = str_replace(variable, "a_définir", "a_definir")) load("extdata/cogifiee_chargement_observatoire_artificialisation.RData") observatoire_artificialisation <- data_cogifiee load("extdata/cogifiee_chargement_pci.RData") @@ -104,3 +105,4 @@ rm(liste_52) usethis::use_data(result, overwrite = TRUE, internal = FALSE) usethis::use_data(teruti, overwrite = TRUE, internal = FALSE) usethis::use_data(metadata_donnee, overwrite = TRUE, internal = FALSE) + diff --git a/data/result.rda b/data/result.rda index bd1c2773b86d9a90a7e77744801343d276e077d6..79edf22ec9439374b6601ee548ab7547f9af0fa7 100644 Binary files a/data/result.rda and b/data/result.rda differ diff --git a/data/teruti.rda b/data/teruti.rda index 55042be8bbf4760aa54403893d4ad833acdc88a7..60723695fc1c71f788fff0b18bafd7158358963f 100644 Binary files a/data/teruti.rda and b/data/teruti.rda differ diff --git a/devstuff_history.R b/devstuff_history.R index 9f9d5d215437a3b17c78ef45b8c5e7e779075b10..5f9aceb6e42750d75e32b47298cf0da6aeeec297 100644 --- a/devstuff_history.R +++ b/devstuff_history.R @@ -14,8 +14,10 @@ usethis::use_package("sf") usethis::use_package("tricky") usethis::use_package("qpdf") usethis::use_package("forcats") +usethis::use_package("stats") 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") usethis::use_rmarkdown_template(template_name = "publication artificialisation", template_description = "Template Rmarkdown pour la publication sur l'artificialisation des sols en Pays de la Loire") usethis::use_data_raw("dataprep") @@ -36,7 +38,10 @@ usethis::use_vignette("ac-ch1-3","ac- Chapitre 1 Graphe 3") usethis::use_r("creer_graphe_1_3") usethis::use_test("creer_graphe_1_3") - +##creer_carte_1_7 +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") ## A faire tourner avant chaque commit # usethis::use_r("globals.R") # liste les objets à passer en variables globales diff --git a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd index e383d763d0449a85666e168c81e5f1b24b0a0c63..80ccd28e51d0f535325aba6d70786bb8d3f822fa 100644 --- a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd @@ -7,14 +7,14 @@ output: logo: "prefecture_r52" params: millesime_teruti: 2018 - millesime_ocsge: 2019 + millesime_ocsge: 2017 millesime_observatoire_artificialisation: 2019 title: "L'artificialisation des sols en Pays de la loire en `r params$millesime_observatoire_artificialisation`" --- ```{r setup, include=FALSE} -knitr::opts_chunk$set(echo = TRUE) +knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, error = FALSE) library(gouvdown) library(propre.artificialisation) ``` @@ -37,3 +37,6 @@ creer_graphe_1_3( params$millesime_teruti) ``` texte... +```{r carte part du territoire communal artificialise} +creer_carte_1_7( params$millesime_ocsge) +``` diff --git a/man/creer_carte_1_7.Rd b/man/creer_carte_1_7.Rd new file mode 100644 index 0000000000000000000000000000000000000000..da4ca4bfafc5563a9c1ba42efd89ff60f5f520e4 --- /dev/null +++ b/man/creer_carte_1_7.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/creer_carte_1_7.R +\name{creer_carte_1_7} +\alias{creer_carte_1_7} +\title{Creation de la Carte de la part du territoire communal artificialise selon OCSGE} +\usage{ +creer_carte_1_7(millesime_ocsge) +} +\arguments{ +\item{millesime_ocsge}{une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique.} +} +\value{ +Une carte +} +\description{ +Carte de la part du territoire communal artificialise selon OCSGE +} +\examples{ +creer_carte_1_7(millesime_ocsge=2017) +} diff --git a/tests/testthat/test-creer_carte_1_7.R b/tests/testthat/test-creer_carte_1_7.R new file mode 100644 index 0000000000000000000000000000000000000000..ff88beaebdeeca05614e797617d797acba20b456 --- /dev/null +++ b/tests/testthat/test-creer_carte_1_7.R @@ -0,0 +1,7 @@ +test_that("creer_carte_1_7 fonctionne", { + + # Test que la carte est un ggplot + objet <- creer_carte_1_7(millesime_ocsge = 2017) + testthat::expect_equal(attr(objet, "class"), c("gg","ggplot")) + +}) diff --git a/vignettes/af-ch1-7.Rmd b/vignettes/af-ch1-7.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..58fad76dba26e57036dddff19847098f72465590 --- /dev/null +++ b/vignettes/af-ch1-7.Rmd @@ -0,0 +1,25 @@ +--- +title: "af- Chapitre 1 Carte 7" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{af- Chapitre 1 Carte 7} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +ggplot2::theme_set(gouvdown::theme_gouv(plot_title_size = 14, subtitle_size = 12, base_size = 10, caption_size = 10) + + ggplot2::theme(plot.caption.position = "plot")) +``` + +# Descriptif +La fonction `creer_carte_1_7()` produit la carte de la part du territoire communal artificialise selon OCSGE. + +```{r setup} +library(propre.artificialisation) +creer_carte_1_7(millesime_ocsge=2017) +``` diff --git a/vignettes/test.R b/vignettes/test.R index 9eac1d9c84272ddf07d998f2a26f5fd260ba1437..dcb03a90a41b28b12fd5521b4c54ed6df3a0eb29 100644 --- a/vignettes/test.R +++ b/vignettes/test.R @@ -1,39 +1,62 @@ library(dplyr) library(gouvdown) -millesime_teruti <- 2018 - -# Creation de la table utile a la production du graphique -data <- teruti %>% - dplyr::mutate(valeur=as.numeric(.data$valeur)) %>% - dplyr::filter(.data$CodeZone == "52" & .data$TypeZone == "R\u00e9gions" | .data$CodeZone %in% c("44","49","53","72","85") & .data$TypeZone == "D\u00e9partements", - .data$date == lubridate::make_date(millesime_teruti,"01","01")) %>% - tidyr::spread(key=.data$variable,value=.data$valeur,fill=0) %>% - tricky::set_standard_names() %>% - dplyr::mutate (voiries=(.data$sols_revetus )/.data$tous_sols*100, - hors_voiries=(.data$sols_batis+ .data$sols_stabilises+ .data$autres_sols_artificialises )/.data$tous_sols*100 - ) %>% - dplyr::select(.data$codezone,.data$zone,.data$voiries,.data$hors_voiries)%>% - tidyr::gather(variable,valeur,.data$voiries:.data$hors_voiries)%>% - dplyr::mutate(variable = replace(.data$variable, .data$variable=="hors_voiries","surfaces artificialisées hors voiries"), - codezone = replace(.data$codezone, .data$codezone=="52","Région"))%>% - dplyr::mutate(variable=factor(.data$variable,levels=c("surfaces artificialisées hors voiries","voiries"))%>% forcats::fct_inorder()) - -graph_1_3<-data %>% - ggplot2::ggplot(ggplot2::aes(x=.data$codezone,y=.data$valeur,fill=.data$variable)) + - ggplot2::geom_bar(stat="identity")+ - ggplot2::labs(title= glue::glue("Part des surfaces artificialis\u00e9es dans la surface \nd\u00e9partementale et r\u00e9gionale en {millesime_teruti} en % (Teruti-Lucas)"),subtitle="",x="",y="", - fill="", - caption = glue::glue("Source : Teruti-Lucas {millesime_teruti}"))+ - - gouvdown::scale_fill_gouv_discrete(palette = "pal_gouv_fr")+ - # ggplot2::geom_text(ggplot2::aes(y=.data$valeur , label=paste0(round(.data$valeur,1),"%")),position= ggplot2::position_dodge(width=0), vjust=0, size=3)+ - ggplot2::theme(legend.position = "bottom")+ - ggplot2::scale_y_continuous(labels = scales::number_format(suffix = " %", accuracy = 1)) - -return(graph_1_3) +millesime_ocsge <- 2017 +library(mapfactory) +#' Creation de la Carte communale de la part artificialisee des communes selon OCSGE +#' @description Carte communale des Pays de la Loire de la part artificialisee des communes selon OCSGE +#' +#' @param millesime_ocsge une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. +#' +#' @return Une carte +#' +#' @importFrom dplyr filter mutate left_join +#' @importFrom ggplot2 ggplot aes geom_sf labs scale_size_area scale_fill_gradient2 coord_sf stat_sf_coordinates +#' @importFrom glue glue +#' @importFrom lubridate make_date +#' @importFrom sf st_as_sf +#' +#' @export +#' +#' @examples +#' creer_carte_1_7(millesime_ocsge=2017) +creer_carte_1_7 <- function(millesime_ocsge){ + + data <- result %>% + dplyr::mutate(valeur=as.numeric(.data$valeur)) %>% + dplyr::filter(.data$TypeZone == "Communes", + .data$date == lubridate::make_date(millesime_ocsge,"01","01"), + .data$variable %in% c("a_definir","autre_surface_naturelle","espace_agricole", + "espace_artificialise","surface_en_eau","surface_naturelle_boisee")) %>% + dplyr::arrange(.data$CodeZone) %>% + dplyr::group_by(.data$TypeZone,.data$Zone,.data$CodeZone,.data$date) %>% + dplyr::summarise(variable=.data$variable,valeur=.data$valeur / sum(.data$valeur,na.rm=T)) %>% + dplyr::filter(.data$variable == "espace_artificialise") %>% + dplyr::ungroup() %>% + dplyr::select(TypeZone,CodeZone,Zone,date,valeur) %>% + dplyr::mutate(valeur=.data$valeur * 100) + + fond_carto <- 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_1_7 <- mapfactory::creer_carte_communes(data = data, + code_region=52, + carto = fond_carto, + indicateur = valeur, + bornes = bins, + pourcent = TRUE, + decimales = 1, + titre = "Part artificialis\u00e9e des communes", + sous_titre = NULL, + bas_de_page = glue::glue("Source : OCSGE {millesime_ocsge}"), + suffixe = NULL + ) + + return(carte_1_7) + +}