Introduction to the

Today’s content is more “useless”, feel more fun, so do a “different” visualization! The BrickR package, which brings LEGO into the R and Tidyverse ecosystem, is divided into two parts:

  • Mosaics: Mosaic images converted into Lego bricks.
  • 3D Models: Build 3D Lego models from data tables using the RGL package.

Today’s issue will focus on the first part:

Mosaic

PNG or JPEG images are first read, and then the Lego version of the Mosaic is built using image_to_mosaic and build_mosaic functions. Here’s an official example:

Library (brickr) # install. Packages (" PNG ") library(PNG) download.file("https://r-charts.com/images/favicon.png", img, Image_to_mosaic () %>% build_mosaic()Copy the code

If the package is not installed, use install.Packages (” Brickr “) to install it. Here readPNG() comes from the PNG package, remember to install it ahead of time too.

Here is to create a temporary file IMG, and then download an image from the Internet, and store in IMG. Then the image is read and converted into Mosaic data, and finally constructed into Mosaic map.

Of course, if you can load local images, for example, xiaobian will upload their own public number of avatar demonstration:

 readPNG("avatar.png") %>%
   image_to_mosaic() %>%
   build_mosaic()
Copy the code

The resulting graph looks like this:

Image_to_mosaic () contains some important parameters that can be seen in detail. Image_to_mosaic (). The img_size parameter is used to adjust the image size. If it is one value, it means creating a square image, and if it is two values c(56,48), it means creating a rectangle image with length 56 and width 48. The higher the value, the higher the pixels, and the denser the Mosaics in the Lego version, for example when img_size = 120, the result is:

It feels good to use it as a profile picture! If you want to use this feature to create avatars, make sure the background is a solid color for better results. If the photo is not in PNG format, you can use this website to convert it to PNG format and import it.

Introduction to the Build process

You can use build_instructions to describe the process of building this diagram. The default is 6 steps.

 library(brickr)
 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()
Copy the code

I apply it to the tweets: R on | use R to the right point shape, let then get the expression of love build steps are as follows:

Is it still very Romantic 👻

Use lego materials

You can use the build_pieces function to tell you which colors and shapes of lego materials are used. For example, the official website example:

 library(brickr)
 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()
Copy the code

Xiaobian has something to say

  • Today we will introduce some functions of brickR package, mainly talking about the use of three functions and explain the results of the picture, see this tutorial.
  • I think this package is really interesting. You can upload your profile picture and turn it into a Mosaic as your profile picture. Isn’t that cool? Ha ha.
  • Another feature of this package will be introduced in the next issue, constructing 3d mosaics!