From a5a2cf06bae68803e45311d0c8540d78eb46c1eb Mon Sep 17 00:00:00 2001
From: Daniel Kalioudjoglou
 <daniel.kalioudjoglou@developpement-durable.gouv.fr>
Date: Wed, 18 Sep 2024 16:52:30 +0200
Subject: [PATCH] modification du graphique series_temporelles_prix

affichage de texte dans les graphiques vides
---
 DESCRIPTION                                 |  9 ++--
 NAMESPACE                                   |  3 ++
 R/creer_graphique_series_temporelles.R      |  1 -
 R/creer_graphique_series_temporelles_prix.R | 47 ++++++++++++++++-----
 R/globals.R                                 |  3 +-
 devstuff.R                                  |  2 +-
 man/indic_ecln.Rd                           |  2 +-
 7 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index 27b1066..2a02230 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,5 +1,6 @@
 Package: propre.ecln
-Title: Publication conjoncturelle sur la commercialisation de logement neufs
+Title: Publication conjoncturelle sur la commercialisation de logement
+    neufs
 Version: 0.0.0.9000
 Authors@R: c(
     person("Maël", "THEULIERE", , "mael.theuliere@gmail.com", role = c("aut", "cre"),
@@ -7,7 +8,8 @@ Authors@R: c(
     person("Daniel", "Kalioudjoglou", , "daniel.kalioudjoglou@developpement-durable.gouv.fr", role = "aut"),
     person("Denis", "Douillard", , "denis.douillard@developpement-durable.gouv.fr", role = "aut")
   )
-Description: Template Rmarkdown et fonctions pour réaliser la publication trimestrielle sur la commercialisation de logement neufs.
+Description: Template Rmarkdown et fonctions pour réaliser la publication
+    trimestrielle sur la commercialisation de logement neufs.
 License: GPL (>= 3)
 Depends: 
     R (>= 2.10)
@@ -34,7 +36,8 @@ Imports:
     scales,
     sf,
     stringr,
-    tidyr
+    tidyr,
+    utils
 Remotes: 
     gitlab::dreal-datalab/drealdown,
     gitlab::dreal-datalab/mapfactory,
diff --git a/NAMESPACE b/NAMESPACE
index 94fc2cc..10c660f 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -28,12 +28,14 @@ importFrom(dplyr,case_when)
 importFrom(dplyr,distinct)
 importFrom(dplyr,filter)
 importFrom(dplyr,full_join)
+importFrom(dplyr,group_by)
 importFrom(dplyr,left_join)
 importFrom(dplyr,mutate)
 importFrom(dplyr,pull)
 importFrom(dplyr,rename)
 importFrom(dplyr,select)
 importFrom(dplyr,slice_max)
+importFrom(dplyr,summarise)
 importFrom(drealdown,my_icon)
 importFrom(forcats,fct_drop)
 importFrom(forcats,fct_explicit_na)
@@ -56,6 +58,7 @@ importFrom(ggplot2,geom_label)
 importFrom(ggplot2,geom_line)
 importFrom(ggplot2,geom_point)
 importFrom(ggplot2,geom_sf)
+importFrom(ggplot2,geom_text)
 importFrom(ggplot2,ggplot)
 importFrom(ggplot2,guide_legend)
 importFrom(ggplot2,guides)
diff --git a/R/creer_graphique_series_temporelles.R b/R/creer_graphique_series_temporelles.R
index ba80ea9..406000d 100644
--- a/R/creer_graphique_series_temporelles.R
+++ b/R/creer_graphique_series_temporelles.R
@@ -15,7 +15,6 @@
 #' @importFrom gouvdown scale_color_gouv_discrete
 #' @importFrom scales format_format
 #' @importFrom geofacet facet_geo
-#' @examples
 creer_graphique_series_temporelles <- function(data = indic_ecln,
                                                type_logement = "Appartements",
                                                indicateurs = c("Encours", "Mises en vente", "Ventes"),
diff --git a/R/creer_graphique_series_temporelles_prix.R b/R/creer_graphique_series_temporelles_prix.R
index 26c4d4a..4246990 100644
--- a/R/creer_graphique_series_temporelles_prix.R
+++ b/R/creer_graphique_series_temporelles_prix.R
@@ -11,13 +11,12 @@
 #'
 #' @return Un graphique ggplot2.
 #' @export
-#' @importFrom dplyr filter mutate
+#' @importFrom dplyr filter mutate group_by summarise select case_when
 #' @importFrom forcats fct_drop
-#' @importFrom ggplot2 ggplot aes geom_line geom_point theme scale_x_date scale_y_continuous labs facet_wrap element_line
+#' @importFrom ggplot2 ggplot aes geom_line geom_point theme scale_x_date scale_y_continuous labs facet_wrap element_line geom_text
 #' @importFrom scales dollar_format
 #' @importFrom stringr str_split_fixed
 #' @importFrom gouvdown scale_color_gouv_discrete
-#' @examples
 creer_graphique_series_temporelles_prix <- function(data = indic_ecln,
                                                     type_logement = "Logements",
                                                     type_zone = c("R\u00e9gions", "D\u00e9partements"),
@@ -44,6 +43,24 @@ creer_graphique_series_temporelles_prix <- function(data = indic_ecln,
       Indicateur = stringr::str_split_fixed(.data$Indicateur, " - ", 2)[, 1]
     )
 
+  # creation des annotations pour zonages avec aucune donnée
+  annotations <- data_prep %>%
+    dplyr::group_by(.data$Zone) %>%
+    dplyr::summarise(som_valeur = sum(.data$Valeur,na.rm=TRUE)) %>%
+    # dplyr::mutate(Zone= as.character(.data$Zone)) %>%
+    dplyr::mutate(
+      x = mean(range(data_prep$Periode)),
+      y = 4000,
+      Indicateur = "xx") %>%
+    dplyr::mutate(mon_texte = dplyr::case_when(
+      som_valeur == 0 ~ "Donn\u00e9es\nnon disponibles\nen raison\ndu secret\nstatistique",
+      TRUE ~ ""
+    )) %>%
+    dplyr::mutate(color_axe_y = dplyr::case_when(
+      som_valeur == 0 ~ "red",
+      TRUE ~ "black"
+    )) %>%
+    dplyr::select(-som_valeur)
 
   p <- ggplot2::ggplot(
     data_prep,
@@ -56,13 +73,6 @@ creer_graphique_series_temporelles_prix <- function(data = indic_ecln,
     )
   ) +
     ggplot2::geom_line(size = 1.2) +
-    # ggplot2::geom_point(data = data %>%
-    #   dplyr::filter(
-    #     .data$Indicateur %in% indic,
-    #     .data$TypeIndicateur == type_indicateur,
-    #     .data$TypeZone %in% type_zone,
-    #     .data$Periode == max(.data$Periode)
-    #   )) +
     ggplot2::theme(legend.position = "none",
                    legend.title = element_blank(),
                    panel.grid.major = element_line(color = "lightgrey", linetype = "solid"),
@@ -72,7 +82,7 @@ creer_graphique_series_temporelles_prix <- function(data = indic_ecln,
                    axis.text.x = element_text(size=11, hjust=-0.1),
                    axis.line = element_line(size = 0.5, linetype = "solid")) +
     ggplot2::scale_x_date(date_labels = "%y", expand = c(0, 0), date_breaks = "1 year") +
-    ggplot2::scale_y_continuous(labels = scales::dollar_format(big.mark = " ", decimal_mark = ",", prefix = "", suffix = " \u20ac")) +
+    ggplot2::scale_y_continuous(labels = scales::dollar_format(big.mark = " ", decimal_mark = ",", prefix = "", suffix = " \u20ac",accuracy = 1)) +
     gouvdown::scale_color_gouv_discrete(palette = "pal_gouv_fr") +
     ggplot2::labs(
       title = titre,
@@ -82,6 +92,9 @@ creer_graphique_series_temporelles_prix <- function(data = indic_ecln,
       color = "",
       caption = bas_de_page
     )
+
+
+
   if (nlevels(data_prep$Zone) > 1) {
     if (type_facet == "classique") {
       p <- p +
@@ -93,6 +106,18 @@ creer_graphique_series_temporelles_prix <- function(data = indic_ecln,
         geofacet::facet_geo(~Zone, grid = mygrid, scales = "free", drop = TRUE)
     }
   }
+
+  p <- p +
+   ggplot2::geom_text(data = annotations,
+                              ggplot2::aes(x = x,
+                                           y = y,
+                                           label = mon_texte),
+                              color = "blue",
+                              size = 4)
+  # ggplot2::scale_y_continuous(labels = scales::dollar_format(big.mark = " ", decimal_mark = ",", prefix = "", suffix = " \u20ac"))
+
+
+
   return(p)
 }
 
diff --git a/R/globals.R b/R/globals.R
index fc3c027..419c5e5 100644
--- a/R/globals.R
+++ b/R/globals.R
@@ -1,3 +1,4 @@
 utils::globalVariables(
-  c("liste_zone", "indic_ecln", "%m-%","mygrid","params","Valeur",".")
+  c("liste_zone", "indic_ecln", "%m-%","mygrid","params",
+    "Valeur",".","mon_texte","som_valeur","x","y")
 )
diff --git a/devstuff.R b/devstuff.R
index 7ce034c..b537160 100644
--- a/devstuff.R
+++ b/devstuff.R
@@ -25,4 +25,4 @@ usethis::use_package('tidyr')
 usethis::use_r("tableau_synthese_trimestrielle")
 
 attachment::att_amend_desc()
-yusethis::use_r("globals.R")
+usethis::use_r("globals.R")
diff --git a/man/indic_ecln.Rd b/man/indic_ecln.Rd
index 4b77153..3a09558 100644
--- a/man/indic_ecln.Rd
+++ b/man/indic_ecln.Rd
@@ -6,7 +6,7 @@
 \alias{indic_ecln}
 \title{Table contenant les différents indicateurs de la source ecln utiles pour la publication.}
 \format{
-Table de 2535180 lignes et 9 colonnes:
+Table de 96720 lignes et 10 colonnes:
 \describe{
 \item{TypeZone}{Type de territoire}
 \item{CodeZone}{Code du territoire}
-- 
GitLab