#Paquetes

library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(viridisLite)

#DataSet

anime <- read.csv("anime.csv")
summary_rating <- anime %>%
  summarise(
    Q1 = quantile(na.omit(rating), 0.25),
    Q3 = quantile(na.omit(rating), 0.75),
    IQR = Q3 - Q1
  )


debajo <- summary_rating$Q1 - 1.5 * summary_rating$IQR
encima <- summary_rating$Q3 + 1.5 * summary_rating$IQR

anime_SA <- anime %>%
  filter(rating >= debajo & rating <= encima)

#Graficos

##Diagrama de barras

tabla_tipo <- table(anime_SA$type)
tabla_tipo <- tabla_tipo[names(tabla_tipo) != ""]
tabla_tipo
## 
##   Movie   Music     ONA     OVA Special      TV 
##    2219     464     595    3262    1652    3643
barplot(
  tabla_tipo, 
  main = "Diagrama de tipo de anime", 
  ylab = "Frecuencia", 
  xlab = "Tipo",
  ylim = c(0,4000),
  col = viridis(6)
  )

##Diagrama de torta

porcentaje_tipo <- round(tabla_tipo*100/sum(tabla_tipo))

pie(
  porcentaje_tipo,
  labels = paste(porcentaje_tipo, '%'),
  main = 'Tipos de pelicula',
  col = viridis(6)
)
legend(
  'bottomright', 
  legend = names(porcentaje_tipo), 
  fill = viridis(6)
)

##Diagrama histograma

summary(anime_SA$rating)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   3.930   5.930   6.580   6.526   7.190   9.110
anime_ratings <- na.omit(anime_SA$rating)

hist(
  anime_ratings,
  main = "Histograma de rating",
  xlab = "Rating",
  prob = TRUE,
  ylim = c(0,0.7)
)

lines(density(anime_ratings), col = "black", lwd = 2)

mean_rating <- mean(anime_ratings)
median_rating <- median(anime_ratings)

abline(v = mean_rating, col = "red", lwd = 2, lty = 2)
abline(v = median_rating, col = "blue", lwd = 2, lty = 2)

text(mean_rating, 0.65, paste("Media =", round(mean_rating, 2)), col = "red", pos = 4)
text(median_rating, 0.60, paste("Mediana =", round(median_rating, 2)), col = "blue", pos = 4)

summary(anime_ratings)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   3.930   5.930   6.580   6.526   7.190   9.110

##BoxPlot

boxplot(
  anime_SA$rating,  
  col = "#00f7ff",
  ylab = "Rating",
  outline = FALSE,
  main = "Boxplot",
  ylim = c(3,10)
)

summary_rating <- summary(anime_SA$rating)

summary_rating
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   3.930   5.930   6.580   6.526   7.190   9.110
abline(h = summary_rating[1], col = "red", lty = 2)
abline(h = summary_rating[6], col = "red", lty = 2)

text(1, 3.5, paste("Minimo:", round(summary_rating[1], 2)), col = "red", pos = 4)
text(1, 9.5, paste("Maximo:", round(summary_rating[6], 2)), col = "red", pos = 4)

points(1, summary_rating[4], pch = 19, col = "blue")
text(1.2, summary_rating[4], paste("Media:", round(summary_rating[4], 2)), col = "blue", pos = 4)

points(1, summary_rating[3], pch = 19, col = "orange")
text(0.57, summary_rating[3], paste("Mediana:", round(summary_rating[3], 2)), col = "orange", pos = 4)

##Comparación variables cuantitativas y cualitativas

#complete_data <- na.omit(data.frame(rating = anime_ratings, members = anime$members))

#x <- complete_data$rating
#y <- complete_data$members / 10^6  

#plot(x,y,
#     main="Rating vs Miembros",
#    col=viridis(6),
#     pch=20,
#     xlab="Rating",
#     ylab="Miembros/10^6"
#     )
#legend("bottomright",legend=levels(tabla_tipo),fill = unique(tabla_tipo),cex=0.75)

##Boxplots

boxplot(
  rating~type,
  data=anime_SA,
  outline=FALSE,
  col = viridis(6),
  ylim=c(0,10),
  main="Boxplot"
)
abline(h=mean(anime_SA$rating),col="red",lwd=2)
text(paste("",round(mean(anime_SA$rating),2)),x=0.5,y=6,col="red")