Let’s get LEGO’d!

February 23, 2019

(This article was first published on Little Miss Data, and kindly contributed to R-bloggers)

Screen Shot 2019-02-23 at 4.11.02 PM.png

If you follow my blog, you may have noticed that I like to create some pretty weird and nerd-fabulous graphs.

In this blog I’ll introduce you to a new and exciting form of neat R graphs that I recently stumbled upon. Often times, my blogs are tutorials. However, since Ryan Timpe and Tyler Morgan-Wall have done such a great job documenting their work, this blog is more like a public service announcement with a few tips.


The blog

Ryan’s blog on creating 3D Lego image masterpieces can be found here. Ryan kindly created a series of functions to transform your image to legos and then turn them 3D building on Tyler’s R rayshader package.



In my journey to create some 3D Lego images of my own, I learned a few tips. Below, I’ll give you the super condensed version of what I did which is mostly pointing to their joint content!

1) Run Ryan’s script to create the Lego functions in R

There is a whole bunch of genius here. You can definitely browse the code but also, just know that it works to create the functions that you will call when making your lego graphs.

2) Run through Ryan’s steps to create the Lego mosaics

Ryan has done a great job making the code very straightforward, but I’ll still review the steps and the associated output below.

2A) Read in your image and transform it into Lego pieces

mosaic1 <- readJPEG("/MyPics/Laura.jpg") %>% 
  scale_image(48) %>%
  legoize() %>% 

mosaic1 %>% display_set()




2B) Generate the instructions to create your own physical lego image

The transformed lego image is great, but what’s better is if you could actually make this with real Legos! This function generates your own personal guide to creating your real Lego masterpiece.

mosaic1 %>% 




2C) Identify which Lego pieces you will need to build your image
Ryan thought of everything. If you are going to create the image with real Legos, you need to know what pieces to use! Call the display_pieces() function to get a comprehensive list of what to use to build your image. Note that these are created with the real lego colors and you can order most of them through Lego’s “Pick a Brick” website.

pieces <- mosaic1 %>% table_pieces()
mosaic1 %>% display_pieces()



2D) Create your 3D Lego image
Follow the code below to create your 3D Lego image. If you are finding that you want to flip between which image area is elevated, please see the tips and troubleshooting section below.


# install.packages("rayshader")
# If you have issues installing rayshader, please see tips below.

mosaic1 %>% 
  collect_3d() %>% 




Tips and Troubleshooting

  • If you have issues installing rayshader on Mac, I found Tylers tip to install XQuartz directly worked like a charm.

  • When running step 1, it assumes that you have the “Lego_Colors.csv” file on your computer. If you don’t want to bother downloading it, please replace the referenced lego color file (line 5) with “https://raw.githubusercontent.com/ryantimpe/LEGOMosaics/master/Colors/Lego_Colors.csv”. For example:


lego_colors <- read_csv("https://raw.githubusercontent.com/ryantimpe/LEGOMosaics/master/Colors/Lego_Colors.csv",..”




Calling all RLadies and Rladdies

Because this is such a cool graphing technique, I ran a variety of other images through the functions. Specifically I thought the R Ladies graphic turned out pretty great! Gabriela de Queiroz (Founder of RLadies), had the great idea to 3D print the image. I couldn’t get that to work yet. If anyone is able to successfully 3D print the image or build the actual lego representation, please let me know! I’m posting the RLadies lego instructions below, in case anyone feels inspired!


Thank you

Thank you for following along with this fun little blog to showcase Ryan Timpe and Tyler Morgan-Wall’s R wizardry. Please share your creations with us on twitter!

To leave a comment for the author, please follow the link and comment on their blog: Little Miss Data.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)