LEGO mosaics in R with brickr

Package

brickr

Author

Ryan Timpe

Mosaics

The brickr package can be used to create LEGO mosaics from images. Read a PNG or JPEG image file and apply the image_to_mosaic and build_mosaic functions as in the following example.

# install.packages("brickr")
library(brickr)
# install.packages("png")
library(png)

# Get the image
img <- tempfile()
download.file("https://r-charts.com/images/favicon.png",
              img, mode = "wb")

# Create the LEGO mosaic
readPNG(img) %>%
  image_to_mosaic() %>%
  build_mosaic()

image_to_mosaic and build_mosaic functions

LEGO instructions

You can build the instructions of your mosaic with build_instructions. The default number of steps is 6.

build_instructions brickr

# install.packages("brickr")
library(brickr)
# install.packages("png")
library(png)

# Get the image
img <- tempfile()
download.file("https://r-charts.com/images/favicon.png",
              img, mode = "wb")

# Create the LEGO instructions
readPNG(img) %>%
  image_to_mosaic() %>%
  build_instructions()

If you want to change the number of building steps modify the num_steps argument of the build_instructions function.

build_instructions function

# install.packages("brickr")
library(brickr)
# install.packages("png")
library(png)

# Get the image
img <- tempfile()
download.file("https://r-charts.com/images/favicon.png",
              img, mode = "wb")

# Create the LEGO instructions
readPNG(img) %>%
  image_to_mosaic() %>%
  build_instructions(num_steps = 4)

LEGO pieces

You can also get the count of each LEGO bricks needed with build_pieces.

# install.packages("brickr")
library(brickr)
# install.packages("png")
library(png)

# Get the image
img <- tempfile()
download.file("https://r-charts.com/images/favicon.png",
              img, mode = "wb")

# List of LEGO pieces
readPNG(img) %>%
  image_to_mosaic() %>%
  build_pieces()

build_pieces function

ggplot2

Elegant Graphics for Data Analysis

Buy on Amazon
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon
Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon

See also