library(tidyverse)
library(jsonlite)
library(magrittr)
library(httr)PokeAPI
Introduction
I am going to use the free PokeAPI that provides information on the Pokemon franchise and the Pokemon within the franchise. It is fairly comprehensive and provides a ton of data including move sets, items, and game information.
PokeAPI is free and doesn’t require an account to use the API, therefore it is straightforward to use and pull data.
Load Packages
Calling the API
Next I want to call the API so I can use the data.
url_poke <- "https://pokeapi.co/api/v2/pokemon"
url_poke_content <- url_poke %>%
GET()
poke_data <-
url_poke_content %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON()Stats function
Next I will create a reusable function that allows me to pull the stats for the Pokemon much easier. The height and weight is easy to pull but the stats of the Pokemon are nested so it is much more challenging to pull them out to get a true look at the stats. So to do that I am going to specify which stat I want for that specific stat so that they line up (longest code block of my life)
get_pokemon_stats <- function(pokemon_name) {
url <- paste0("https://pokeapi.co/api/v2/pokemon/", pokemon_name)
data <- url %>%
GET() %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON()
name <- data[["name"]]
height <- data[["height"]]
weight <- data[["weight"]]
stats <- data[["stats"]]
hp <- stats$base_stat[1]
attack <- stats$base_stat[2]
defense <- stats$base_stat[3]
sp_attack <- stats$base_stat[4]
sp_defense <- stats$base_stat[5]
speed <- stats$base_stat[6]
data.frame(
name = name,
height = height,
weight = weight,
hp = hp,
attack = attack,
defense = defense,
sp_attack = sp_attack,
sp_defense = sp_defense,
speed = speed
)
}Pokemon Stats
Now I will pull the stats of some of the most well known Pokemon. I love Pokemon and have played my whole life (nerd), so by doing this I will finally prove that my guy Mudkip is the best starter.
starters <- c(
"bulbasaur", "charmander", "squirtle", # Gen 1
"chikorita", "cyndaquil", "totodile", # Gen 2
"treecko", "torchic", "mudkip", # Gen 3
"turtwig", "chimchar", "piplup" # Gen 4
)
all_stats <- map_dfr(starters, get_pokemon_stats)Comparison
Now to finish it off I will make a simple graph to compare the stats of the Pokemon.
all_stats %>%
mutate(total_stats = hp + attack + defense + sp_attack + sp_defense + speed) %>%
ggplot(aes(x = name, y = total_stats)) +
geom_col() +
labs(
title = "Total Base Stats Comparison",
x = "Pokemon",
y = "Total Stats"
) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))Analysis
Okay well I guess comparing the total of the base stats isn’t the best visual but it is the simplest way to compare. According to base stat totals Chikorita would technically be the best, but Mudkip is still definitely better. Nobody uses Chikorita anyways so that is kind of irrelevant.