library(dslabs)
library(readr)
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(viridis)
## Cargando paquete requerido: viridisLite
library(viridisLite)
library(RColorBrewer)
pokemon_db <- read.csv("pokemon.csv")
typeTable <- table(pokemon_db$type1)
par(mar = c(8,4,4,1))
barplot(typeTable, main = "Pokemon type", ylab = "Number of Pokemons", xlab = "Type", las = 2, ylim = c(0,120), col = "lightblue", mgp = c(5.5, 1.5, 1))
pie(typeTable, labels = row.names(typeTable), col = rocket(18), main = "Pie diagram", border = rocket(18))
legend("bottomright", legend = names(typeTable), cex = 0.60, fill = rocket(18))
pokemon_db <- pokemon_db %>% mutate(generation = factor(generation, levels = sort(unique(generation))))
generationTable <- table(pokemon_db$generation)
barplot(generationTable, main = "Number of pokemons by generation", ylab = "Number of pokemons", xlab = "Generation", col = "lightgreen", ylim = c(0, max(generationTable) + 10))
generationTable <- sort(generationTable, decreasing = TRUE)
barplot(generationTable, main = "Number of pokemons by generation (Ordered by number of pokemons)", ylab = "Number of pokemons", xlab = "Generation", col = "red", ylim = c(0, max(generationTable) + 10))
#Histograma que muestra el comportamiento de los HP de los pokemons, también muestra la media y mediana de estos.
hist(pokemon_db$hp, breaks = 20, main = "Histogram about Pokemons HP", xlab = "HP (Health Points)", col = "lightblue", border = "darkblue",ylim = c(0, 150), labels = TRUE)
media_hp <- mean(pokemon_db$hp, na.rm = TRUE)
mediana_hp <- median(pokemon_db$hp, na.rm = TRUE)
abline(v = media_hp, col = "red", lwd = 2, lty = 2)
abline(v = mediana_hp, col = "blue", lwd = 2, lty = 2)
legend("topright", legend = c("Media", "Mediana"), col = c("red", "blue"), lwd = 2, lty = 2)
hist(pokemon_db$speed, breaks = "Sturges", main = "Histogram about Pokemons Speed", xlab = "Speed", col = brewer.pal(10, "Set1"), border = brewer.pal(10, "Set1"),ylim = c(0, 250), labels = TRUE)
## Warning in brewer.pal(10, "Set1"): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors
## Warning in brewer.pal(10, "Set1"): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors
hist(pokemon_db$hp, breaks = "Sturges",probability = TRUE,main = "Density histogram about Pokemons HP", xlab = "HP (Health Points)", col = rocket(10), border = rocket(10), ylim = c(0, 0.02))
lines(density(pokemon_db$hp, na.rm = TRUE), col = "black", lwd = 2)
boxplot(pokemon_db$hp, col = "lightblue", ylab = "HP (Health Points)", outline = FALSE, main = "Pokemons HP - Boxplot", ylim = c(0, 150))
media_hp <- mean(pokemon_db$hp, na.rm = TRUE)
points(media_hp, col = "black", pch = 20)
text(x = 1, y = media_hp, labels = paste("Media:", round(media_hp, 2)), pos = 4, col = "black")
x <- pokemon_db$hp
y <- pokemon_db$speed
plot(x, y, main = "HP vs Speed", col = pokemon_db$generation, pch = 20, xlab = "HP (Health Points)", ylab = "Pokemons Speed", cex = 0.7)
legend("topright", legend = levels(as.factor(pokemon_db$generation)), col = unique(pokemon_db$generation), pch = 20, cex = 0.75)
boxplot(attack ~ type1, data = pokemon_db, col = brewer.pal(9, "Set1"), outline = FALSE, ylim = c(0, 200), main = "Attack by type - Boxplot", xlab = "Pokemon type", ylab = "Attack")
media_attack <- mean(pokemon_db$attack, na.rm = TRUE)
abline(h = media_attack, col = "darkblue", lwd = 2)
text(x = 1, y = media_attack, labels = paste("Media:", round(media_attack, 2)), col = "darkblue")
par(mfrow = c(1, 2))
x <- pokemon_db$speed
y <- pokemon_db$attack
colors <- brewer.pal(n = length(unique(pokemon_db$type1)), name = "Set1")
## Warning in brewer.pal(n = length(unique(pokemon_db$type1)), name = "Set1"): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors
plot(x, y, main = "Speed vs attack", col = as.factor(pokemon_db$type1), pch = 20, xlab = "Speed", ylab = "Attack", cex = 0.7)
legend("topright", legend = levels(as.factor(pokemon_db$type1)), col = colors, pch = 20, title = "Primary type")
boxplot(hp ~ type1, data = pokemon_db, col = brewer.pal(n = length(unique(pokemon_db$type1)), name = "Set1"), outline = FALSE, ylim = c(0, 200), main = "Pokemon HP by Pokemon primary type - Boxplot", xlab = "Primary type", ylab = "HP (Health Points)")
## Warning in brewer.pal(n = length(unique(pokemon_db$type1)), name = "Set1"): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors
media_hp <- mean(pokemon_db$hp, na.rm = TRUE)
abline(h = media_hp, col = "blue", lwd = 2)
text(x = 1, y = media_hp, labels = paste("Media:", round(media_hp, 2)), col = "blue")