Skip to content

Instantly share code, notes, and snippets.

@simecek
Last active September 13, 2018 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simecek/537ee3c61f69695f5de7a735e73b5df0 to your computer and use it in GitHub Desktop.
Save simecek/537ee3c61f69695f5de7a735e73b5df0 to your computer and use it in GitHub Desktop.
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