|
| 1 | +--- |
| 2 | +title: "Réalisation de cartes" |
| 3 | +author: "Guyliann Engels & Philippe Grosjean" |
| 4 | +description: "**SDD III : Module 6** Réalisation de cartes avec R" |
| 5 | +tutorial: |
| 6 | + id: "C06La_map" |
| 7 | + version: 0.0.1/3 |
| 8 | +output: |
| 9 | + learnr::tutorial: |
| 10 | + progressive: true |
| 11 | + allow_skip: true |
| 12 | +runtime: shiny_prerendered |
| 13 | +--- |
| 14 | + |
| 15 | +```{r setup, include=FALSE} |
| 16 | +BioDataScience3::learnr_setup() |
| 17 | +SciViews::R() |
| 18 | +library(sf) |
| 19 | +library(tmap) |
| 20 | +fr_sf <- sf::st_as_sf(maps::map("france", plot = FALSE, fill = TRUE)) |
| 21 | +
|
| 22 | +# function |
| 23 | +chart.sf <- function(data, specif = aes(), formula = NULL, mapping = aes(), type = NULL, theme = theme_svmap, env = parent.frame()) { |
| 24 | + if (!missing(specif)) |
| 25 | + if (inherits(specif, "formula")) { |
| 26 | + formula <- specif |
| 27 | + } else if (inherits(specif, "uneval")) { |
| 28 | + mapping <- specif |
| 29 | + } else rlang::abort("'specif' must be either a formula or aes()/f_aes()") |
| 30 | + if (!is.null(formula)) { |
| 31 | + args <- chart:::as_list(match.call())[-1] |
| 32 | + args$data <- NULL |
| 33 | + args$specif <- NULL |
| 34 | + args$formula <- NULL |
| 35 | + args$mapping <- NULL |
| 36 | + args$type <- NULL |
| 37 | + args$auto.labs <- NULL |
| 38 | + args$env <- NULL |
| 39 | + mapping <- chart:::.rename_aes(chart:::.f_to_aes(formula, args, with.facets = TRUE)) |
| 40 | + # Special case ~0 |
| 41 | + if (is.numeric(mapping$x) && mapping$x == 0) |
| 42 | + mapping$x <- NULL |
| 43 | + } |
| 44 | + facets <- mapping$facets |
| 45 | + mapping$facets <- NULL |
| 46 | + p <- ggplot(data = data, mapping = mapping, environment = env) + |
| 47 | + theme() |
| 48 | + if (!is_null(facets)) { |
| 49 | + if (is_null(rlang::f_lhs(facets))) { |
| 50 | + p <- p + facet_wrap(facets) |
| 51 | + } else { |
| 52 | + p <- p + facet_grid(facets) |
| 53 | + } |
| 54 | + } |
| 55 | + if (inherits(p, "ggplot")) |
| 56 | + class(p) <- unique(c("Chart", class(p))) |
| 57 | + p |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +```{r, echo=FALSE} |
| 62 | +BioDataScience3::learnr_banner() |
| 63 | +``` |
| 64 | + |
| 65 | +```{r, context="server"} |
| 66 | +BioDataScience3::learnr_server(input, output, session) |
| 67 | +``` |
| 68 | + |
| 69 | +---- |
| 70 | + |
| 71 | +## Objectifs |
| 72 | + |
| 73 | +- appréhender les objets sf |
| 74 | + |
| 75 | +- Réaliser des cartes avec R |
| 76 | + |
| 77 | +## Avant de débuter |
| 78 | + |
| 79 | +Les packages suivants sont chargé par défaut pour chaque exercice |
| 80 | + |
| 81 | +```{r, eval=FALSE, echo=TRUE} |
| 82 | +SciViews::R() |
| 83 | +library(sf) |
| 84 | +library(tmap) |
| 85 | +``` |
| 86 | + |
| 87 | +Ce code est exécuté par défaut avant de débuter votre séance d'exercices. Il permet de réaliser des cartes avec la fonctio chart(). |
| 88 | + |
| 89 | +```{r, eval=FALSE, echo=TRUE} |
| 90 | +SciViews::R |
| 91 | +chart.sf <- function(data, specif = aes(), formula = NULL, mapping = aes(), type = NULL, theme = theme_svmap, env = parent.frame()) { |
| 92 | + if (!missing(specif)) |
| 93 | + if (inherits(specif, "formula")) { |
| 94 | + formula <- specif |
| 95 | + } else if (inherits(specif, "uneval")) { |
| 96 | + mapping <- specif |
| 97 | + } else rlang::abort("'specif' must be either a formula or aes()/f_aes()") |
| 98 | + if (!is.null(formula)) { |
| 99 | + args <- chart:::as_list(match.call())[-1] |
| 100 | + args$data <- NULL |
| 101 | + args$specif <- NULL |
| 102 | + args$formula <- NULL |
| 103 | + args$mapping <- NULL |
| 104 | + args$type <- NULL |
| 105 | + args$auto.labs <- NULL |
| 106 | + args$env <- NULL |
| 107 | + mapping <- chart:::.rename_aes(chart:::.f_to_aes(formula, args, with.facets = TRUE)) |
| 108 | + # Special case ~0 |
| 109 | + if (is.numeric(mapping$x) && mapping$x == 0) |
| 110 | + mapping$x <- NULL |
| 111 | + } |
| 112 | + facets <- mapping$facets |
| 113 | + mapping$facets <- NULL |
| 114 | + p <- ggplot(data = data, mapping = mapping, environment = env) + |
| 115 | + theme() |
| 116 | + if (!is_null(facets)) { |
| 117 | + if (is_null(rlang::f_lhs(facets))) { |
| 118 | + p <- p + facet_wrap(facets) |
| 119 | + } else { |
| 120 | + p <- p + facet_grid(facets) |
| 121 | + } |
| 122 | + } |
| 123 | + if (inherits(p, "ggplot")) |
| 124 | + class(p) <- unique(c("Chart", class(p))) |
| 125 | + p |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +## Réalisation de cartes basiques |
| 130 | + |
| 131 | +```{r} |
| 132 | +read("NLD_prov", package = "tmap") %>.% |
| 133 | + select(., name, population, geometry) %>.% |
| 134 | + st_as_sf(.) -> nld |
| 135 | +
|
| 136 | +nld |
| 137 | +``` |
| 138 | + |
| 139 | + |
| 140 | +Vous avez à disposition l'objet nld qui représente les provinces au Pays-bas. |
| 141 | + |
| 142 | +### chart |
| 143 | + |
| 144 | +Réalisez une carte des Pays-Bas avec les limites de chaque province. |
| 145 | + |
| 146 | +```{r} |
| 147 | +chart(data = nld) + |
| 148 | + geom_sf() |
| 149 | +``` |
| 150 | + |
| 151 | +- Utilisez la fonction chart() afin de reproduire le graphique ci-dessus |
| 152 | + |
| 153 | +```{r chartmap1, exercise = TRUE, exercise.lines = 2} |
| 154 | +___(___ = ___) + |
| 155 | + ___() |
| 156 | +``` |
| 157 | + |
| 158 | +```{r chartmap1-solution} |
| 159 | +chart(data = nld) + |
| 160 | + geom_sf() |
| 161 | +``` |
| 162 | + |
| 163 | +```{r chartmap1-check} |
| 164 | +grade_code("C'est parfait tu as su réaliser ta première carte avec avec chart()") |
| 165 | +``` |
| 166 | + |
| 167 | +```{r} |
| 168 | +chart(data = nld, ~ 0 %fill=% name) + |
| 169 | + geom_sf() |
| 170 | +``` |
| 171 | + |
| 172 | +- Reproduisez avec chart() le graphique ci-dessus. Utilisez l'interface formule. |
| 173 | + |
| 174 | +```{r chartmap2, exercise = TRUE, exercise.lines = 2} |
| 175 | +___(___ = ___, ~ ___ %___% ___) + |
| 176 | + ___() |
| 177 | +``` |
| 178 | + |
| 179 | +```{r chartmap2-solution} |
| 180 | +chart(data = nld, ~ 0 %fill=% name) + |
| 181 | + geom_sf() |
| 182 | +``` |
| 183 | + |
| 184 | +```{r chartmap2-check} |
| 185 | +grade_code("C'est parfait tu as su réaliser ton second graphique première carte avec avec chart()") |
| 186 | +``` |
| 187 | + |
| 188 | +```{r} |
| 189 | +chart(data = nld) + |
| 190 | + geom_sf(aes(fill = population)) |
| 191 | +``` |
| 192 | + |
| 193 | +- Reproduisez avec chart() le graphique ci-dessus. N'utilisez l'interface formule. |
| 194 | + |
| 195 | +```{r chartmap3, exercise = TRUE, exercise.lines = 2} |
| 196 | +___(___ = ___) + |
| 197 | + ___(___(___ = ___)) |
| 198 | +``` |
| 199 | + |
| 200 | +```{r chartmap3-solution} |
| 201 | +chart(data = nld) + |
| 202 | + geom_sf(aes(fill = population)) |
| 203 | +``` |
| 204 | + |
| 205 | +```{r chartmap3-check} |
| 206 | +grade_code("C'est parfait tu as su réaliser ton troisième graphique avec chart()") |
| 207 | +``` |
| 208 | + |
| 209 | + |
| 210 | +### R de base |
| 211 | + |
| 212 | +Vous avez pu vous rendre compte que chart/ggplot prend pas mal de temps pour r"aliser.La réalisation d'une carte pren |
| 213 | +Réalisez une carte des Pays-Bas avec les limites de chaque province. |
| 214 | + |
| 215 | +```{r} |
| 216 | +plot(st_geometry(nld)) |
| 217 | +``` |
| 218 | + |
| 219 | +- Utilisez le R de base afin de reproduire le graphique ci-dessus |
| 220 | + |
| 221 | +```{r basemap1, exercise = TRUE, exercise.lines = 1} |
| 222 | +___(___(___)) |
| 223 | +``` |
| 224 | + |
| 225 | +```{r basemap1-solution} |
| 226 | +plot(st_geometry(nld)) |
| 227 | +``` |
| 228 | + |
| 229 | +```{r basemap1-check} |
| 230 | +grade_code("C'est parfait tu as su réaliser ta première carte avec avec le R de base") |
| 231 | +``` |
| 232 | + |
| 233 | +Le code R de base est bien plus rapide que les cartes réalisées avec ggplot. Cette remarque est valable pour tous les types de graphiques. |
| 234 | + |
| 235 | +# tmap |
| 236 | + |
| 237 | +Le package tmap est un package très utile pour réaliser des cartes. Il est rapide comme le R de base et il suit la logique d'addition des couches de ggplot. |
| 238 | + |
| 239 | +```{r} |
| 240 | +qtm(nld) |
| 241 | +``` |
| 242 | + |
| 243 | +Reproduisez le graphique ci-dessus en une seule instruction avec le package tmap. |
| 244 | + |
| 245 | +```{r tmap1, exercise = TRUE, exercise.lines = 1} |
| 246 | +___(___) |
| 247 | +``` |
| 248 | + |
| 249 | +```{r tmap1-solution} |
| 250 | +qtm(nld) |
| 251 | +``` |
| 252 | + |
| 253 | +```{r tmap1-check} |
| 254 | +grade_code("C'est parfait tu as su réaliser ta première carte avec avec le tmap") |
| 255 | +``` |
| 256 | + |
| 257 | + |
| 258 | +```{r} |
| 259 | +tm_shape(nld) + |
| 260 | + tm_fill("name", title = "Province") |
| 261 | +``` |
| 262 | + |
| 263 | +Reproduisez le graphique ci-dessus en une seule instruction avec le package tmap. |
| 264 | + |
| 265 | +```{r tmap2, exercise = TRUE, exercise.lines = 2} |
| 266 | +tm_shape(___) + |
| 267 | + tm_fill(___, title = ___) |
| 268 | +``` |
| 269 | + |
| 270 | +```{r tmap2-solution} |
| 271 | +tm_shape(nld) + |
| 272 | + tm_fill("name", title = "Province") |
| 273 | +``` |
| 274 | + |
| 275 | +```{r tmap2-check} |
| 276 | +grade_code("C'est parfait tu as su réaliser ta seconde carte avec avec tmap") |
| 277 | +``` |
| 278 | + |
| 279 | +```{r} |
| 280 | +tm_shape(nld) + |
| 281 | + tm_fill("name", title = "Province") + |
| 282 | + tm_compass() + |
| 283 | + tm_scale_bar(position = c("left", "bottom")) |
| 284 | +``` |
| 285 | + |
| 286 | +A présent, tentez de reproduire une carte un peu plus complexes en 4 fonctions avec le package tmap. Utilisez judicieusement les instructions employées pour l'exercice précédent. |
| 287 | + |
| 288 | +1. je prépare la carte |
| 289 | +2. j'affiche les provinces sur ma carte via les couleurs et je change le titre de ma légende |
| 290 | +3. j'ajoute le nord |
| 291 | +4. j'ajoute l'échelle en bas à gauche |
| 292 | + |
| 293 | +```{r tmap3, exercise = TRUE, exercise.lines = 4} |
| 294 | +___(___) + |
| 295 | + ___(___, title = ___) + |
| 296 | + ___(___) + |
| 297 | + ___(position = c(___, ___)) |
| 298 | +``` |
| 299 | + |
| 300 | + |
| 301 | +```{r tmap3-solution} |
| 302 | +tm_shape(nld) + |
| 303 | + tm_fill("name", title = "Province") + |
| 304 | + tm_compass() + |
| 305 | + tm_scale_bar(position = c("left", "bottom")) |
| 306 | +``` |
| 307 | + |
| 308 | +```{r tmap3-check} |
| 309 | +grade_code("C'est parfait !!!") |
| 310 | +``` |
| 311 | + |
| 312 | + |
| 313 | +## Conclusion |
| 314 | + |
| 315 | +```{r comm_noscore, echo=FALSE} |
| 316 | +question_text( |
| 317 | + "Laissez-nous vos impressions sur cet outil pédagogique", |
| 318 | + answer("", TRUE, message = "Pas de commentaires... C'est bien aussi."), |
| 319 | + incorrect = "Vos commentaires sont enregistrés.", |
| 320 | + placeholder = "Entrez vos commentaires ici...", |
| 321 | + allow_retry = TRUE |
| 322 | +) |
| 323 | +``` |
0 commit comments