diff --git a/R/creer_graphe_3_4.R b/R/creer_graphe_3_4.R index 8c36b27fbabb17dbd16aef5b5468a6993dd28ac2..8ceb5da74e9f289ddb73f2929a6771f7efef54cb 100644 --- a/R/creer_graphe_3_4.R +++ b/R/creer_graphe_3_4.R @@ -25,12 +25,12 @@ creer_graphe_3_4 <- function(millesime_stock_artif = NULL,millesime_population = NULL, code_reg = NULL){ - attempt::stop_if(millesime_stock_artif, is.null, msg = "millesime_stock_artif n'est pas renseign\u00e9") - attempt::stop_if_not(millesime_stock_artif, is.numeric, msg = "millesime_stock_artif n'est pas un nombre") - attempt::stop_if(millesime_population, is.null, msg = "millesime_population n'est pas renseign\u00e9") - attempt::stop_if_not(millesime_population, is.numeric, msg = "millesime_population 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") + attempt::stop_if(millesime_stock_artif, is.null, msg = "millesime_stock_artif n\'est pas renseign\u00e9") + attempt::stop_if_not(millesime_stock_artif, is.numeric, msg = "millesime_stock_artif n\'est pas un nombre") + attempt::stop_if(millesime_population, is.null, msg = "millesime_population n\'est pas renseign\u00e9") + attempt::stop_if_not(millesime_population, is.numeric, msg = "millesime_population 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") # # calcul des millesimes utilises millesime_debut <- 2016 @@ -89,7 +89,7 @@ creer_graphe_3_4 <- function(millesime_stock_artif = NULL,millesime_population = dplyr::left_join(flux) %>% dplyr::left_join(evol_popul) %>% dplyr::left_join(popul_debut) %>% - dplyr::mutate(Zone=as.factor(.data$Zone)) %>% + dplyr::mutate(Zone = as.factor(.data$Zone)) %>% dplyr::mutate(surf_artif_par_hab_an_x=.data$stock_depart / .data$population_debut *10000 ) %>% dplyr::mutate(surf_artif_par_nouv_hab_entre_x_y =.data$flux_artificialisation / .data$evolution_population *10000) %>% dplyr::select (.data$TypeZone,.data$Zone,.data$surf_artif_par_hab_an_x,.data$surf_artif_par_nouv_hab_entre_x_y) %>% @@ -97,26 +97,31 @@ creer_graphe_3_4 <- function(millesime_stock_artif = NULL,millesime_population = dplyr::arrange(desc(.data$TypeZone),desc(.data$Zone)) %>% dplyr::mutate(variable=forcats::fct_relevel(.data$variable,"surf_artif_par_nouv_hab_entre_x_y","surf_artif_par_hab_an_x")) %>% dplyr::mutate(Zone = forcats::fct_drop(.data$Zone) %>% - forcats::fct_inorder()) + forcats::fct_inorder()) %>% + dplyr::mutate(valeur = ifelse(.data$valeur<0, 0, valeur)) # creation du graphique valeur_max_graphique <- max(data$valeur,na.rm=T)+500 - graph_3_4 <- data %>% ggplot2::ggplot(ggplot2::aes(x=.data$Zone,y=.data$valeur,fill=.data$variable), width = 0.9) + - ggplot2::geom_bar(stat="identity",position = ggplot2::position_dodge())+ - ggplot2::coord_flip() + - ggplot2::geom_text(ggplot2::aes(label=format(round(.data$valeur,0), big.mark = " "),color = .data$variable),position= ggplot2::position_dodge(width=1), hjust=-0.1)+ - ggplot2::scale_fill_manual(values = c("#82534b","#FF8D7E")) + - ggplot2::scale_color_manual(values = c("#82534b","#FF8D7E")) + + graph_3_4 <- data %>% + ggplot2::ggplot(ggplot2::aes(x = .data$Zone, y = .data$valeur,fill = .data$variable), width = 0.9) + + ggplot2::geom_bar(stat = "identity", position = ggplot2::position_dodge()) + + ggplot2::coord_flip() + + ggplot2::geom_text(ggplot2::aes(label = ifelse(.data$valeur>0, format(round(.data$valeur,0), big.mark = " "), "population en baisse"), + color = .data$variable), + position = ggplot2::position_dodge(width = 1), + hjust = -0.1) + + ggplot2::scale_color_manual(values = gouvdown::gouv_palettes$pal_gouv_h[1:2]) + + ggplot2::scale_fill_manual(values = gouvdown::gouv_palettes$pal_gouv_h[1:2]) + ggplot2::labs( - title = glue::glue("Surfaces cadastr\u00e9es artificialis\u00e9es (m2)", width = 60), - subtitle = glue::glue("<span style = 'color:#FF8D7E'>par habitant pr\u00e9sent en {millesime_debut_population}</span> et <span style = 'color:#82534b'>par nouvel habitant entre {millesime_debut_population} et {millesime_fin_population}</span> "), - x = "", y = "", + title = glue::glue("Surfaces cadastr\u00e9es artificialis\u00e9es (m\u00b2)", width = 60), + subtitle = glue::glue("<span style = \'color:#FF8D7E\'>par habitant pr\u00e9sent en {millesime_debut_population}</span> et <span style = \'color:#D6857B\'>par nouvel habitant entre {millesime_debut_population} et {millesime_fin_population}</span> "), + x = "", + y = "", fill = "", - caption = glue::glue("Source : DGFip Majic/Cerema Fichiers fonciers - Insee - {millesime_stock_artif}") + caption = glue::glue("Source : Cerema Fichiers fonciers {millesime_stock_artif} - Insee Population l\u00e9gale {millesime_population} ") ) + ggplot2::scale_y_continuous( - # limits = c(0, max(data$valeur) +500), labels = scales::label_number(big.mark = " ", decimal.mark = ",") ) + ggplot2::theme( diff --git a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd index 130f0768c58b7fe93a2dd25ae2d2ea7887695388..aac91978a398921b0da1c2f29ba6cbb9f17a71c8 100644 --- a/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/publication/skeleton/skeleton.Rmd @@ -11,7 +11,7 @@ params: millesime_obs_artif: 2020 millesime_etalement_urbain: 2020 millesime_obs_artif_gk3: 2019 - millesime_population: 2018 + millesime_population: 2019 millesime_stock_artif: 2020 code_reg: 52 title: "L'artificialisation des sols en `r mapfactory::get_nom_reg(params$code_reg)` en `r params$millesime_obs_artif`" @@ -156,8 +156,8 @@ creer_graphe_3_3(millesime_stock_artif = params$millesime_stock_artif, ## 4 - En Loire-Atlantique, un nouvel habitant consommera moins d'espace que la population en place. -```{r graph surfaces artificialisees par habitant,fig.width=10,fig.height=7} -creer_graphe_3_4(millesime_stock_artif = params$millesime_obs_artif_gk3, +```{r graph surfaces artificialisees par habitant,fig.width=9,fig.height=7} +creer_graphe_3_4(millesime_stock_artif = params$millesime_stock_artif, millesime_population = params$millesime_population, code_reg = params$code_reg) ``` diff --git a/vignettes/ca-ch3.Rmd b/vignettes/ca-ch3.Rmd index e6bb3be4f30d0a60c7cce392be5b9c455ed1e610..9f4e7f76865aae64ac39b558de60652ea0e77457 100644 --- a/vignettes/ca-ch3.Rmd +++ b/vignettes/ca-ch3.Rmd @@ -65,7 +65,5 @@ creer_graphe_3_3(millesime_stock_artif = 2020, La fonction `creer_graphe_3_4()` produit le graphique des surfaces artificialisées pour un nouvel habitant par département. ```{r creer_graphe_3_4, fig.height=5,fig.width=8} -creer_graphe_3_4(millesime_stock_artif = 2020, - millesime_population = 2019, - code_reg = 52) +creer_graphe_3_4(millesime_stock_artif = 2020, millesime_population = 2019, code_reg = 52) ```