Little useless-useful R functions – Drawing randomly generated @Github contribution graph

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 <- 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! 🙂

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)