#-- carga de librerias

library(tidyverse) # incluye ggplot2
library(readxl) # funciones para importar xlsx
library(janitor) # funciones de limpieza
library(patchwork) #combinar gráficos de ggplot
library(ggExtra)
library(ggthemes) # nuevas temas para los plots
library(plotly) #gráficos interactivos # remotes::install_github("plotly/plotly")
library(tibble)
library(skimr) # reseumen numerico
library(modeest)
library(ggrepel) # añadir etiquetas a los gráficos
library(RColorBrewer) #paletas de colores
#library(plotly) #graficos interactivos
#library(corrplot)
#library(ggridges)


#--  cargar datos
datos_pinguinos <- read.csv("data/data_penguins.csv")
head(datos_pinguinos)



# PIPE |>      %>%  
# (CTRL + SHIFT + M) 
summary(datos_pinguinos)

#Ejemplo 3
head(datos_pinguinos[datos_pinguinos$ISLAND.=="Torgersen",c(4,5)],5)

datos_pinguinos |> 
  filter(ISLAND.=="Torgersen") |> 
  select(bill.lenght,bill.depth) |> 
  head(5)


datos_pinguinos |> 
  head()

datos_pinguinos |> 
  summary()

# tabla de continegencia
table(datos_pinguinos$SPECIES)


# Uso de ggplot
ggplot()

# ggplot - data
ggplot(data = datos_pinguinos) 


# ggplot - data - mmaping
ggplot(data = datos_pinguinos,
       mapping = aes(x=SPECIES))


# ggplot - geometria bar
ggplot(data = datos_pinguinos,
       mapping = aes(x=SPECIES)) +
  geom_bar()


ggplot(data = datos_pinguinos,
       mapping = aes(x=ISLAND.)) +
  geom_bar()

# resumen datos
datos_pinguinos |> 
  summary()

# Errores en los datos
# Genero debe ser categorico genero
# la variable X - no arroja de informración
#  nombre de las variables no están normalizadas

# Limpieza datos

datos_pinguinos <- janitor::clean_names(datos_pinguinos) 

#--  cargar datos
datos_pinguinos <- read.csv("data/data_penguins_clean.csv")
datos_pinguinos |> 
  summary()


datos_pinguinos$species <- as.factor(datos_pinguinos$species)
datos_pinguinos$island <- as.factor(datos_pinguinos$island)
datos_pinguinos$sex <- as.factor(datos_pinguinos$sex)

datos_pinguinos |> 
  summary()

ggplot(data = datos_pinguinos,
       mapping = aes(x=island)) +
  geom_bar()

# ELIMINAR COLUMNA 1 "x"
datos_pinguinos <- datos_pinguinos[,-1]


# Tipo de análisis exploratorio: Univariado
# Una sola variable 
# Tipo de variable : cuantitativa
# Variables cuantitativas : bill_lenght, bill_depth, flipper_lenght, body_mass_g

datos_pinguinos |> 
  glimpse()

# ejemplo  normal
ggplot(data = datos_pinguinos,
       mapping = aes(x=island)) +
  geom_bar()

# ejemplo pipe
datos_pinguinos |> 
  ggplot(mapping = aes(x=island)) +
  geom_bar()

# ---------------------------------------------
# Graficos de Histogramas

datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram()


# Valor intervalos
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 15)


# color intervalos
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff")


# relleno intervalos
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff",
                 fill  = "salmon")


# datos en histograma
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff",
                 fill  = "salmon") +
  stat_bin(aes(label =..count..), 
           geom="text")


# datos en histograma , cambio de bins
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 15, 
                 color = "#ffffff",
                 fill  = "salmon") +
  stat_bin(aes(label =..count..), 
           geom="text",
           bins = 15,
           vjust=1.5, 
           hjust=0.5)


# datos categoricos: especies, island, sex
# datos_pinguinos |> 
#   glimpse()

table(datos_pinguinos$sex)

# Histograma con dos variables 
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff",
                 aes(fill = sex))



# Histograma con dos variables - transparencia
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff",
                 aes(fill = sex),
                 alpha= 0.5,
                 position = "identity")


summary(datos_pinguinos$flipper_lenght)
min <- 172
max<- 231

max -min

# categoria
# female = color1
# male  = color2


# facetas en columnas
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff",
                 aes(fill = sex)) +
  facet_grid(.~sex) 


# facetas en columnas
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff",
                 aes(fill = sex)) +
  facet_grid(sex~.) 


# Info en el gráfico
datos_pinguinos |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff",
                 aes(fill = sex)) +
  facet_grid(sex~.) +
  labs(title = "Distribución de las longuitud de la aletas de los pinguinos por sexo",
       subtitle = "Datos: Estación Palmer,Antártida",
       x = "Longuitud aleta",
       y = "Cantidad") +
  theme(legend.position = "none")



# Práctica 
table(datos_pinguinos$sex)
table(datos_pinguinos$species)
table(datos_pinguinos$island)

# Filtro de datos de la variable island=="Torgersen"
# categorizada por sexo
datos_pinguinos |> 
  filter(island=="Torgersen") |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff",
                 aes(fill = sex)) +
  facet_grid(sex~.) +
  labs(title = "Distribución de las longuitud de la aletas de los pinguinos por sexo",
       subtitle = "Datos: Estación Palmer,Antártida",
       x = "Longuitud aleta",
       y = "Cantidad") +
  theme(legend.position = "none")


# Filtro de datos de la variable island=="Torgersen"
# categorizada por species
datos_pinguinos |> 
  filter(island=="Torgersen") |> 
  ggplot(aes(x=flipper_lenght)) +
  geom_histogram(bins = 30, 
                 color = "#ffffff",
                 aes(fill = species)) +
  facet_grid(species~.) +
  labs(title = "Distribución de las longuitud de la aletas de los pinguinos por sexo",
       subtitle = "Datos: Estación Palmer,Antártida",
       x = "Longuitud aleta",
       y = "Cantidad") +
  theme(legend.position = "none")
















