Refactor code for improved readability and consistency across R Markdown files

- Updated comments and code formatting in `3-td_ggplot2 - enonce.Rmd` for clarity.
- Enhanced code structure in `4-td_graphiques - enonce.Rmd` by organizing options and library calls.
- Replaced pipe operator `%>%` with `|>` in `Code_Lec3.Rmd` for consistency with modern R syntax.
- Cleaned up commented-out code and ensured consistent spacing in ggplot calls.
This commit is contained in:
2025-11-06 09:26:58 +01:00
parent 8f5f2b417c
commit 03bf0a4db2
10 changed files with 764 additions and 588 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -297,7 +297,7 @@ On présente ci-dessous un aperçu des données.
fold <- getwd()
# Load data
# load(paste0(fold, "/M2/Data Visualisation/tp1", "/data/datafreMPTL.RData")) # VSCode # nolint
# load(paste0(fold, "/M2/Data Visualisation/tp1", "/data/datafreMPTL.RData")) # VSCode
load(paste0(fold, "/data/datafreMPTL.RData")) # RStudio
paged_table(dat, options = list(rows.print = 15))
```
@@ -505,7 +505,7 @@ df_plot <- dat |>
p3 <- ggplot(df_plot, aes(x = DrivAge, y = freq)) +
geom_point() +
geom_smooth() +
labs(x = "Age du conducteur", y = "Frequence") +
labs(x = "Age du conducteur", y = "Frequence") +
theme_bw()
p3
```
@@ -642,12 +642,16 @@ plot_pairwise_disc <- function(df, var1, var2) {
df |>
group_by(varx, vary) |>
summarize(exp = sum(Exposure),
nb_claims = sum(ClaimNb),
freq = sum(ClaimNb) / sum(Exposure), .groups = "drop") |>
summarize(
exp = sum(Exposure),
nb_claims = sum(ClaimNb),
freq = sum(ClaimNb) / sum(Exposure), .groups = "drop"
) |>
ggplot(aes(x = varx, y = freq, colour = vary, group = vary), alpha = 0.3) +
geom_point() + geom_line() + theme_bw() +
labs(x = var1, y = "Frequence", colour = var2)
geom_point() +
geom_line() +
theme_bw() +
labs(x = var1, y = "Frequence", colour = var2)
}
```

View File

@@ -23,8 +23,13 @@ editor_options:
```{r setup, include=FALSE}
## Global options
knitr::opts_chunk$set(cache = FALSE, warning = FALSE, message = FALSE, fig.retina = 2)
options(encoding = 'UTF-8')
knitr::opts_chunk$set(
cache = FALSE,
warning = FALSE,
message = FALSE,
fig.retina = 2
)
options(encoding = "UTF-8")
```
@@ -33,11 +38,11 @@ options(encoding = 'UTF-8')
library(lattice)
library(grid)
library(ggplot2)
require(gridExtra)
require(gridExtra)
library(locfit)
library(scales)
library(formattable)
library(RColorBrewer)
library(RColorBrewer)
library(plotly)
library(dplyr)
library(tidyr)
@@ -88,7 +93,7 @@ de vie par pays sur la période 1952-1990. Les observations ont lieu tous les 5
Dans un premier temps, il faut installer le package et le charger.
```{r}
# install.packages("gapminder")
# install.packages("gapminder") #nolint
library(gapminder)
```
@@ -140,7 +145,7 @@ pouvez observer entre `gdpPercap` et `lifeExp`.
:::
```{r}
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point()
```
@@ -158,7 +163,7 @@ visualisations permettant de comparer des distributions.
```{r}
ggplot(data = gapminder, aes(x = lifeExp)) +
geom_density()
geom_density()
```
@@ -171,16 +176,16 @@ Il faut au préalable récupérer un fond de carte (ici de l'année 2016). Nous
les données `gapminder` de 2007.
```{r}
library(giscoR)
library(giscoR)
library(sf)
world <- gisco_countries
world <- subset(world, NAME_ENGL != "Antarctica") # Remove Antartica
# Merge data
world_df <- gapminder %>%
world_df <- gapminder |>
filter(year == "2007")
world_df <- world %>%
world_df <- world |>
left_join(world_df, by = c("NAME_ENGL" = "country"))
ggplot(world_df) +
@@ -231,7 +236,7 @@ accidents <- read_csv("data/accidentsVelo.csv",
date = col_date(format = "%Y-%m-%d")))
# few ajustements
accidents <- accidents %>%
accidents <- accidents |>
mutate(mois = factor(mois),
jour = factor(jour),
dep = factor(dep),
@@ -247,8 +252,8 @@ correct <- paste0("0", str_sub(correct, 1, 1), ":",
accidents$hrmn[issue] <- correct
# Extract hour
accidents <- accidents %>%
mutate(hour = paste(date, hrmn, sep = " ")) %>%
accidents <- accidents |>
mutate(hour = paste(date, hrmn, sep = " ")) |>
mutate(hour = strptime(hour, "%Y-%m-%d %H:%M")$hour)
# mapping table for french departments
@@ -327,8 +332,8 @@ library(mapview)
library(sf)
## Remove NA
df_map_dyn <- accidents %>%
filter(???) %>%
df_map_dyn <- accidents |>
filter(???) |>
na.omit()
# Make map and print it
@@ -354,27 +359,27 @@ 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 = ???) %>%
fr <- gisco_get_nuts(resolution = "20", country = ???, nuts_level = ???) |>
mutate(res = "20M")
# Remove white-space to avoid errors.
library(stringr)
departements_francais <- departements_francais %>%
departements_francais <- departements_francais |>
mutate(dep_name = str_trim(dep_name))
fr <- fr %>%
fr <- fr |>
mutate(NUTS_NAME = str_trim(NUTS_NAME))
# Merge and remove departements outside metropolitan France
fr_map <- fr %>%
left_join(???) %>%
fr_map <- fr |>
left_join(???) |>
filter(! dep %in% c("971", ???) )
# count the number of accidents
df_acc <- ???
# merge statistics with the map
map_acc <- fr_map %>%
map_acc <- fr_map |>
left_join(df_acc, by = c("dep" = "dep"))
# map with all accidents