Skip to content
Snippets Groups Projects
Commit ae8b7c8f authored by Maël Theulière's avatar Maël Theulière
Browse files

initialisation `creer_carte_volume()`

parent 3469fff9
No related branches found
No related tags found
No related merge requests found
Pipeline #132437 failed
......@@ -3,6 +3,7 @@
export("%>%")
export(FormatCaractere)
export(FormatDate)
export(creer_carte_volume)
export(creer_chiffres_clefs_ecln)
export(creer_graphique_evolution_annuelle)
export(creer_graphique_evolution_trim)
......@@ -14,6 +15,9 @@ export(creer_tableau_synthese_trimestrielle)
export(data_prep)
export(set_theme)
importFrom(COGiter,filtrer_cog)
importFrom(COGiter,filtrer_cog_geo)
importFrom(COGiter,list_epci_in_reg)
importFrom(COGiter,nom_zone)
importFrom(attempt,stop_if_not)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
......@@ -21,8 +25,10 @@ importFrom(dplyr,case_when)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,full_join)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,pull)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(drealdown,my_icon)
importFrom(forcats,fct_drop)
......@@ -33,26 +39,36 @@ importFrom(geofacet,facet_geo)
importFrom(ggforce,geom_mark_circle)
importFrom(ggplot2,aes)
importFrom(ggplot2,coord_flip)
importFrom(ggplot2,coord_sf)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_rect)
importFrom(ggplot2,facet_wrap)
importFrom(ggplot2,geom_area)
importFrom(ggplot2,geom_bar)
importFrom(ggplot2,geom_label)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_sf)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,guides)
importFrom(ggplot2,labs)
importFrom(ggplot2,margin)
importFrom(ggplot2,position_dodge)
importFrom(ggplot2,scale_size)
importFrom(ggplot2,scale_x_date)
importFrom(ggplot2,scale_y_continuous)
importFrom(ggplot2,stat_sf_coordinates)
importFrom(ggplot2,theme)
importFrom(ggplot2,theme_set)
importFrom(ggspatial,annotation_north_arrow)
importFrom(ggspatial,annotation_scale)
importFrom(ggspatial,north_arrow_fancy_orienteering)
importFrom(glue,glue)
importFrom(gouvdown,gouv_colors)
importFrom(gouvdown,scale_color_gouv_discrete)
importFrom(gouvdown,scale_fill_gouv_discrete)
importFrom(gouvdown,theme_gouv)
importFrom(gouvdown,theme_gouv_map)
importFrom(grid,unit)
importFrom(kableExtra,add_header_above)
importFrom(kableExtra,add_indent)
......@@ -68,9 +84,12 @@ importFrom(lubridate,year)
importFrom(lubridate,years)
importFrom(lubridate,ymd)
importFrom(magrittr,"%>%")
importFrom(mapfactory,fond_carto)
importFrom(rlang,.data)
importFrom(scales,dollar_format)
importFrom(scales,format_format)
importFrom(sf,st_bbox)
importFrom(sf,st_crs)
importFrom(stringr,str_c)
importFrom(stringr,str_replace)
importFrom(stringr,str_split_fixed)
......
#' Création de la carte en volume sur les indicateurs de commercialisation de logements neufs
#'
#' @param data Le dataframe.
#' @param code_region Le code de la région.
#' @param indicateurs Nom de l'indicateur.
#' @param type_logement Appartements ou Maisons.
#' @param titre Le titre du graphique.
#' @param titre_legende Le titre de la légende.
#' @param bas_de_page Le bas de page du graphique.
#'
#' @return une carte en ggplot2
#' @export
#' @importFrom COGiter filtrer_cog_geo list_epci_in_reg nom_zone
#' @importFrom dplyr filter rename left_join mutate
#' @importFrom ggplot2 ggplot geom_sf stat_sf_coordinates coord_sf scale_size theme element_rect element_blank labs
#' @importFrom ggspatial annotation_north_arrow north_arrow_fancy_orienteering annotation_scale
#' @importFrom gouvdown theme_gouv_map
#' @importFrom mapfactory fond_carto
#' @importFrom sf st_bbox st_crs
creer_carte_volume<-function(data=indic_ecln,
code_region = params$reg,
indicateurs = c("Encours","Mises en vente","Ventes"),
type_logement="Appartements",
titre="",
titre_legende="",
bas_de_page=""){
# On ne garde que les données des ventes pour réaliser les classes de valeur
indic <- paste0(indicateurs," - ",type_logement)
epci_geo <- COGiter::filtrer_cog_geo(reg = code_region,garder_supra = TRUE)$epci
data_pour_carte<-data %>%
dplyr::filter(Indicateur %in% indic,
TypeZone=="EPCI"
) %>%
dplyr::rename(EPCI=CodeZone)
epci_reg <- COGiter::list_epci_in_reg(code_region)
data_prep<-epci_geo %>%
dplyr::left_join(data_pour_carte) %>%
dplyr::mutate(epci_reg = EPCI %in% epci_reg,
indicateur_positif = Valeur>0)
# Pour la Corse : définition de l'emprise de la carte : on veut 30 km de vide autour de la région et une carte plus large que haute
bbox_reg <- sf::st_bbox(data_prep %>% filter(epci_reg))
fond_carte <- mapfactory::fond_carto(nom_reg = COGiter::nom_zone("R\u00e9gions",code_region))
if(code_region == "94"){
hauteur = bbox_reg$ymax - bbox_reg$ymin + 30000
largeur = bbox_reg$xmax - bbox_reg$xmin + 30000
x_mil = bbox_reg$xmin + largeur/2
y_min = bbox_reg$ymin - 15000
y_max = bbox_reg$ymax + 15000
x_min = ifelse(largeur >= hauteur, bbox_reg$xmin - 15000, x_mil - hauteur/2)
x_max = ifelse(largeur >= hauteur, bbox_reg$xmax + 15000, x_mil + hauteur/2)
} else {
y_min = bbox_reg$ymin
y_max = bbox_reg$ymax
x_min = bbox_reg$xmin
x_max = bbox_reg$xmax
}
p<-ggplot2::ggplot() +
ggplot2::geom_sf(data = fond_carte$epci, fill = "light grey", color = "white", size = .1) +
ggplot2::geom_sf(data = fond_carte$reg_ombre, fill = "dark grey", color = "light grey") +
ggplot2::geom_sf(data = fond_carte$regions %>% filter(REG == code_region), fill = "grey", color = "white",size = .2) +
ggplot2::geom_sf(data=data_prep %>% dplyr::filter(epci_reg), color="white",size=.1,fill = "#B8B8B8")+
ggplot2::geom_sf(data=data_prep %>% dplyr::filter(epci_reg,indicateur_positif), color="white",size=.1,fill = "#fff5f1")+
ggplot2::geom_sf(data = fond_carte$departements,fill=NA,color = "white",size = .2) +
ggplot2::stat_sf_coordinates(data=data_prep %>% dplyr::filter(epci_reg,indicateur_positif), aes(size = Valeur),shape = 21, color = "white", fill = "#000091") +
ggplot2::coord_sf(
xlim = c(x_min, x_max),
ylim = c(y_min, y_max),
expand = FALSE,
crs = sf::st_crs(fond_carte$epci),
datum = NA
) +
ggplot2::scale_size(range = c(5/length(indic),20/length(indic))) +
ggspatial::annotation_north_arrow(location = "br", style = ggspatial::north_arrow_fancy_orienteering) +
ggspatial::annotation_scale() +
gouvdown::theme_gouv_map(base_size=12,
strip_text_size = 12,
plot_title_size = 20,
subtitle_size = 16) +
ggplot2::theme(panel.background = ggplot2::element_rect(fill = "light blue"),
plot.background=ggplot2::element_rect(fill="#ffffff",color="#ffffff"),
legend.position = "bottom",
legend.title = ggplot2::element_blank(),
plot.caption.position = "plot"
) +
ggplot2::labs(fill=titre_legende,
caption=bas_de_page,
title=titre,
x = NULL,y=NULL)
if (length(indic) > 1) {
p <-p+ facet_wrap(~Indicateur,ncol=3)
}
return(p)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/creer_carte.R
\name{creer_carte_volume}
\alias{creer_carte_volume}
\title{Création de la carte en volume sur les indicateurs de commercialisation de logements neufs}
\usage{
creer_carte_volume(
data = indic_ecln,
code_region = params$reg,
indicateurs = c("Encours", "Mises en vente", "Ventes"),
type_logement = "Appartements",
titre = "",
titre_legende = "",
bas_de_page = ""
)
}
\arguments{
\item{data}{Le dataframe.}
\item{code_region}{Le code de la région.}
\item{indicateurs}{Nom de l'indicateur.}
\item{type_logement}{Appartements ou Maisons.}
\item{titre}{Le titre du graphique.}
\item{titre_legende}{Le titre de la légende.}
\item{bas_de_page}{Le bas de page du graphique.}
}
\value{
une carte en ggplot2
}
\description{
Création de la carte en volume sur les indicateurs de commercialisation de logements neufs
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment