Blego: The Lego Blog


Welcome to the Wonderful World of Lego!

We can’t be the only people who have spent evenings lying awake in bed wishing that we could render some lego bricks in R. Luckily for us, the wonderful Simon Couch introduced us to a package called brickr created by Ryan Timpe, where we can do that, and so much more! Brickr has two main functions- creating 2D mosaics from uploaded images, and rendering legos or sets in 3D. We highly recommend brickr as a fun and educational method of procrastination. Both the brickr github repo and Rian Timpe’s personal repo have many fun examples of brickr in action, including a Baby Yoda model! We spent SO MUCH TIME trying to get the brickr 3D renderings to work correctly, and at one point even considered DMing the creater on twitter to see if he could help us. But we finally figured it out, and rendered the most beautiful red 2 x 4 lego brick we had ever seen. While we made many things with brickr, we decided to include three 3D renderings: “Hello Kelly” (because we wanted to say hi to Kelly!), “Izzy-Iggy” (the name of our github repo from the project), and a cute little house we designed instead of studying for our other finals! All three of these were made using the bricks_from_excel() function in brickr, and were designed on a template excel document that was set up to be read by brickr.

The brickr github repository can be found here

#a cute little house!

read_xlsx("data/brickr_pattern.xlsx", sheet = "mybrickr") %>% 
  bricks_from_excel() %>% 
 build_bricks(rgl_lit = FALSE, outline_bricks = TRUE,
               background_color =  "#99e7ff")

par3d(userMatrix = rotate3d(par3d("userMatrix"), .1*pi, 0, 0 ,1))

We’ve all been there, you’re sitting with a cup of coffee in hand staring upon your giant lego collection and thinking, “I wonder what the largest lego set is?”. Well, it turns out the largest lego set is the 2017 Millenium Falcon, which has 7541 pieces in it. Since 2000 there have been 21 different versions and redesigns of the millenium falcon set. Other very large lego sets include the Harry Potter Hogwarts Castle with 6020 pieces, the Taj Mahal with 5932 pieces, and the 2007 Millennium Falcon with 5197 pieces.

The data used to create the following graphs comes from the Rebrickable website. here. Rebrickable allows users to input different lego pieces and gives them instructions to create sets out of the pieces they already own. This means that Rebrickable has tons and tons of data on legos, literally twelve different CSV’s of colors, size, part, theme, anything you could possibly want to know about an individual lego or set. Rebrickable has all of their data available for free on their website or through their API. The data was luckily in a tidy format (yay!) and using the handy schema we were able to combine data sets containing the information we desired. To create the radial networks and other graphs we desired the data needed to be formatted into a data tree. Luckily, the data.tree package has just the tools to make that happen! A data tree is a made up of nodes, where each node is a data structure consisting of a value. In a couple of our data trees the ‘root’ node is legos and then from there the ‘children’ nodes are themes, sets, and maybe the number of parts. The data tree is non linear in the sense that none of the ‘child’ nodes can point back to the ‘root’ node. The data.tree package allowed us to easily create these nested data frames using the paste function, the $ operator, and the as.node function.

Here are some cool graphs we made!!

Once our data trees were created we could start graphing (yay!). At the center of the radial network is legos, from there the first set of nodes is the top ten lego themes by number of occurrences, and each the secondary set of nodes is the largest sets, limited to 100 for legability. Perusing through the network we can see that Star Wars is a popular theme with many large sets.

(See the readme document/the radial_network.html file for information on our troubles and the radial network we made)

##Keep this
# Circlepacker package

the_themes <- sets_themes %>%
  count(theme_name) %>%
## Selecting by n
sets_themes$pathString <- paste("legos", 
                            sep = "/")

top_5_themes <- left_join(the_themes,sets_themes, by="theme_name") %>% 
  top_n(100, num_parts)

legos_circle <- as.Node(top_5_themes)

# Make the plot

circle_g <- circlepackeR(legos_circle, size = "num_parts", color_min =
          "hsl(56,80%,80%)", color_max = "hsl(341,30%,40%)", 
          width = 1000, height = 800)


The largest circle in this figure represents legos i.e the tree root , then the next set of circles is the top five themes within legos. Within each theme are the largest sets and clicking on an individual set’s circle you can see the number of parts within that set. This graph is limited to the hundred largest lego sets within those top five themes. With the circlepackeR and the networkD3 packages we found ourselves wanting to change some of the characteristics of the graphs such as font size and spacing and it was slightly infuriating that these were not adjustable within the packages.

The Millennium Falcon

