1 Définition du répertoire de travail

Le répertoire de travail c’est le répertoire ou le dossier que R utilise pour chercher les fichiers ou sauvegarder les fichiers générés.

"E:/Logiciel R/CARTOGRAPHIE AVEC 
R/TP CARTO"
## [1] "E:/Logiciel R/CARTOGRAPHIE AVEC \nR/TP CARTO"

2 Chargement des packages utiles

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.7     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.3.2, PROJ 7.2.1; sf_use_s2() is TRUE
library(ggspatial)
library(readxl)
library(bookdown)
library(rmdformats)

3 Chargement et préparation des données

setwd("E:/Logiciel R/CARTOGRAPHIE AVEC R/TP CARTO")
cg <- st_read("Departements_du_CongoV.shp", quiet=TRUE)
centres <- read_excel("Centres d'état civil VF 29_06_2022.xlsx")
cg1 <- st_read("Departements_du_CongoV.shp", quiet=TRUE) 
d <- select(centres,"Département",Centres_principaux = "Centres principaux",
            Centres_secondaires = "Centres secondaires")
cg1 <- left_join(cg,d, by = c("Noms_Dép" = "Département"))

4 Production des cartes avec GGPLOT2

4.1 Carte choroplète simple

ggplot(cg1) +  geom_sf(aes(fill = Centres_principaux)) + 
  ggtitle("Répartition des centres d'état civil sécondaires par département") +
theme_void() 

4.2 Carte choroplète avec quelques éléments supplémentaires

 p <- ggplot(data=cg1)+
  geom_sf(fill="grey99")+ 
  geom_sf(aes(fill = Centres_secondaires))+ 
  ggtitle("Répartition des centres d'état civil sécondaires par département") +
  annotation_scale(location = "br", line_width = .5) +
  annotation_north_arrow(location = "tl", height = unit(0.7, "cm"),
                         width = unit(0.7, "cm")) +
  theme_void()
 p

4.3 Mise des labels avec geom_text

nc3 <- cg1 
nc3_points <- sf::st_point_on_surface(nc3)
## Warning in st_point_on_surface.sf(nc3): st_point_on_surface assumes attributes
## are constant over geometries of x
## Warning in st_point_on_surface.sfc(st_geometry(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
nc3_coords <- as.data.frame(sf::st_coordinates(nc3_points))
nc3_coords$NAME <- nc3$Noms_Dép
nc3_coords
##           X          Y          NAME
## 1  14.64418 -0.3259996 Cuvette-Ouest
## 2  16.28896 -0.5522947       Cuvette
## 3  17.36807  1.4804970      Likouala
## 4  15.42076 -2.0140998      Plateaux
## 5  13.49239 -4.1423867       Bouénza
## 6  12.48168 -3.3706274         Niari
## 7  13.62976 -3.0703926      Lékoumou
## 8  11.96267 -4.1927982       Kouilou
## 9  12.08291 -4.8477391  Pointe-Noire
## 10 15.59947  1.3784315        Sangha
## 11 15.11950 -3.8167735          Pool
## 12 15.32100 -4.2386605   Brazzaville
ggplot() +
  geom_sf(data = nc3, aes(fill = Centres_secondaires)) +
  geom_text(data = nc3_coords, aes(X, Y, label = NAME), colour = "white")

4.4 Production de la carte en passant par une transformation de l’objet sf (simple feature)

La transformation permet de projet les objets non-sf sur la carte. La projection que nous avons trouvée satisfaisante pour le Congo pour le Congo est celle pour laquelle EPSG = 2177. Les valeurs comprises entre 2164 et 2180 donnes également une bonne projection pour le Congo (après test).

# la bonne projection pour le Congo est celle dont la valeur est comprise entre 2164 et 2180
k <- sf::st_transform(
  cg1,2154) # la transformation conforme pour le Congo est 2164 ou 2174

k_2177 <- sf::st_transform(
  cg1,2177)
# carte avec k_2177
ggplot(data = k_2177)+ 
  geom_sf(aes(geometry=geometry, fill= Centres_principaux), size = .9)+
  labs(title="Subdivision de la République du Congo", subtitle="Département du Congo",caption="Source: INS, 2022")+  
  theme(plot.title=element_text(size=18, face="bold",color="blue",hjust= 0.1),
        plot.subtitle = element_text(size=10, face="bold",color="black",hjust= 0.6), 
        plot.caption=element_text(size= 12,face = "bold", color = "red"),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_line( size=0.5,linetype = "dotted"))

4.5 Production des cartes à ronds proportionnels

Les cartes à ronds proportionnel nécessite le calcul des centroïdes si l’on veut produire la carte en passant par la transformation suivante.

