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

Hi there. This is a follow up from a previous page on me making a Rock, Paper, Scissors matrix plot/table in the statistical programming language R.

The game of Rock, Paper, Scissors, Lizard, Spock is merely an extension of Rock, Paper, Scissors. We have added the lizard and the Spock as extra options. Just like the other options, the lizard and the spock has its own strengths and weaknesses. Here is a summary visual from the link http://www.momsminivan.com/rock-paper-spock.jpg on which beats what. (A draw occurs when both players choose the same thing.)

Here is another visual with the hand symbols for each choice.

Creating The Outcome Table In R

In Rock, Paper, Scissors, we can create a table with 9 rows and 3 columns. The 9 rows is from 3 choices from the player multiplied by the 3 choices from the opponent. The 3 columns represent the choice from the player, the choice from the opponent and the outcome for the player (Draw, Win, Loss).

With Rock, Paper, Scissors, Lizard, Spock, both players choose one out of choices. We would have 25 rows and 3 columns.

In R, we first create an empty matrix with missing values (NA).  Then we convert this matrix into a data frame.

The column names in the data table can be filled in using the colnames() function.

Next, some of the data is being filled in.

I run this check to check my table is okay so far and error free. This check also helps the reader visualize how the code works. Now, we have the outcome column to fill in.

For the outcome column in the data, I am filling it in manually. After that, I put the outcomes column into the third column of the data table and convert the column as factors.

We can perform a check to make sure our table is formatted correctly.

Creating The Matrix Plot In R

Now that the table is nicely formatted, the matrix plot can be generated.

First, we load the ggplot2 library in R. (You could also load ggplot2 at the very beginning.)

The ggplot() command along with geom_tile() and other addon commands are used to generate the matrix plot.

# Creating the Rock, Paper, Scissors Matrix Plot:

In ggplot(), I specify dataTable as my data object, the first column of dataTable to go with the x-axis, the second column of dataTable to go with the y-axis and the third column of dataTable to determine the fill colours.

The command geom_tile() is crucial for obtaining this matrix plot.

The command scale_fill_manual(values = c(‘blue’,’red’, “green”)) gives the colours for Draw, Loss and Win respectively.

I include labels using the labs() command. Inside labs(), you need to specify which labels you want.

I can change the label fonts and title fonts using theme(). The title can be centered using plot.title = element_text(hjust = 0.5) inside theme(). I use element_text() inside theme to modify the labels I want to change. Note that “#3355FF” is the HTML code for the blue shade I have selected.

An Alternate Method To Creating The Outcome Table

There is an alternate way to create the outcome table. The previous table involved create an empty matrix, converting it into a data frame and filling in the data in a more manual fashion.

This alternate way uses Cartesian products from Set Theory. You can use the function expand.grid() in R to create the table in a more efficient manner. The code to create the table can be found below.

From here, a third empty column can be added. The third column still has to be inputted manually and accordingly.

(Code shown below with matrix plot output only.)

This “alternate” matrix plot is a little different from the one before as the order is now Rock, Paper, Scissors, Lizard, Spock.

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/