diff --git a/.Rbuildignore b/.Rbuildignore index c8b6d784046ef1ee436add1196937632ef9c3eb3..ed6562cc0597e60520c2f0ff5d4df1aab66728cc 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,3 +9,8 @@ ^_pkgdown\.yml$ ^docs$ ^pkgdown$ +^test\.R$ +^vignette/test\.R$ +^ci$ +^doc$ +^Meta$ diff --git a/.gitignore b/.gitignore index 234f0289784f91ce7588b028bd72e1de86c1beb3..0d7f03b37c7df412ca479488a4122bfdf12202e8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .RData .Ruserdata docs +inst/doc diff --git a/DESCRIPTION b/DESCRIPTION index 8af3f3fd83e271d34d968ec8ff76d076f4eb7022..f43f422ae1b245ff4ad0d13dded155d270411686 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,33 +6,44 @@ Authors@R: family = "Kalioudjoglou", role = "aut", email = "daniel.kalioudjoglou@developpement-durable.gouv.fr"), - person(given = "Gwénaëlle", + person(given = "Gwenaelle", family = "Le Bourhis", role = "aut", - email = "gwenaelle.le-bourhis@developpement-durable.gouv.fr"), - person(given = "Maël", - family = "THEULIERE", + email = "gwenaelle.lebourhis@developpement-durable.gouv.fr"), + person(given = "Mael", + family = "Theuliere", role = c("aut", "cre"), - email = "mael.theuliere@developpement-durable.gouv.fr") - ) -Description: The goal of propre.artificialisation is to produce a publication on land use in Pays de la Loire. + email = "mael.theuliere@gmail.com")) +Description: The goal of propre.artificialisation is to produce + a publication on land use in Pays de la Loire. License: GPL (>= 3) -Encoding: UTF-8 -LazyData: true -Roxygen: list(markdown = TRUE) -RoxygenNote: 7.1.1 -Imports: - rmarkdown, - gouvdown (>= 0.0.0.9000), - ggplot2, - COGiter (>= 0.0.7), - dplyr, - sf -Remotes: - maeltheuliere/COGiter, - spyrales/gouvdown Depends: R (>= 2.10) +Imports: + attempt, + dplyr, + forcats, + ggiraph, + ggplot2, + glue, + gouvdown (>= 0.0.0.9000), + lubridate, + magrittr, + scales, + tidyr, + tricky, + utils Suggests: + knitr, + rmarkdown, testthat (>= 3.0.0) +VignetteBuilder: + knitr +Remotes: + spyrales/gouvdown, + pachevalier/tricky Config/testthat/edition: 3 +Encoding: UTF-8 +LazyData: true +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.1.1 diff --git a/NAMESPACE b/NAMESPACE index 6ae926839dd1829f1016a96f766d970ff184ad97..c0b9c24fed0ced1432e8cfaef2d79978a7783216 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,2 +1,28 @@ # Generated by roxygen2: do not edit by hand +export("%>%") +export(creer_graphe_1_1) +export(format_fr_pct) +importFrom(attempt,stop_if_not) +importFrom(dplyr,filter) +importFrom(dplyr,first) +importFrom(dplyr,group_by) +importFrom(dplyr,mutate) +importFrom(dplyr,pull) +importFrom(dplyr,select) +importFrom(dplyr,ungroup) +importFrom(forcats,fct_drop) +importFrom(forcats,fct_reorder) +importFrom(ggiraph,geom_point_interactive) +importFrom(ggiraph,ggiraph) +importFrom(ggplot2,aes) +importFrom(ggplot2,element_text) +importFrom(ggplot2,geom_bar) +importFrom(ggplot2,geom_text) +importFrom(ggplot2,ggplot) +importFrom(ggplot2,labs) +importFrom(ggplot2,scale_y_continuous) +importFrom(ggplot2,theme) +importFrom(glue,glue) +importFrom(magrittr,"%>%") +importFrom(scales,number_format) diff --git a/R/creer_graphe_1_1.R b/R/creer_graphe_1_1.R new file mode 100644 index 0000000000000000000000000000000000000000..17eadfece0c89a37c4a787c4c030f7de0a751997 --- /dev/null +++ b/R/creer_graphe_1_1.R @@ -0,0 +1,72 @@ +#' Creation du graphique du taux d'artificialisation par region (Teruti-Lucas) +#' @description Creation du graphique representant le taux d'artificialisation par region (Teruti-Lucas) +#' +#' @param millesime_teruti une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique. +#' +#' @return Un diagramme en barres +#' +#' @importFrom dplyr filter select group_by mutate first ungroup pull +#' @importFrom forcats fct_drop fct_reorder +#' @importFrom ggiraph geom_point_interactive ggiraph +#' @importFrom ggplot2 ggplot aes geom_bar labs scale_y_continuous theme geom_text element_text +#' @importFrom scales number_format +#' @importFrom glue glue +#' +#' @export +#' +#' @examples +#' creer_graphe_1_1(millesime_teruti=2018) + + +creer_graphe_1_1 <- function(millesime_teruti){ + + # Creation de la table utile a la production du graphique + data_france <- teruti %>% + dplyr::mutate(valeur=as.numeric(.data$valeur)) %>% + dplyr::filter(.data$CodeZone == "FRMETRO", + .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 (taux_artificialisation=.data$sols_artificiels/.data$tous_sols*100) + surf_artificialise_france <- data_france %>% + dplyr::pull(.data$sols_artificiels) + + data <- teruti %>% + dplyr::mutate(valeur=as.numeric(.data$valeur)) %>% + # Filtres pour ne garder que les donnees de la region et du millesime sélectionne + dplyr::filter(.data$TypeZone == "R\u00e9gions", + !(.data$CodeZone %in% c("01","02","03","04","06")), + .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 (taux_artificialisation=.data$sols_artificiels/.data$tous_sols*100, + part_dans_surface_nationale=.data$sols_artificiels/surf_artificialise_france*100, + couleur_barre = dplyr::case_when( + .data$codezone=="52"~ 1, + TRUE ~ 0) + ) %>% + dplyr::select(.data$zone,.data$taux_artificialisation,.data$part_dans_surface_nationale,.data$couleur_barre) + + valeur_max <- max(data$taux_artificialisation,na.rm=T) + + graph_1_1<-data %>% + ggplot2::ggplot(ggplot2::aes(x=forcats::fct_reorder(.data$zone,.data$taux_artificialisation,.desc=F), ymax=valeur_max+4, y=.data$taux_artificialisation,fill=.data$couleur_barre)) + + ggplot2::geom_bar(stat="identity")+ + ggplot2::geom_text(ggplot2::aes(y=.data$taux_artificialisation , + label=format_fr_pct(.data$taux_artificialisation), + color = .data$couleur_barre + ), + position= ggplot2::position_dodge(width=1), + vjust=0.5, + hjust=-0.5, + size=3) + + ggplot2::coord_flip(expand = FALSE) + + ggplot2::labs(title=glue::glue("Taux d'artificialisation par r\u00e9gion en {millesime_teruti}"),subtitle="",x="",y="", + fill="", + caption = glue::glue("Source : Teruti-Lucas {millesime_teruti}"))+ + ggplot2::theme(axis.text.x = ggplot2::element_text(), legend.position = "none") + + ggplot2::scale_y_continuous(labels = scales::number_format(suffix = " %", accuracy = 1)) + + return(graph_1_1) + + } diff --git a/R/globals.R b/R/globals.R new file mode 100644 index 0000000000000000000000000000000000000000..5e9a009df1e536c7dc71db36739ba1d7e2a507a2 --- /dev/null +++ b/R/globals.R @@ -0,0 +1,3 @@ +utils::globalVariables( + c("teruti",".data") +) diff --git a/R/utils-pipe.R b/R/utils-pipe.R new file mode 100644 index 0000000000000000000000000000000000000000..fd0b1d13db4ff91b7f836f72b7d5d88d958f6e1f --- /dev/null +++ b/R/utils-pipe.R @@ -0,0 +1,14 @@ +#' Pipe operator +#' +#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +#' +#' @name %>% +#' @rdname pipe +#' @keywords internal +#' @export +#' @importFrom magrittr %>% +#' @usage lhs \%>\% rhs +#' @param lhs A value or the magrittr placeholder. +#' @param rhs A function call using the magrittr semantics. +#' @return The result of calling `rhs(lhs)`. +NULL diff --git a/R/utils.r b/R/utils.r new file mode 100644 index 0000000000000000000000000000000000000000..637c759afea61b2a94b341cdff3d987a911a41d8 --- /dev/null +++ b/R/utils.r @@ -0,0 +1,21 @@ +#' fonction utilitaire de formatage en pourcentage pour le francais +#' +#' @description fonction utilitaire de formatage en pourcentage pour le francais + +#' @param x un nombre à formater en pourcentage +#' @param dec un entier désignant le nombre de chiffres après la virgule souhaité (1 par défaut) +#' +#' @return une chaîne de texte, x %, avec transformation de la décimale en virgule et insertion d'un espace insécable +#' +#' @importFrom attempt stop_if_not +#' +#' @export +#' +#' @examples +#' format_fr_pct(100/3) +#' + +format_fr_pct <- function(x, dec = 1) { + attempt::stop_if_not(x, is.numeric, msg = "x n'est pas un nombre, revoyez la saisie de l'argument de format_fr_pct(x, dec)") + paste0(formatC(x, decimal.mark = ",", big.mark = "\u202f", format = "f", digits = dec), "\u00a0%") +} diff --git a/devstuff_history.R b/devstuff_history.R index d423d6b179a3d98cb79d0d47f7fef17c8f32b735..632e847057eae3d1c8624551a7a3c96155c0e23a 100644 --- a/devstuff_history.R +++ b/devstuff_history.R @@ -1,12 +1,19 @@ usethis::create_package(".") usethis::use_build_ignore("devstuff_history.R") +usethis::use_build_ignore("vignette/test.R") usethis::use_gpl3_license() usethis::use_readme_rmd() +usethis::use_pipe() usethis::use_package("rmarkdown") usethis::use_package("gouvdown") usethis::use_package("ggplot2") usethis::use_package("dplyr") +usethis::use_package("forcats") +usethis::use_package("ggiraph") usethis::use_package("sf") +usethis::use_package("tricky") +usethis::use_package("qpdf") +usethis::use_package("forcats") usethis::use_dev_package("COGiter",remote = "maeltheuliere/COGiter") usethis::use_dev_package("gouvdown",remote = "spyrales/gouvdown") usethis::use_rmarkdown_template(template_name = "publication artificialisation", @@ -17,3 +24,16 @@ usethis::use_r("data") usethis::use_build_ignore(".gitlab-ci.yml") usethis::use_testthat() usethis::use_test(name = "data") + +##creer_graphe_1_1 +usethis::use_vignette("aa-ch1-1","aa- Chapitre 1 Graphe 1") +usethis::use_r("creer_graphe_1_1") +usethis::use_test("creer_graphe_1_1") + + + +## A faire tourner avant chaque commit +# usethis::use_r("globals.R") # liste les objets à passer en variables globales +attachment::att_amend_desc() +devtools::check() +devtools::install() diff --git a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd index eb916bcab3a554497a808f015857ad8917a63d39..759f38e38d3a46eec0aadb6291bac1ae4da34fc3 100644 --- a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd @@ -6,8 +6,11 @@ output: toc: TRUE logo: "prefecture_r52" params: - annee: 2019 -title: "L'artificialisation des sols en Pays de la loire en `r params$annee`" + millesime_teruti: 2018 + millesime_ocsge: 2019 + millesime_observatoire_artificialisation: 2019 + +title: "L'artificialisation des sols en Pays de la loire en `r params$millesime_observatoire_artificialisation`" --- ```{r setup, include=FALSE} @@ -18,3 +21,7 @@ library(propre.artificialisation) ## Chapitre 1 + +```{r diag barres nb logts par nb de pieces} +creer_graphe_1_1( params$millesime_teruti) +``` diff --git a/inst/rmarkdown/templates/publication/skeleton/www b/inst/rmarkdown/templates/publication/skeleton/www deleted file mode 100644 index 52a940b6cb8de8e7149621bf59ff81a2ff96b3a5..0000000000000000000000000000000000000000 Binary files a/inst/rmarkdown/templates/publication/skeleton/www and /dev/null differ diff --git a/man/creer_graphe_1_1.Rd b/man/creer_graphe_1_1.Rd new file mode 100644 index 0000000000000000000000000000000000000000..46799b96767df91883220522751155b50a177753 --- /dev/null +++ b/man/creer_graphe_1_1.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/creer_graphe_1_1.R +\name{creer_graphe_1_1} +\alias{creer_graphe_1_1} +\title{Creation du graphique du taux d'artificialisation par region (Teruti-Lucas)} +\usage{ +creer_graphe_1_1(millesime_teruti) +} +\arguments{ +\item{millesime_teruti}{une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique.} +} +\value{ +Un diagramme en barres +} +\description{ +Creation du graphique representant le taux d'artificialisation par region (Teruti-Lucas) +} +\examples{ +creer_graphe_1_1(millesime_teruti=2018) +} diff --git a/man/format_fr_pct.Rd b/man/format_fr_pct.Rd new file mode 100644 index 0000000000000000000000000000000000000000..ff280aa35c97b220a87a96a2ede3e6c6a230b7f2 --- /dev/null +++ b/man/format_fr_pct.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.r +\name{format_fr_pct} +\alias{format_fr_pct} +\title{fonction utilitaire de formatage en pourcentage pour le francais} +\usage{ +format_fr_pct(x, dec = 1) +} +\arguments{ +\item{x}{un nombre à formater en pourcentage} + +\item{dec}{un entier désignant le nombre de chiffres après la virgule souhaité (1 par défaut)} +} +\value{ +une chaîne de texte, x \%, avec transformation de la décimale en virgule et insertion d'un espace insécable +} +\description{ +fonction utilitaire de formatage en pourcentage pour le francais +} +\examples{ +format_fr_pct(100/3) + +} diff --git a/man/pipe.Rd b/man/pipe.Rd new file mode 100644 index 0000000000000000000000000000000000000000..a648c2969b222841abe76fb2e13c62c351078b2e --- /dev/null +++ b/man/pipe.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-pipe.R +\name{\%>\%} +\alias{\%>\%} +\title{Pipe operator} +\usage{ +lhs \%>\% rhs +} +\arguments{ +\item{lhs}{A value or the magrittr placeholder.} + +\item{rhs}{A function call using the magrittr semantics.} +} +\value{ +The result of calling \code{rhs(lhs)}. +} +\description{ +See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +} +\keyword{internal} diff --git a/tests/testthat/test-creer_graphe_1_1.R b/tests/testthat/test-creer_graphe_1_1.R new file mode 100644 index 0000000000000000000000000000000000000000..a187667bea96dbbe6b8333d88b9766e9ca215c2a --- /dev/null +++ b/tests/testthat/test-creer_graphe_1_1.R @@ -0,0 +1,8 @@ + + test_that("creer_graphe_1_1 fonctionne", { + + # Test que le graphe est un ggplot + objet <- creer_graphe_1_1(millesime_teruti = 2018) + testthat::expect_equal(attr(objet, "class"), c("gg","ggplot")) + + }) diff --git a/vignettes/.gitignore b/vignettes/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..3432c3fd8eb0e59cc1b0e8ea0307591a5bcdea40 --- /dev/null +++ b/vignettes/.gitignore @@ -0,0 +1,2 @@ +*.html + diff --git a/vignettes/aa-ch1-1.Rmd b/vignettes/aa-ch1-1.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..bd190767192f8890076b8e4fde57ba1bc142e918 --- /dev/null +++ b/vignettes/aa-ch1-1.Rmd @@ -0,0 +1,26 @@ +--- +title: "aa- Chapitre 1 Graphe 1" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{aa- Chapitre 1 Graphe 1} + %\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_graphe_1_1()` produit le graphique des taux d'artificialisation par region (Teruti-Lucas). + +```{r setup} +library(propre.artificialisation) + + creer_graphe_1_1(millesime_teruti=2018) +``` diff --git a/vignettes/test.R b/vignettes/test.R new file mode 100644 index 0000000000000000000000000000000000000000..918650ea63fd3646e125161ce7d2e7d80fedbf57 --- /dev/null +++ b/vignettes/test.R @@ -0,0 +1,55 @@ +library(dplyr) +millesime_teruti <- 2018 + +# Creation de la table utile a la production du graphique + +data_france <- teruti %>% + dplyr::mutate(valeur=as.numeric(.data$valeur)) %>% + dplyr::filter(.data$CodeZone == "FRMETRO", + .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 (taux_artificialisation=.data$sols_artificiels/.data$tous_sols*100) +surf_artificialise_france <- data_france %>% + dplyr::pull(.data$sols_artificiels) + +data <- teruti %>% + dplyr::mutate(valeur=as.numeric(.data$valeur)) %>% + # Filtres pour ne garder que les donnees de la region et du millesime sélectionne + dplyr::filter(.data$TypeZone == "R\u00e9gions", + !(.data$CodeZone %in% c("01","02","03","04","06")), + .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 (taux_artificialisation=.data$sols_artificiels/.data$tous_sols*100, + part_dans_surface_nationale=.data$sols_artificiels/surf_artificialise_france*100, + couleur_barre = case_when( + .data$codezone=="52"~ 1, + TRUE ~ 0) + ) %>% + dplyr::select(.data$zone,.data$taux_artificialisation,.data$part_dans_surface_nationale,.data$couleur_barre) + +valeur_max <- max(data$taux_artificialisation,na.rm=T) + +graph_1_1<-data %>% + ggplot2::ggplot(ggplot2::aes(x=forcats::fct_reorder(.data$zone,.data$taux_artificialisation,.desc=T), ymax=valeur_max+4, y=.data$taux_artificialisation,fill=.data$couleur_barre)) + + ggplot2::geom_bar(stat="identity")+ + ggplot2::coord_flip() + + ggplot2::theme_classic()+ + ggplot2::theme(legend.position = "none")+ + ggplot2::geom_text(ggplot2::aes(y=.data$taux_artificialisation , label=paste0(round(.data$taux_artificialisation,1),"%")),position= ggplot2::position_dodge(width=1), vjust=0.5,hjust=-0.5, size=3)+ + ggplot2::labs(title="taux d'artificialisation par r\u00e9gion (Teruti-Lucas)",subtitle="",x="",y="", + fill="")+ + ggplot2::theme(axis.text.x = ggplot2::element_text()) + +return(graph_1_1) + + + + + + + + + +