Hi there. I have recently played around with the geom_tile() function in the ggplot2 package in the statistical program R. With the geom_tile() function, you can create something as simple as a Rock, Paper, Scissors table.
This page will be about creating such a chart in R as shown below.
Table Of Contents
Rock, Paper Scissors Game
The well known game of Rock, Paper, Scissors is a (non-violent) one on one game using hands only. The winner of this game may or may not win a prize. The player chooses one of rock, paper, scissors and the second player does so also at the same time as the first player. An outcome is determined depending on the choices made. Here is a summary visual on which choices beats what. (If both players choose the same choice, it is a draw.)
Instead of the visual above, a visual table or a matrix plot will be made as an alternate visual.
Creating The Data Table
We start by making a data table in R.
As a player, you can choose one of Rock, Paper, Scissors. The opponent also has three choices. We would have nine possibilities from 3 x 3 = 9.
An empty matrix is created in R. This matrix would have 9 rows for the 9 possibilities and three columns. The first column would be the player’s choice, the second column would be the opponent’s choice and the third column would represent the outcomes as in either Win, Lose or Draw for the player.
Here is the code for setting up the data table in R.
There is another way to create the table. This involves the use of Cartesian products from the mathematical field of Set Theory. This alternative is not shown here since there are 9 combinations and can be hard coded in. Cartesian products would be useful for larger tables.
Creating The Rock, Paper, Scissors Table Visual
We can now start to make the matrix plot visual in R.
First, we convert the Outcome column into a column of factors. The resulting factors would be Win, Lose and Draw (for the player).
# Convert Outcome as Factors:
dataTable[, 3] <- as.factor(dataTable[, 3] )
'data.frame': 9 obs. of 3 variables:
$ You : chr "Rock" "Rock" "Rock" "Paper" ...
$ Opponent: chr "Rock" "Paper" "Scissors" "Rock" ...
$ Outcome : Factor w/ 3 levels "Draw","Lose",..: 1 2 3 3 1 2 2 3 1
The ggplot2 library in R is loaded too.
Here is the code and output for creating the rock, paper, scissors table in R.
# Creating the Rock, Paper, Scissors Matrix Plot:
ggplot(dataTable,aes(x = dataTable[, 1],y = dataTable[, 2],fill = dataTable[, 3])) +
scale_fill_manual(values = c('blue','red', "green")) +
labs(x = "Your Choice", y = "Opponent's Choice",
title = "Rock, Paper, Scissors Chart", fill = "Your Outcome") +
theme(plot.title = element_text(hjust = 0.5),
axis.title.x = element_text(face="bold", colour="#FF7A33", size = 12),
axis.title.y = element_text(face="bold", colour="#FF7A33", size = 12),
legend.title = element_text(face="bold", size = 10))
In the x-axis, I have the player’s choices and in the y-axis, I have the opponent’s choices. The third column in the table or the outcomes column is assigned to fill for the colours.
The choices are in ABC order. In scale_fill_manual(), I have values = c(‘blue’,’red’, “green”).
The labs() function allows for labels. These labels need to be specified in the labs() function.
I now know how to center titles in ggplot2. To center titles, use theme() with plot.title = element_text(hjust = 0.5) as an argument.
Inside theme(), I can adjust the fonts and sizes of the x-axis, y-axis and legend labels using elemen_text() with the arguments face = , colour = , and size = . Note that”#FF7A33″ is the HTML colour code for the salmon-looking label colour.
Here are some useful websites which were useful to me.
- Inspired By This Post (I like this person’s work too.): https://scriptsandstatistics.wordpress.com/2017/03/30/how-to-plot-a-companion-planting-guide-using-ggplot2/