diff --git a/.gitignore b/.gitignore index 0d7f03b37c7df412ca479488a4122bfdf12202e8..9e865fe17efba85b4f5a0033f3fe9c63c82c8e89 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ .Ruserdata docs inst/doc +doc +Meta diff --git a/DESCRIPTION b/DESCRIPTION index 352773eae0e2646980aeaad466a453d2486cd7af..c4c14e6ac3082afbba75d5e7f6c0e69bcc36f7b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,6 +33,7 @@ Imports: mapfactory (>= 0.0.0.9000), scales, stats, + stringr, tidyr, tricky, utils diff --git a/NAMESPACE b/NAMESPACE index a08283449b0cc8ed4af363ab0fd106d9b7986941..6226ea47718611849f81d2f5fefccb6a84fc9a2b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(creer_carte_1_7) export(creer_graphe_1_1) export(creer_graphe_1_4) export(creer_graphe_1_5) +export(creer_graphe_2_4) export(format_fr_pct) importFrom(attempt,stop_if_not) importFrom(dplyr,arrange) @@ -21,12 +22,14 @@ importFrom(dplyr,summarise) importFrom(dplyr,ungroup) importFrom(forcats,fct_drop) importFrom(forcats,fct_inorder) +importFrom(forcats,fct_relevel) importFrom(forcats,fct_reorder) importFrom(ggiraph,geom_point_interactive) importFrom(ggiraph,ggiraph) importFrom(ggplot2,aes) importFrom(ggplot2,coord_flip) importFrom(ggplot2,element_text) +importFrom(ggplot2,facet_wrap) importFrom(ggplot2,geom_bar) importFrom(ggplot2,geom_col) importFrom(ggplot2,geom_text) @@ -38,7 +41,9 @@ importFrom(ggplot2,scale_y_continuous) importFrom(ggplot2,theme) importFrom(ggtext,element_markdown) importFrom(glue,glue) +importFrom(gouvdown,scale_fill_gouv_discrete) importFrom(lubridate,make_date) +importFrom(lubridate,year) importFrom(magrittr,"%>%") importFrom(mapfactory,creer_carte_communes) importFrom(mapfactory,creer_carte_communes_prop) @@ -46,6 +51,7 @@ importFrom(mapfactory,fond_carto) importFrom(mapfactory,format_fr) importFrom(scales,number_format) importFrom(stats,quantile) +importFrom(stringr,str_wrap) importFrom(tidyr,gather) importFrom(tidyr,spread) importFrom(tricky,set_standard_names) diff --git a/R/creer_graphe_2_4.R b/R/creer_graphe_2_4.R new file mode 100644 index 0000000000000000000000000000000000000000..bbbce1dc8baf333d2cb837277209821b80b3bb04 --- /dev/null +++ b/R/creer_graphe_2_4.R @@ -0,0 +1,60 @@ +#' Creation du graphique de la consommation d espaces NAF sur 10 ans en ha +#' @description Graphiques en barres de la consommation d espaces naturels agricoles et forestiers 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 cinq diagrammes 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 glue glue +#' @importFrom lubridate year +#' @importFrom stringr str_wrap +#' @importFrom mapfactory format_fr +#' +#' @export +#' +#' @examples +#' creer_graphe_2_4(millesime_obs_artif = 2019) +creer_graphe_2_4 <- 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::select(.data$Zone, .data$date, .data$flux_naf_artificialisation_total) %>% + dplyr::mutate( + Zone = forcats::fct_drop(.data$Zone) %>% + forcats::fct_relevel(c("Loire-Atlantique", "Maine-et-Loire", "Mayenne", "Sarthe", "Vend\u00e9e")), + flux_naf_artificialisation_total = .data$flux_naf_artificialisation_total / 10000 + ) %>% + 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 + + # 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_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 = "white", size = 3) + + ggplot2::labs( + title = stringr::str_wrap("Consommation d\'espaces naturels, agricoles et forestiers (Naf) par d\u00e9partement", width = 60), + subtitle = glue::glue("de {millesime_debut} \u00e0 {millesime_fin}"), + x = "", y = "Surface NAF consomm\u00e9es (en ha)", + fill = "", + caption = glue::glue("Source : DGFip/Cerema {millesime_obs_artif}") + ) + + ggplot2::facet_wrap(Zone ~ ., ncol = 1) + + ggplot2::theme(legend.position = "none") + + gouvdown::scale_fill_gouv_discrete(palette = "pal_gouv_qual2") + + ggplot2::scale_y_continuous(labels = function(x) format(x, big.mark = " ")) + + return(graph_2_4) +} diff --git a/data-raw/dataprep.R b/data-raw/dataprep.R index 08c7c756f89ba414904ab0d0b3bd6a2062f2cda7..29dec11539b0be965ebc78f6022edde805b1a5f0 100644 --- a/data-raw/dataprep.R +++ b/data-raw/dataprep.R @@ -112,22 +112,14 @@ 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") %>% + mutate(Zone = iconv(Zone,to = "UTF-8"), + TypeZone = iconv(TypeZone,to = "UTF-8")) %>% + mutate(across(where(is.character),as.factor)) rm(liste_52) -# encodage en utf8 de toutes les colonnes -# for (name in colnames(observatoire_artificialisation)){ -# Encoding(colnames(observatoire_artificialisation)) <- "UTF-8" -# } - -Encoding(observatoire_artificialisation$Zone) <- "UTF-8" -Encoding(observatoire_artificialisation$TypeZone) <- "UTF-8" - usethis::use_data(result, overwrite = TRUE, internal = FALSE) usethis::use_data(teruti, overwrite = TRUE, internal = FALSE) usethis::use_data(observatoire_artificialisation, overwrite = TRUE, internal = FALSE) usethis::use_data(metadata_donnee, overwrite = TRUE, internal = FALSE) - -# Encoding(raw$title[2]) <- "UTF-8" - diff --git a/data/observatoire_artificialisation.rda b/data/observatoire_artificialisation.rda index 2f78302721470a71c57d177436e7d1ae81fad0cc..39f0842715cada950518407cf9585dcd9f8d8753 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 db69745cfd3aaa25ba7dc603d7d5cf9b68dc4547..245da718d9caa10f80a44cdef6838b8b7b71ee04 100644 --- a/devstuff_history.R +++ b/devstuff_history.R @@ -13,8 +13,10 @@ usethis::use_package("ggiraph") usethis::use_package("sf") usethis::use_package("tricky") usethis::use_package("qpdf") -usethis::use_package("forcats") usethis::use_package("stats") +usethis::use_package("glue") +usethis::use_package("lubridate") +usethis::use_package("mapfactory") 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") @@ -37,9 +39,6 @@ usethis::use_vignette("ac-ch1-3","ac- Chapitre 1 Carte 3") usethis::use_r("creer_carte_1_3") usethis::use_test("creer_carte_1_3") - - - ##creer_graphe_1_4 usethis::use_vignette("ac-ch1-4","ac- Chapitre 1 Graphe 4") usethis::use_r("creer_graphe_1_4") @@ -55,6 +54,10 @@ 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_graphe_2_4 +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") ## ajout news.md diff --git a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd index f02b17d6271306b7733cfaef93415015ffdbb421..bdfb7f9425abfe1d73e0453de9981643ac5fc5e7 100644 --- a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd @@ -8,7 +8,7 @@ output: params: millesime_teruti: 2018 millesime_ocsge: 2017 - millesime_observatoire_artificialisation: 2019 + millesime_obs_artif: 2019 title: "L'artificialisation des sols en Pays de la loire en `r params$millesime_observatoire_artificialisation`" --- @@ -63,5 +63,9 @@ creer_graphe_1_4( params$millesime_teruti) creer_carte_1_7( params$millesime_ocsge) ``` +## Chapitre 2 +```{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_graphe_2_4.Rd b/man/creer_graphe_2_4.Rd new file mode 100644 index 0000000000000000000000000000000000000000..72814d64e3782b1224e1299eefc27a4fb1009b65 --- /dev/null +++ b/man/creer_graphe_2_4.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/creer_graphe_2_4.R +\name{creer_graphe_2_4} +\alias{creer_graphe_2_4} +\title{Creation du graphique de la consommation d espaces NAF sur 10 ans en ha} +\usage{ +creer_graphe_2_4(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{ +Graphiques en barres de la consommation d espaces naturels agricoles et forestiers sur 10 ans en ha par departement +} +\examples{ +creer_graphe_2_4(millesime_obs_artif = 2019) +} diff --git a/tests/testthat/test-creer_graphe_2_4.R b/tests/testthat/test-creer_graphe_2_4.R new file mode 100644 index 0000000000000000000000000000000000000000..38358d01fe33646eb601697df2a3c132222c61ce --- /dev/null +++ b/tests/testthat/test-creer_graphe_2_4.R @@ -0,0 +1,7 @@ +test_that("creer_graphe_2_4 fonctionne", { + + # Test que le graphe est un ggplot + objet <- creer_graphe_2_4(millesime_obs_artif=2019) + testthat::expect_equal(attr(objet, "class"), c("gg","ggplot")) + +}) diff --git a/vignettes/aa-ch1-1.Rmd b/vignettes/aa-ch1-1.Rmd index 1d225713578d88d9dcbc57688b285c18058578f1..9a35689821d7edecfd5e3f3c26fd668a0a7a59d5 100644 --- a/vignettes/aa-ch1-1.Rmd +++ b/vignettes/aa-ch1-1.Rmd @@ -14,7 +14,9 @@ knitr::opts_chunk$set( message = FALSE, warning = FALSE, error = FALSE, - comment = "#>" + comment = "#>", + fig.width = 9, + fig.height = 7 ) diff --git a/vignettes/ac-ch1-4.Rmd b/vignettes/ac-ch1-4.Rmd index c691b1fd023b73621fef830f40233ef75291707a..bf2295b3c0cc2cf9e54b39884fffa7b266a0bab0 100644 --- a/vignettes/ac-ch1-4.Rmd +++ b/vignettes/ac-ch1-4.Rmd @@ -14,16 +14,22 @@ knitr::opts_chunk$set( message = FALSE, warning = FALSE, error = FALSE, - comment = "#>" + comment = "#>", + fig.width = 9, + fig.height = 7 ) -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_graphe_1_4()` produit le graphique en barres des surfaces artificialisees en volume (Teruti-Lucas) -```{r setup} +La fonction `creer_graphe_1_4()` produit le graphique en barres des surfaces artificialisees en volume (Teruti-Lucas). + +```{r viz} library(propre.artificialisation) +library(gouvdown) - creer_graphe_1_4(millesime_teruti=2018) +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")) +creer_graphe_1_4(millesime_teruti=2018) ``` diff --git a/vignettes/ae-ch1-5.Rmd b/vignettes/ae-ch1-5.Rmd index 46bc9a9078e74d616e05ace217773c017a94fb0b..203421e63bb5bc75e8ec66acf3656b9a99cc5cab 100644 --- a/vignettes/ae-ch1-5.Rmd +++ b/vignettes/ae-ch1-5.Rmd @@ -14,11 +14,15 @@ knitr::opts_chunk$set( message = FALSE, warning = FALSE, error = FALSE, - comment = "#>" + comment = "#>", + fig.width = 9, + fig.height = 7 ) ``` + # Descriptif + La fonction `creer_graphe_1_5()` produit le graphique en barres de la part des surfaces artificialisees dans la surface departementale et regionale (Teruti-Lucas). ```{r viz} diff --git a/vignettes/bd-ch2-4.Rmd b/vignettes/bd-ch2-4.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..113f29485e98dbd73e89390dfe47352c956a7f77 --- /dev/null +++ b/vignettes/bd-ch2-4.Rmd @@ -0,0 +1,29 @@ +--- +title: "bd- Chapitre 2 Graphe 4" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{bd- Chapitre 2 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_2_4()` produit graphique de la consommation d espaces NAF sur 10 ans en ha + +```{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_4(millesime_obs_artif=2019) +``` diff --git a/vignettes/test.R b/vignettes/test.R index 31e973bfa00424874dba6eac606c16152cd0b226..a502a18bd34b397527cc479643153e4b92e41774 100644 --- a/vignettes/test.R +++ b/vignettes/test.R @@ -1,7 +1,7 @@ #' 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 #' -#' @param millesime_teruti 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 Un diagramme en barres #' @@ -18,6 +18,50 @@ #' @export #' #' @examples -#' creer_graphe_2_4(millesime_teruti=2018) +#' 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) + +