k_2177$centroid <- sf::st_centroid(k_2177$geometry)
ggplot(k_2177, size = 10) + 
  geom_sf(colour = "black") +
  geom_sf(aes(geometry = centroid, size = Centres_principaux,
              #color= "gray35",
              show.legend = "point")) +
  ggtitle("Carte à symboles proportionnels"  )+
  geom_sf_text(aes(label =Noms_Dép),size= 3 ,family="sans",color = "gray21")+
  #theme_void()
  theme(plot.title=element_text(size=18, face="bold",color="blue",hjust= 0.3),
        plot.subtitle = element_text(size=10, face="bold",color="black",hjust= 0.6), 
        plot.caption=element_text(size= 12,face = "bold", color = "red"),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_line( size=0.1,linetype = "dotted")) +
  annotation_scale(location = "br", line_width = .5) +
  annotation_north_arrow(location = "tl", height = unit(0.7, "cm"),
                         width = unit(0.7, "cm"))
## Warning: Ignoring unknown aesthetics: show.legend

4.6 Carte choroplèthe et à fonds proportionnels

ggplot(k_2177, size = 10) + 
  geom_sf(colour = "black") +
  geom_sf(aes(geometry = centroid, size = Centres_principaux,
              color= Centres_secondaires,
              show.legend = "point")) +
  ggtitle("Carte à symboles proportionnels et choroplèthe"  )+
  geom_sf_text(aes(label =Noms_Dép),size= 3 ,family="sans",color = "gray21")+
  #theme_void()
  theme(plot.title=element_text(size=18, face="bold",color="blue",hjust= 0.3),
        plot.subtitle = element_text(size=10, face="bold",color="black",hjust= 0.6), 
        plot.caption=element_text(size= 12,face = "bold", color = "red"),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_line( size=0.1,linetype = "dotted")) +
  annotation_scale(location = "br", line_width = .5) +
  annotation_north_arrow(location = "tl", height = unit(0.7, "cm"),
                         width = unit(0.7, "cm"))
## Warning: Ignoring unknown aesthetics: show.legend

5 Une façon de produire les cartes avec GGPLOT2

Au lieu de calculer les centroïdes, une autre façon de produire les cartes à ronds proportionnel est d’utiliser le fonction stat_sf_coordinates() . Cette fonction permet l’extraction des coordonnées d’un objet ‘sf’ et les résume en une paire de coordonnées (x et y) par géométrie. C’est pratique lorsque vous dessinez un objet sf sous forme de geoms comme du texte et des étiquettes (donc geom_sf_text() et geom_sf_label() s’appuient sur cela. Cette fonction évite de passer par le calcul des centroïdes comme vu dans la section 4.5 ainsi que dans la section 4.4

v <- left_join(cg,d, by = c("Noms_Dép" = "Département"))

names(v)
##  [1] "OBJECTID"            "Id"                  "Noms_Dép"           
##  [4] "Population"          "Superficie"          "Shape_Leng"         
##  [7] "Shape_Area"          "Ojectid"             "Centres_principaux" 
## [10] "Centres_secondaires" "geometry"
ggplot(data=v)+
  geom_sf(fill="grey97")+
  stat_sf_coordinates(aes(size = Centres_secondaires,
      fill= Centres_principaux),
      color = "black",
      shape= 21,
      show.legend = "point") +
  scale_fill_gradient2(name = "Centres_principaux",
                       low = "darkblue", mid = "white", high = "darkred")+
  labs(title = "Carte choroplèthe à fonds proportionnel",
       subtitle="Centres principaux et secondaires",
       caption = "Auteur: MALANDA MANKOUSSOU JC.\n Source: INS, 2022")+
  theme_void() +
  theme(plot.title=element_text(size=12,
                     face="bold",color="blue",hjust= 0.3),
        plot.subtitle = element_text(size=10, face="bold",
                        color="black",hjust= 0.6), 
        plot.caption=element_text(size= 8,face = "bold.italic", color = "gray10", hjust= 0.4)) +
  scale_size_area(name = "Centres_secondaires", max_size = 10) +
  annotation_scale(location = "br", line_width = .5) +
  annotation_north_arrow(location = "tl", height = unit(0.7, "cm"),width = unit(0.7, "cm")) +
geom_sf_text(aes(label =Noms_Dép),size= 3.5 ,family="serif",color = "gray9", face = "bold") 
## Warning: Ignoring unknown parameters: face
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data

## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data

ggsave(filename="Cartes ggplot avec stat_sf_coordinates.png",dpi="print")
## Saving 7 x 5 in image
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data

## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data