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.
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!
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.
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() %>% collect_bricks() 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 %>% generate_instructions(6)
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. library(rayshader) mosaic1 %>% collect_3d() %>% display_3d(fov=0,theta=-30,phi=40,windowsize=c(1000,800),zoom=0.8) render_snapshot()
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",..”
By default collect3d() displays the lighter bricks at a higher elevation. To display the darker bricks in a higher elevation, Ryan suggested to set the collect_3D() function argument highest_el='dark'. For example:
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!