```{r, echo = FALSE}
knitr::opts_chunk$set(fig.width=12)
```
```{r packages, echo = FALSE, include = FALSE}
library(dplyr)
library(tidyr)
library(ggplot2)
library(here)
```

```{r import donnees, echo = FALSE}
full <- read.csv(file.path(
        here(), "data",
        "aggregated_dataframe_suppdiplome.csv"
    ))

full[["ine"]] <- as.factor(full[["ine"]])
full[["cours"]] <- as.factor(full[["cours"]])
full[["ects"]] <- as.numeric(full[["ects"]])
full[["annee"]] <- as.factor(full[["annee"]])
full[["type_annee"]] <- as.factor(full[["type_annee"]])
full[["bloc"]] <- as.factor(full[["bloc"]])
full[["parcours"]] <- as.factor(full[["parcours"]])
full[["domaine2A"]] <- as.factor(full[["domaine2A"]])
full[["dominante3A"]] <- as.factor(full[["dominante3A"]])




```

```{r differents_dataframe, echo = FALSE}
df_ue_choix <- full[grepl("UE à choix *", full[["bloc"]]), ]
df_ue_choix <- df_ue_choix[-which(df_ue_choix[["cours"]] == "UE à choix Semestre 1"), ]
df_ue_choix <- df_ue_choix[-which(df_ue_choix[["cours"]] == "UE à choix Semestre 2"), ]

df_count <- df_ue_choix %>%
    group_by(cours, bloc) %>%
    summarise(n = n()) %>%
    ungroup() %>%
    mutate(freq = n / sum(n))

par_dominante_effectif <- full %>% group_by(dominante3A) %>% 
    select(-c(annee,type_annee,bloc,cours,ects)) %>%
    distinct() %>% count(sort = TRUE)

par_domaine_effectif <- full %>% group_by(domaine2A) %>% 
    select(-c(annee,type_annee,bloc,cours,ects)) %>%
    distinct() %>% count(sort = TRUE)

par_parcours_effectif <- full %>% group_by(parcours) %>% 
    select(-c(annee,type_annee,bloc,cours,ects)) %>%
    distinct() %>% count(sort = TRUE)

```
```{r, echo = FALSE}

ggplot(df_count %>% filter(n > 20)) +
    aes(x = reorder(cours, n), y = reorder(n, n)) +
    geom_bar(stat = "identity", width = 1, aes(fill = .data$bloc)) +
    scale_x_discrete() +
    theme(axis.text.y = element_text(angle = 0, vjust = .5, hjust = 1)) +
    coord_flip()
```

```{r, echo = FALSE}

ggplot(par_domaine_effectif) +
    aes(x = reorder(domaine2A, n), y = reorder(n, n)) +
    geom_bar(stat = "identity", width = 1, aes(fill = .data$domaine2A)) +
    scale_x_discrete() +
    theme(axis.text.y = element_text(angle = 0, vjust = .5, hjust = 1)) +
    coord_flip() + 
    theme(legend.position = "none")
```
```{r, echo = FALSE}

ggplot(par_dominante_effectif) +
    aes(x = reorder(dominante3A, n), y = reorder(n, n)) +
    geom_bar(stat = "identity", width = 1, aes(fill = .data$dominante3A)) +
    scale_x_discrete() +
    theme(axis.text.y = element_text(angle = 0, vjust = .5, hjust = 1)) +
    coord_flip() + 
    theme(legend.position = "none")
```

```{r , echo = FALSE}
ggplot(par_parcours_effectif) +
    aes(x = reorder(parcours, n), y = reorder(n, n)) +
    geom_bar(stat = "identity", width = 1, aes(fill = .data$parcours)) +
    scale_x_discrete() +
    theme(axis.text.y = element_text(angle = 0, vjust = .5, hjust = 1)) +
    coord_flip() + 
    theme(legend.position = "none")
```