The 2017 Millennium Falcon lego set is the largest lego set to date, with 7,541 pieces. But what else can we learn about the Millennium Falcon set without paying the hefty $799.99 price tag? The set contains 377 different part types from 39 different part categories, made up of 24 different colors. There are 150 spare parts, and 26 translucent parts. The most common color with 3304 parts is “light bluish grey”, which makes up almost 44% of the set. The most common part is a Black Technic Pin with Friction Ridges Lengthwise and Center Slots (there are 269 of them) with dark tan 1 x 2 plates as the second most common part (with 243 of them). The circle graph that we made about the Millennium Falcon shows a more in-depth distribution of the colors that make up the set. The circles, from biggest to smallest, represent all parts, all parts of a specific color, the categories of parts of each color, the part name, and then the quantity of that specific part in that specific color. With this graph, you can figure out exactly how many Black Technic Pin with Friction Ridges Lengthwise and Center Slots are in the set without having to do a bunch of data wrangling.

#USC Millennium Falcon, 7541 parts, released 2017
#set num = 75192-1

millenium_falcon <- parts_inventory %>%
  filter(inventory_id == 19670) %>% 
  left_join(colors, by="color_id") %>%
  left_join(parts, by="part_num") %>%
  left_join(parts_categories, by="part_cat_id") %>%
  rename("part_cat"="name", "color_name"="name.x", "part_name"="name.y")
## Warning: Column `part_num` joining factors with different levels, coercing to
## character vector
millenium_falcon$pathString <- paste("mf", 
                            sep = "/")

MILF <- as.Node(millenium_falcon)

#color, then part category, then part name, then part quantity
m <- circlepackeR(MILF, size = "quantity", width = 1000, height = 800)


The set can be configured to both old and new Star Wars styles, and can be piloted by either old or young Han Solo, depending on your preference. It contains 8 minifigures, including Princess Leia, Rey, and BB-8. Unfortunately, the set is currently out of stock on so we’ll need to find a different activity to keep us entertained during quarantine.

millennium_figs <- inventories %>%
  filter(inventory_id == 19670) %>%
  left_join(minifig_inventory, by="inventory_id") %>%
  left_join(minifigs, by="fig_num") %>%
  select(4, 6, 7)
## Warning: Column `fig_num` joining factors with different levels, coercing to
## character vector
##      fig_num                                       name num_parts
## 1 fig-001714              Chewbacca with Dark Brown Fur         3
## 2 fig-001810                                       Finn         4
## 3 fig-002057                       Rey in Crossed Robes         4
## 4 fig-002514                    C-3PO with Printed Legs         3
## 5 fig-002544                                       BB-8         2
## 6 fig-002549               Princess Leia in Hoth Outfit         4
## 7 fig-002550 Han Solo with Dark Brown Legs with Gunbelt         4
## 8 fig-002551                             Han Solo (Old)         4

Colors of the Millennium (the year 2000)

After looking in depth at the Millennium Falcon, we found ourselves wondering about what the most common colors in other lego sets are. And continuing with our “millennium” theme, we looked at lego sets that were released in the year 2000. The treemap package turned out to be frustrating to work with. There isn’t a lot of documentation, and any documentation hasn’t been updated since 2017. Ideally, the treemap would be colored by the color of the part represented, but we couldn’t figure out how to fix that. Either way, the figure shows that black and light grey were the most common colors used in 2000, with 8660 black pieces and 4962 light grey pieces. The treemap also doesn’t display the numbers, so we had to figure those out with some classic dplyr techniques.


bigcolors %>%
## # A tibble: 10 x 2
##    color      n_parts
##    <fct>        <int>
##  1 Black         8660
##  2 Light Gray    4962
##  3 Red           3259
##  4 White         2928
##  5 Yellow        2876
##  6 Sand Green    2867
##  7 Dark Gray     2465
##  8 Blue          2372
##  9 Green         1877
## 10 Tan            914

Kelly’s Angels

mosaic <- png::readPNG("iggy-izzy.png") %>% 
  image_to_mosaic(img_size = 80) 

mosaic %>% build_mosaic()

mosaic %>% build_pieces()

On a final note, have you ever wanted see what you would look like made out of lego bricks? Luckily you can actualize your dreams using the brickr package. Building the mosaic is surprsingly fairly straightforward. First you have to upload a png file to your R-studio and then using the read:PNG function to save the image and then pipe it through the build_mosaic function. In addition, the build_pieces function gives you a nice run through of the suggested lego pieces to recreate your image.

We had a lot fun creating these graphics and hope you enjoyed them to!