library(pacman)
p_load(dplyr, ggplot2, scales, ggthemes, ggpubr)

#Funções para a formatação numérica das escalas dos gráficos
fun_pct <- function(x) sprintf("%0.1f", round((x*100), digits = 1))
fun_pct_2 <- function(x) paste0(sprintf("%0.1f", round((x*100), digits = 1)), "%")

#função para o dplyr::select ser a função select padrão:
select <- dplyr::select
EAD <- read.csv("C:/Users/luizz/Downloads/Inep atual/Novo Artigo EaD/Final/EAD_PART_ETARIA.csv", sep = ";")

EAD <- EAD %>% 
   mutate(idade = case_when(
      Idade == "Ate 24 anos" ~ "Até 24", 
      Idade == "25 a 34 anos" ~ "25-34", 
      Idade == "35 a 44 anos" ~ "35-44", 
      Idade == "45  a 54 anos" ~ "45-54", 
      Idade == "55 anos ou mais" ~ "55+", T ~"")) %>%
   mutate(idade = factor(idade)) %>%
   mutate(idade = factor(idade, 
                         levels = c("Até 24",
                                    "25-34",
                                    "35-44",
                                    "45-54", 
                                    "55+")))

EAD %>% ggplot() +
   geom_line(aes(x=Ano, y=EaD, color = EaD), size = 1.5) +
   theme_wsj() +
   facet_grid(vars(idade)) +
   scale_y_continuous(
      limits = c(0, 0.6),
      breaks = c(0.2, 0.4, 0.6)) 

# 
# EAD_wide <- EAD %>%
#    select(-Idade) %>%
#    pivot_wider(
#     names_from = idade, 
#     values_from = c(EaD))

EAD %>% ggplot() +
   geom_line(aes(x=Ano, y=EaD, group = idade, color = idade), size = 1.5) +
   theme_wsj() +
   theme(
   legend.justification = 0.4,  # Centraliza a legenda
    legend.position = "top", 
    legend.title = element_text(size=14), 
    legend.text = element_text(size=14),    
   ) +
   scale_y_continuous(
      name = "percentual de matriculados em cursos EaD", 
      labels = fun_pct_2,
      limits = c(0, 0.6),
      breaks = c(0, 0.2, 0.4, 0.6)) +
   scale_x_continuous(
      limits = c(2009.8, 2018.2),
      breaks = c(seq(2010, 2018, 2))
   ) +
   geom_text(data = EAD %>% filter(idade %in% c("Até 24", "25-34", "45-54") & Ano %in% c(2010, 2018)),
             aes(x=Ano, y=EaD, label = fun_pct(EaD), fontface = "bold", color = idade), 
             size = 4.5,
             vjust=-1) +
   geom_text(data = EAD %>% filter(idade %in% c("55+") & Ano %in% c(2010)),
             aes(x=Ano, y=EaD, label = fun_pct(EaD), fontface = "bold", color = idade), 
             size = 4.5,
             hjust=1.1) +
   geom_text(data = EAD %>% filter(idade %in% c("55+") & Ano %in% c(2018)),
             aes(x=Ano, y=EaD, label = fun_pct(EaD), fontface = "bold", color = idade), 
             size = 4.5,
             vjust=1.7) +
   geom_text(data = EAD %>% filter(idade %in% c("35-44") & Ano %in% c(2010)),
             aes(x=Ano, y=EaD, label = fun_pct(EaD), fontface = "bold", color = idade), 
             size = 4.5,
             vjust=1.7) +
   geom_text(data = EAD %>% filter(idade %in% c("35-44") & Ano %in% c(2018)),
             aes(x=Ano, y=EaD, label = fun_pct(EaD), fontface = "bold", color = idade), 
             size = 4.5,
             hjust=-0.1) +
   scale_fill_manual(values = c("#C72E29", "#016392", "#BE9C2E", "#098154", "#5C5756"),  aesthetics = c("colour", "fill"))