Skip to content
Snippets Groups Projects

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

7 files
+ 157
1
Compare changes
  • Side-by-side
  • Inline
Files
7
+ 78
0
#' Creation du graphique du taux d'evolution 2009/19 (%) de l'artificialisation cadastree par aire d'attraction des villes.
#' @description Graphique du taux d'evolution 2009/19 (%) de l'artificialisation cadastree par aire d'attraction des villes.
#'
#' @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 attempt stop_if stop_if_not
#' @importFrom COGiter charger_zonage ajouter_typologie
#' @importFrom dplyr filter mutate select group_by lag ungroup
#' @importFrom forcats fct_drop fct_inorder fct_rev
#' @importFrom ggplot2 ggplot aes geom_bar geom_label position_dodge coord_flip labs theme element_text scale_y_continuous
#' @importFrom glue glue
#' @importFrom lubridate year
#' @importFrom mapfactory format_fr_pct_2
#' @importFrom scales percent_format
#' @importFrom stringr str_detect
#' @export
#' @example
#' creer_graphe_2_6(millesime_obs_artif_gk3=2019,code_reg = 52)
creer_graphe_2_6 <- function(millesime_obs_artif_gk3= NULL,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% "52", msg = "code_reg n'est pas un code r\u00e9gion valide")
zonage_aav <- COGiter::charger_zonage("CATEAAV2020")
millesime_debut <- millesime_obs_artif_gk3 - 9
data <- COGiter::ajouter_typologie(observatoire_artificialisation_gk3,
zonage_df = zonage_aav,
departements = FALSE,
regions = TRUE,
metro = FALSE,
metrodrom = FALSE,
drom = FALSE, franceprovince = FALSE
) %>%
dplyr::filter(
(.data$TypeZone %in% c("CATEAAV2020 - R\u00e9gions")),
stringr::str_detect(.data$CodeZone, "NA", negate = TRUE)
) %>%
dplyr::mutate(date=lubridate::year(.data$date)) %>%
dplyr::filter(.data$date == millesime_obs_artif_gk3 | .data$date == millesime_debut) %>%
dplyr::select (-.data$surface_naf) %>%
dplyr::group_by(.data$TypeZone,.data$Zone,.data$CodeZone) %>%
dplyr::mutate(evolution = (.data$surface_artificialisee - dplyr::lag(.data$surface_artificialisee))*100/ dplyr::lag(.data$surface_artificialisee)) %>%
dplyr::ungroup() %>%
dplyr::filter(.data$date == millesime_obs_artif_gk3) %>%
dplyr::mutate(Zone = forcats::fct_drop(.data$Zone) %>%
forcats::fct_inorder() %>%
forcats::fct_rev()) %>%
dplyr::select(.data$Zone,.data$evolution)
valeur_max <- max(data$evolution,na.rm=T)
graph_2_6<-data %>%
ggplot2::ggplot(ggplot2::aes(x=.data$Zone, y=.data$evolution)) +
ggplot2::geom_bar(stat="identity",fill="#FF8D7E",width=.5)+
ggplot2::geom_label(ggplot2::aes(y=.data$evolution ,
label=mapfactory::format_fr_pct_2(.data$evolution)
),
position= ggplot2::position_dodge(width=1),
vjust=0.5,
hjust=-0.5,
size=3,
color ="#FF8D7E",
fill = "#FF8D7E30") +
ggplot2::coord_flip(expand = FALSE) +
ggplot2::labs(title=glue::glue("\u00c9volution {millesime_debut} - {millesime_obs_artif_gk3} en hectares\nde l'artificialisation cadastr\u00e9e par aire d'attraction des villes"),
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::percent_format(accuracy = 1, big.mark = " ",scale=1),limits = c(0,valeur_max*1.2))
return(graph_2_6)
}
Loading