mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-01-26 03:54:12 +01:00
Enhance tp2 Rmd (histogram, interactive maps, choropleth fixes); add TP3 Shiny apps and project files; update .gitignore; add shap (+cloudpickle, numba, llvmlite, tqdm, slicer) to pyproject.toml and uv.lock; remove generated tp2 HTML/assets
This commit is contained in:
@@ -48,7 +48,6 @@ library(tidyr)
|
||||
library(rmarkdown)
|
||||
library(ggthemes)
|
||||
library(cowplot)
|
||||
|
||||
```
|
||||
|
||||
# Objectifs du TP
|
||||
@@ -91,7 +90,7 @@ ont lieu tous les 5 ans.
|
||||
Dans un premier temps, il faut installer le package et le charger.
|
||||
|
||||
```{r}
|
||||
# install.packages("gapminder") #nolint
|
||||
# install.packages("gapminder")
|
||||
library(gapminder)
|
||||
```
|
||||
|
||||
@@ -159,6 +158,12 @@ ggplot(data = gapminder, aes(x = lifeExp)) +
|
||||
geom_density()
|
||||
```
|
||||
|
||||
```{r}
|
||||
ggplot(data = gapminder, aes(x = lifeExp)) +
|
||||
geom_histogram(aes(y = ..density..), bins = 30) +
|
||||
geom_density()
|
||||
```
|
||||
|
||||
### Cartes
|
||||
|
||||
Afin de visualiser plus précisément les caractéristiques régionales de
|
||||
@@ -226,8 +231,9 @@ library(tidyverse)
|
||||
path <- getwd()
|
||||
accidents <- read_csv(
|
||||
paste0(
|
||||
path,
|
||||
"/data/accidentsVelo.csv"),
|
||||
path,
|
||||
"/data/accidentsVelo.csv"
|
||||
),
|
||||
col_types = cols(Num_Acc = col_double(), date = col_date(format = "%Y-%m-%d"))
|
||||
)
|
||||
|
||||
@@ -245,8 +251,13 @@ accidents <- accidents |>
|
||||
# correct some issues with variables `hrmn`
|
||||
issue <- which(str_length(accidents$hrmn) == 4)
|
||||
correct <- accidents$hrmn[issue]
|
||||
correct <- paste0("0", str_sub(correct, 1, 1), ":",
|
||||
str_sub(correct, 2, 2), str_sub(correct, 4, 4))
|
||||
correct <- paste0(
|
||||
"0",
|
||||
str_sub(correct, 1, 1),
|
||||
":",
|
||||
str_sub(correct, 2, 2),
|
||||
str_sub(correct, 4, 4)
|
||||
)
|
||||
accidents$hrmn[issue] <- correct
|
||||
|
||||
# Extract hour
|
||||
@@ -257,8 +268,9 @@ accidents <- accidents |>
|
||||
# mapping table for french departments
|
||||
departements_francais <- read_excel(
|
||||
paste0(
|
||||
path,
|
||||
"/data/departements-francais.xlsx"),
|
||||
path,
|
||||
"/data/departements-francais.xlsx"
|
||||
),
|
||||
col_types = c("text", "text", "text")
|
||||
)
|
||||
```
|
||||
@@ -321,9 +333,57 @@ intéressantes sont à extraire de ce jeu de données.
|
||||
(latitude, longitude) en France sur toute la période en modulant la
|
||||
gravité.
|
||||
|
||||
```{r}
|
||||
library(mapview)
|
||||
library(sf)
|
||||
|
||||
## Remove NA
|
||||
df_map_dyn <- accidents |>
|
||||
filter(!is.na(lat) & !is.na(long)) |>
|
||||
mutate(
|
||||
lat = as.numeric(str_replace_all(str_trim(lat), ",", ".")),
|
||||
long = as.numeric(str_replace_all(str_trim(long), ",", "."))
|
||||
) |>
|
||||
filter(!is.na(lat) & !is.na(long))
|
||||
|
||||
# Make map and print it
|
||||
mymap <- st_as_sf(df_map_dyn[1:5000, ], coords = c("long", "lat"), crs = 4326)
|
||||
mapview(
|
||||
mymap,
|
||||
cex = 2,
|
||||
legend = TRUE,
|
||||
layer.name = "Gravité",
|
||||
zcol = "grav",
|
||||
map.types = "OpenStreetMap"
|
||||
)
|
||||
```
|
||||
|
||||
2. Faire deux variantes de cette carte, selon le caractère urbain ou
|
||||
non des accidents.
|
||||
|
||||
```{r}
|
||||
# Map for urban accidents
|
||||
df_map_urban <- df_map_dyn |>
|
||||
mutate(
|
||||
agg = dplyr::if_else(
|
||||
agg == 1,
|
||||
"urbanisation",
|
||||
"agglomération",
|
||||
missing = NA_character_
|
||||
)
|
||||
)
|
||||
|
||||
# Make map and print it
|
||||
mymap_urban <- st_as_sf(df_map_urban, coords = c("long", "lat"), crs = 4326)
|
||||
mapview(
|
||||
mymap_urban,
|
||||
cex = 2,
|
||||
legend = TRUE,
|
||||
layer.name = "Urbanisation",
|
||||
zcol = "agg"
|
||||
)
|
||||
```
|
||||
|
||||
3. Commenter ces figures.
|
||||
|
||||
4. Quelles limites voyez-vous à cette représentation ?
|
||||
@@ -333,21 +393,6 @@ Voici un premier code à trou pour vous aider. Pour alléger les temps de
|
||||
production afficher uniquement quelques points. Vous pourrez ajouter
|
||||
l'ensemble du jeu de données quand votre code sera finalisé.
|
||||
|
||||
```{r}
|
||||
library(mapview)
|
||||
library(sf)
|
||||
|
||||
## Remove NA
|
||||
df_map_dyn <- accidents |>
|
||||
filter(!is.na(lat) & !is.na(long)) |>
|
||||
na.omit()
|
||||
|
||||
# Make map and print it
|
||||
mymap <- st_as_sf(df_map_dyn, coords=c("long", "lat"), crs=4326)
|
||||
mapview(mymap, cex = 2, layer.name = "Gravité",
|
||||
zcol = "grav",legend = TRUE )
|
||||
```
|
||||
|
||||
#### Carte choroplèthe {.unnumbered}
|
||||
|
||||
::: exercise-box
|
||||
@@ -360,9 +405,9 @@ mapview(mymap, cex = 2, layer.name = "Gravité",
|
||||
|
||||
Voici un premier code à trou pour vous aider.
|
||||
|
||||
```{r, eval = F}
|
||||
# get french map - level nuts2
|
||||
fr <- gisco_get_nuts(resolution = "20", country = ???, nuts_level = ???) |>
|
||||
```{r}
|
||||
# get french map - level nuts3
|
||||
fr <- gisco_get_nuts(resolution = "20", country = "FRA", nuts_level = 3) |>
|
||||
mutate(res = "20M")
|
||||
|
||||
# Remove white-space to avoid errors.
|
||||
@@ -375,23 +420,28 @@ fr <- fr |>
|
||||
|
||||
# Merge and remove departements outside metropolitan France
|
||||
fr_map <- fr |>
|
||||
left_join(???) |>
|
||||
filter(! dep %in% c("971", ???) )
|
||||
left_join(departements_francais, by = c("NUTS_NAME" = "dep_name")) |>
|
||||
filter(!dep %in% c("971", "972", "973", "974", "976"))
|
||||
|
||||
# count the number of accidents
|
||||
df_acc <- ???
|
||||
df_acc <- accidents |>
|
||||
filter(!is.na(dep)) |>
|
||||
group_by(dep) |>
|
||||
summarise(n = n())
|
||||
|
||||
# merge statistics with the map
|
||||
map_acc <- fr_map |>
|
||||
left_join(df_acc, by = c("dep" = "dep"))
|
||||
left_join(df_acc, by = c("dep" = "dep"))
|
||||
|
||||
# map with all accidents
|
||||
g_map_acc <- ggplot(map_acc) +
|
||||
geom_sf(???) +
|
||||
scale_fill_viridis_c(option = "viridis") +
|
||||
labs(title = "Carte des accidents de vélo",
|
||||
subtitle = "Année 2005-2021",
|
||||
fill = "Nombre d'accidents") +
|
||||
geom_sf(aes(fill = n)) +
|
||||
scale_fill_viridis_c(option = "viridis") +
|
||||
labs(
|
||||
title = "Carte des accidents de vélo",
|
||||
subtitle = "Année 2005-2021",
|
||||
fill = "Nombre d'accidents"
|
||||
) +
|
||||
theme_void()
|
||||
g_map_acc
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user