Instantly share code, notes, and snippets.

Embed
What would you like to do?
if (!require(tidyverse)) {
install.packages("tidyverse")
library(tidyverse)
}
# Game of Life visualisation
draw_board <- function(board) {
# number of rows and columns
M = nrow(board)
N = ncol(board)
# melt board for ggplot
mboard <- melt(board)
names(mboard) <- c("row", "col", "value")
mboard$value <- factor(mboard$value, levels=c(0,1))
# box around the board
boundingbox <- data.frame(x=c(0.5,0.5,M+0.5,M+0.5), y=c(N+0.5,0.5,0.5,N+0.5))
ggplot(mboard, aes(x=row, y=col, fill=value)) +
geom_tile() +
coord_flip() +
scale_x_reverse() +
theme_void() +
theme(legend.position = "None") +
scale_fill_manual(values = c("white", "black")) +
geom_polygon(data=boundingbox, aes(x=x, y=y),colour="black", fill=NA)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment