Pokemon and TrelliscopeJS!

December 29, 2016
By

(This article was first published on Ryan Hafen, and kindly contributed to R-bloggers)

I’m always looking for ways to spark my kid’s interest in computers, data, etc. This has proven to be more difficult than I thought it would be (kids these days…). I suspect this may have something to do with the ubiquity of electronic devices that “just work”, making them less novel and less interesting to tinker with, but speculation on this is a post for another time…

Anyway, all of my kids are crazy into Pokemon so when I came across some Pokemon data the other day that leant itself very nicely to a Trelliscope display, I thought I might have a chance to engage them. And then I thought why not write a post about it. You can find the resulting display and code to recreate it in this post. Hope you enjoy!

To start out, here’s the display:

If this display doesn’t appear correctly for you (because of blog aggregators, etc.), you can follow this link to the display in a dedicated window. For better viewing, you can also click the bottom right “fullscreen” button to expand the display to fill the window.

The data from which this was created is a simple data frame of Pokemon statistics, based on this source (which borrows from here). I slightly modified the data to add some variables that enhance the display (I changed the image URL to a better source, added a variable “pokedex” that provides a link to the pokemon’s pokedex entry on pokemon.com, and removed a few special Pokemon that I couldn’t find on pokedex).

Since this data is simply a data frame where each row refers to a Pokemon, it lends itself nicely to a Trelliscope display showing an image of the Pokemon as the panel and allowing interaction with the Pokemon being viewed based on the various statistics provided.

Here’s the code to make the display. Once the data is read, it’s just a few lines.

# install packages if not installed
devtools::install_github("hafen/trelliscopejs")
install.packages(c("readr", "dplyr"))

library(readr)
library(dplyr)
library(trelliscopejs)

# read the data (making "_id" columns strings)
pok <-
  read_csv("https://raw.githubusercontent.com/hafen/pokRdex/master/pokRdex_mod.csv") %>%
  mutate_at(vars(matches("_id$")), as.character)

# take a look
glimpse(pok)
Observations: 801
Variables: 30
$ pokemon                  "bulbasaur", "ivysaur", "venusaur", "ve...
$ id                       1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...
$ species_id               "1", "2", "3", "3", "4", "5", "6", "6",...
$ height                   7, 10, 20, 24, 6, 11, 17, 17, 17, 5, 10...
$ weight                   69, 130, 1000, 1555, 85, 190, 905, 1105...
$ base_experience          64, 142, 236, 281, 62, 142, 240, 285, 2...
$ type_1                   "grass", "grass", "grass", "grass", "fi...
$ type_2                   "poison", "poison", "poison", "poison",...
$ attack                   49, 62, 82, 100, 52, 64, 84, 130, 104, ...
$ defense                  49, 63, 83, 123, 43, 58, 78, 111, 78, 6...
$ hp                       45, 60, 80, 80, 39, 58, 78, 78, 78, 44,...
$ special_attack           65, 80, 100, 122, 60, 80, 109, 130, 159...
$ special_defense          65, 80, 100, 120, 50, 65, 85, 85, 115, ...
$ speed                    45, 60, 80, 80, 65, 80, 100, 100, 100, ...
$ ability_1                "overgrow", "overgrow", "overgrow", "th...
$ ability_2                NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
$ ability_hidden           "chlorophyll", "chlorophyll", "chloroph...
$ color_1                  "#78C850", "#78C850", "#78C850", "#78C8...
$ color_2                  "#A040A0", "#A040A0", "#A040A0", "#A040...
$ color_f                  "#81A763", "#81A763", "#81A763", "#81A7...
$ egg_group_1              "monster", "monster", "monster", "monst...
$ egg_group_2              "plant", "plant", "plant", "plant", "dr...
$ url_image                "http://assets.pokemon.com/assets/cms2/...
$ generation_id            "1", "1", "1", NA, "1", "1", "1", NA, N...
$ evolves_from_species_id  NA, "1", "2", NA, NA, "4", "5", NA, NA,...
$ evolution_chain_id       "1", "1", "1", NA, "2", "2", "2", NA, N...
$ shape_id                 "8", "8", "8", NA, "6", "6", "6", NA, N...
$ shape                    "quadruped", "quadruped", "quadruped", ...
$ pokebase                 "bulbasaur", "ivysaur", "venusaur", "ve...
$ pokedex                  "http://www.pokemon.com/us/pokedex/bulb...

Now we can create a Trelliscope display by specifying url_image as the source for the panel images. We also specify a default state indicating that the values for the variables pokemon and pokedex should be shown as labels by default.

pok %>%
  mutate(panel = img_panel(url_image)) %>%
  trelliscope("pokemon", nrow = 3, ncol = 6,
    state = list(labels = c("pokemon", "pokedex")))

This will produce the interactive plot shown at the top of this post. You can use the display to find Pokemon based on sorting or filtering on several of their attributes.

Note that despite my kids constantly telling me about and showing me their Pokemon cards, I am not a Pokemon expert, so there may be some interesting things I am missing. But I can say that my kids were finally impressed and engaged with something that I showed them. Success!

If this is your first exposure to Trelliscope and you are interested in other things it can do, please see my original blog post.

This is my last post of the year. Happy new year!

To leave a comment for the author, please follow the link and comment on their blog: Ryan Hafen.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)