# Creating A Pokémon Types Matrix Plot (Gen 1) In R

Hi there.

This page is about creating a Pokémon Types Table In R. This table will deal with first generation of Pokémon which includes the first 150 Pokémon and Mew. In Generation 1, there are no Dark, Steel or Fairy types. The main reference I am using is http://pokemondb.net/type.

References

• http://stackoverflow.com/questions/10232525/geom-tile-heatmap-with-different-high-fill-colours-based-on-factor
• http://stackoverflow.com/questions/16074440/r-ggplot2-center-align-a-multi-line-title
• http://docs.ggplot2.org/dev/vignettes/themes.html
• http://docs.ggplot2.org/current/theme.html
• https://scriptsandstatistics.wordpress.com/2017/03/30/how-to-plot-a-companion-planting-guide-using-ggplot2/
• http://pokemondb.net/type

Related Pages To This Include:

Cartesian Products In R

Making A Rock, Paper, Scissors Table In R

Creating A Rock, Paper, Scissors, Lizard, Spock Matrix Plot In R

Creating A Pokémon (Gen 2 – 5) Types Matrix Plot In R

Creating A Pokémon (Gen 6+) Types Table In R

A Pokémon Types Table (Gen 1)

I have recently found inspiration for some R projects when I looked into the geom_tile() function from R’s ggplot2 package.

This page/guide is about making a Pokémon Types Table In R which will look something as close to this:

Source: https://img.pokemondb.net/images/typechart-gen1.png

Creating A Pokémon Types Table In R

We start with creating the table of Pokémon Types with their damage multipliers.

This first line of R code is for creating the Pokémon Types as a vector.

We check that this vector has a count of 15 for the 15 Pokémon types (No Dark, Steel or Fairy types in Gen. 1).

Since there are 15 Pokémon types, we would need 15 x 15 = 225 cases in our data table. These 225 cases translates to 225 rows in our table. The table could be inputted manually but that would take too long. Instead, the use of Cartesian products from the mathematical field of Set Theory along with R’s expand.grid() function is applied. Please refer to the code and output below.

An empty third column for Attack Damage multiplier is added. (NA means missing value)

The column titles are currently Var1 and Var2 with an unnamed third column name. The colnames() function is used to assign new column names.

Filling In The Third Column

This is the part where there is quite a bit of work. Because a third empty column was added there are 225 missing values denoted by NA. Each row where there is an attack type and defense type has to be dealt with separately. (There is no for/while loop solution here.)

If there is attack type advantage then the damage does normal damage (x1). This is denoted by 1 in the third column.

An attack type advantage over the defending Pokémon type results in double the damage applied from the attacking move to the defending Pokémon. This is denoted by 2 in the third column

If there is an attack type which is not favourable against the defending Pokémon type results in half damage (x1/2). The attacking move was not very effective.

There are cases where certain attack types have no effect on the defending Pokémon type. The damage multiplier her is just 0.

Here is the full code for filling in the third column (Attack Damage Multiplier):

This next piece of code involves having the values from the third column as factors. Also I check the structure of the table just as a check.

Creating The Pokémon Types Matrix Plot In R

The table of Pokémon Types and damage multipliers is now ready for plotting purposes.

The ggplot2 package in R for data visualization is first loaded.

Here is the code and output for the Pokémon types matrix plot. In this plot, the defending Pokémon types are on the x-axis (bottom) and the attacking Pokémon type is on the y-axis (side/vertical).

In ggplot(), I have caretesianProd as the data, x = cartesianProd[, 2] for the defending Pokémon type, y = cartesianProd[, 2] for the Pokémon attack type and the third column cartesianProd[, 3] for the fill colours (red, gray, green, black).

The parts geom_tile() and scale_fill_manual(values = c(‘black’, ‘red’,’gray’, “green”)) provide tiles and the colours for the plot.

The labs() function provides labels for the plot. Arguments are needed in labs() to get desired labels. In the plot above, I put in labels for the x and y axes, the title and the legend title.

The theme() function allows for visual adjustments for labels. I use plot.title = element_text(hjust = 0.5) inside theme() in order to center the title. Labels for axes and the legend title can also be altered inside theme().

By default, the tick labels on the x-axis are horizontal. Leaving them horizontal would be a visible mess. I use axis.text.x = element_text(angle = 90,vjust = 0.2, hjust = 1) to have the tick labels vertical for clarity. (E.g. Normal, Fire, Water, Electric on the bottom axis are sideways text and looks cleaner.)

Version 2 Plot

Another version of the plot would be switching the axes. That is, attacking types on the bottom and defending Pokémon types on the y-axis (side/vertical).

Version 3 Plot

This plot has the x-axis as the defending Pokémon type and the attacking move type as the y-axis. The x-axis labels are now on the top instead of the bottom. The usage of  scale_x_discrete(position = “top”) allows for the x-axis labels to move to the top.

Version 4 Plot

This last plot I have here has the attack types on the x-axis and the defense types on the y-axis with x-axis labels on top.

Using The Pokémon Types Matrix Plot

For this section, we will refer to the Version 3 plot:

Using this table is not too hard.

As an example, if the defending Pokémon type is a Rock type and the attacking move is a water type then the attack would double the normal damage as indicated by the green square.

Dual Typed Defending Pokémon Cases

There are dual-typed Pokémon tables out there but you could use the single type table twice. As an example, an Ice type move to a Grass and Water type would deal normal damage (x1) as we have the multipliers of 2 and 1/2.

A Look At Pokémon Types From The Table

You can use a Pokémon types table to look at matchups and figure out which types are more defensive/offensive in general. Dragon typed attacks deal normal damage or double damage to other Dragon types. Good defensive types include Dragon, Ghost, and Psychic types. (You could make arguments for Rock and Grass types.) Some solid offensive types include Psychic, Ice and Rock. Fighting and grass type moves are not great versus a lot of types.