Little useless-useful R functions – Drawing randomly generated @Github contribution graph
This article is originally published at https://tomaztsql.wordpress.com
Github is collecting various images on their Instagram and Twitter that looks like a contribution graph.
Well, why not create one in R with randomly generated data and the use of the ggplot library.
Run the function by yourself:
library(ggplot2)
# introduce Github colours
colours <- c("#ebedf0", "#9be9a8", "#40c463","#309b4c","#216e39")
# colours: lavender, palegreen, mediumseagreen,seagreen, forestgreen
# nof_contributions: 0, 1, 2-5, 5-10, 10+
# Generate grid on imaginary
y <- LETTERS[1:7] # letters
y <- weekdays(Sys.Date()+0:6) # days
x <- paste0("W_", seq(1,52))
data <- expand.grid(X=x, Y=y)
data$cols <- sample(colours, 364, replace=TRUE)
# Contribution graph
ggplot(data, aes(X, Y)) +
geom_tile(aes(fill = cols, width=0.9, height=0.9)) +
scale_fill_manual(values=colours) +
labs(title = "Your R generated contributions in past year") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank(),
legend.position = "none",
panel.background = element_rect(fill = 'white', color = 'white')
)
And there you go!

Jokingly: …. If you want to see the tiles, that you usually see at your local pool, run this slightly altered code:
# Generate grid
colours <- c("#ebedf0", "#9be9a8", "#40c463","#309b4c","#216e39")
y <- LETTERS[1:60] # letters
x <- seq(1,60)
data <- expand.grid(X=x, Y=y)
data$cols <- sample(colours, 3600, replace=TRUE)
ggplot(data, aes(X, Y)) +
geom_tile(aes(fill = cols, width=0.95, height=0.95)) +
scale_fill_manual(values=colours) +
labs(title = "Your local pool tiles") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
legend.position = "none",
panel.background = element_rect(fill = 'white', color = 'white')
)
As always, code is available on Github in the same Useless_R_function repository. Check Github for future updates.
Happy R-coding, stay healthy and contribute on Github!
Thanks for visiting r-craft.org
This article is originally published at https://tomaztsql.wordpress.com
Please visit source website for post related comments.