Last active
September 13, 2018 16:45
-
-
Save simecek/537ee3c61f69695f5de7a735e73b5df0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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