Skip to content
Snippets Groups Projects

Resolve "graphique des taux de croissance annuels entre 2009 et 2019 par type de territoires"

3 files
+ 22
14
Compare changes
  • Side-by-side
  • Inline
Files
3
  • b2a96c55
    mise à jour creer_graph_2_3 · b2a96c55
    mael.theuliere authored
    - intégration du paramétre code_reg pour sélectionner une autre région
    - correction de la datapréparation
    - correction du titre
    - correction de la gestion de la borne max
+ 17
11
@@ -2,10 +2,10 @@
#' @description Graphique du taux d'evolution 2009/19 (%) de l'artificialisation cadastree des differentes regions.
#'
#' @param millesime_obs_artif_gk3 une année parmi les millesimes sélectionnables par l'utilisateur, au format numerique.
#'
#' @param code_reg code insee de la région sur laquelle construire le graphique
#' @return Un diagramme en barres
#'
#' @importFrom dplyr mutate filter select group_by case_when
#' @importFrom dplyr mutate filter select group_by case_when summarise
#' @importFrom forcats fct_reorder
#' @importFrom ggplot2 ggplot aes geom_bar geom_text position_dodge coord_flip labs theme element_text scale_y_continuous
#' @importFrom glue glue
@@ -15,28 +15,34 @@
#' @export
#'
#' @examples
#' creer_graphe_2_3(millesime_obs_artif_gk3=2019)
#' creer_graphe_2_3(millesime_obs_artif_gk3=2019,code_reg = "52")
creer_graphe_2_3 <- function(millesime_obs_artif_gk3){
creer_graphe_2_3 <- function(millesime_obs_artif_gk3,code_reg = NULL){
attempt::stop_if(millesime_obs_artif_gk3, is.null, msg = "millesime_obs_artif_gk3 n'est pas renseign\u00e9")
attempt::stop_if_not(millesime_obs_artif_gk3, is.numeric, msg = "millesime_obs_artif_gk3 n'est pas un nombre")
attempt::stop_if(code_reg, is.null, msg = "code_reg n'est pas renseign\u00e9")
attempt::stop_if_not(code_reg, ~ .x %in% levels(COGiter::regions$REG), msg = "code_reg n'est pas un code r\u00e9gion valide")
# Creation de la table utile a la production du graphique
data <- observatoire_artificialisation_gk3 %>%
dplyr::mutate(date=year(.data$date)) %>%
dplyr::mutate(date=lubridate::year(.data$date)) %>%
dplyr::filter(.data$TypeZone == "R\u00e9gions",
!(.data$CodeZone %in% c("01","02","03","04","06")),
.data$date == millesime_obs_artif_gk3 | .data$date == millesime_obs_artif_gk3 - 9) %>%
dplyr::select (-.data$surface_naf) %>%
dplyr::group_by(.data$TypeZone,.data$Zone,.data$CodeZone) %>%
summarise(evolution=round((sum(.data$surface_artificialisee[which(date==2019)],na.rm=T) - sum(.data$surface_artificialisee[which(date==2010)],na.rm=T))/10000,0)) %>%
dplyr::arrange(.data$date) %>%
dplyr::mutate(evolution = round(((.data$surface_artificialisee - dplyr::lag(.data$surface_artificialisee))/10000),0)) %>%
dplyr::filter(.data$date == millesime_obs_artif_gk3) %>%
dplyr::select(.data$TypeZone,.data$Zone,.data$CodeZone,.data$evolution) %>%
dplyr::mutate(couleur_barre = dplyr::case_when(
.data$CodeZone=="52"~ 1,
.data$CodeZone==code_reg~ 1,
TRUE ~ 0))
valeur_max <- max(data$evolution,na.rm=T)
millesime_debut <- millesime_obs_artif_gk3 - 9
graph_2_3<-data %>%
ggplot2::ggplot(ggplot2::aes(x=forcats::fct_reorder(.data$Zone,.data$evolution,.desc=F), ymax=valeur_max+5000, y=.data$evolution,fill=.data$couleur_barre)) +
ggplot2::ggplot(ggplot2::aes(x=forcats::fct_reorder(.data$Zone,.data$evolution,.desc=F), y=.data$evolution,fill=.data$couleur_barre)) +
ggplot2::geom_bar(stat="identity")+
ggplot2::geom_text(ggplot2::aes(y=.data$evolution ,
label=mapfactory::format_fr(.data$evolution, dec = 0),
@@ -47,12 +53,12 @@ creer_graphe_2_3 <- function(millesime_obs_artif_gk3){
hjust=-0.5,
size=3) +
ggplot2::coord_flip(expand = FALSE) +
ggplot2::labs(title=glue::glue("\u00e9volution {millesime_debut} - {millesime_obs_artif_gk3} en hectares\nde l'artificialisation cadastr\u00e9e par r\u00e9gion"),
ggplot2::labs(title=glue::glue("\u00c9volution {millesime_debut} - {millesime_obs_artif_gk3} en hectares\nde l'artificialisation cadastr\u00e9e par r\u00e9gion"),
subtitle="",x="",y="",
fill="",
caption = glue::glue("Source : Observatoire artificialisation {millesime_obs_artif_gk3}"))+
ggplot2::theme(axis.text.x = ggplot2::element_text(), legend.position = "none") +
ggplot2::scale_y_continuous(labels = scales::number_format(suffix = "", accuracy = 1, big.mark = " "))
ggplot2::scale_y_continuous(labels = scales::number_format(suffix = "", accuracy = 1, big.mark = " "),limits = c(0, valeur_max+5000))
return(graph_2_3)
Loading