YEAR: 2019 +COPYRIGHT HOLDER: Ryan Timpe ++ +
Copyright (c) 2019 Ryan Timpe
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+You can generate a brickr mosaic object from an image using brickr::image_to_mosaic(). Pass this object to build_mosaic() to construct a visualization of the mosaic.
mosaic <- png::readPNG("../Images/mf_unicorn.PNG") %>%
+ image_to_mosaic()
+
+mosaic %>% build_mosaic()
The default is to create a mosaic with 48 knobs (brick studs) on each side. Change this using the img_size input. A single value will create a square mosaic, while an array of two values represent the width and length.

A key feature in image_to_mosaic() is the conversion of the original image into colors produced by LEGO. The default is to map each individual pixel to any of the 41 flat colors currently used for brick production using the CIE94 algorithm. These defaults can be changed.
There are currently 2 main ways to control the color palettes to build mosaics in brickr.
+color_palette input takes a list of pre-defined palettes to create the mosaic.color_table input can take a data frame of specific colors. Usually a subset of the included lego_colors table.Each of the 41 colors belongs to one of three categories:
+Use the color_palette input in the image_to_mosaic() function to limit the bricks used to any combination of these three categories.
p1 <- png::readPNG("../Images/mf_unicorn.PNG") %>%
+ image_to_mosaic(32, color_palette = c('universal', 'generic')) %>%
+ build_mosaic(title = "universal & generic")
+
+p2 <- png::readPNG("../Images/mf_unicorn.PNG") %>%
+ image_to_mosaic(32, color_palette = c('universal')) %>%
+ build_mosaic(title = "universal")
+
+gridExtra::grid.arrange(p1, p2, layout_matrix = matrix(c(1,2), ncol=2))
For grayscale or black and white mosaics, use color_palette = 'bw'. This creates the mosaic by measuring the brightness of each pixel in the image and mapping it one of the four shades of gray. Use the contrast input to adjust the relative brightness of the pixels.
png::readPNG("../Images/mf_unicorn.PNG") %>%
+ image_to_mosaic(32, color_palette = 'bw', contrast = 1.1)%>%
+ build_mosaic()
With the color_table input, it is possible to restrict the colors in the mosaic to a custom palette.
#Remove blue and azure colors from lego_colors
+lego_colors_wo_blue <- lego_colors %>%
+ dplyr::filter(!grepl("blue|azur", tolower(Color)))
+
+png::readPNG("../Images/mf_unicorn.PNG") %>%
+ image_to_mosaic(32, color_table = lego_colors_wo_blue)%>%
+ build_mosaic(title = "Mosaic without blue or azur")
brickr uses the farver package to match image colors to the subset of LEGO colors. Technical details of the different algorithms can be found on Wikipedia.
The default algorithm is ‘cie94’, though the other farver options are available using the method input. The farver ‘euclidean’ is not very accurate, so ‘brickr_classic’ returns a manually calculated version for Euclidean RGB distance matching.
c("cie94", "cie2000", "euclidean", "brickr_classic") %>%
+ purrr::map(~png::readPNG("../Images/mf_unicorn.PNG") %>%
+ image_to_mosaic(24, method =.x) %>%
+ build_mosaic(title = .x )) -> mosaics_by_method
+
+gridExtra::grid.arrange(grobs = mosaics_by_method, layout_matrix =rbind(c(1,2),c(3,4)))
When rendering a mosaic from a photographic with many shades of similar colors, using dithering by setting dithering = TRUE will help to add texture to the mosaic and avoid large areas of the same color. This works particularly well for large mosaics, but is purely a stylist preference.
c(FALSE, TRUE) %>%
+ purrr::map(~jpeg::readJPEG("../Images/goldengirls.JPG") %>%
+ image_to_mosaic(dithering = .x) %>%
+ build_mosaic(title = paste("dithering =", .x))) -> mosaics_by_dither
+
+gridExtra::grid.arrange(grobs = mosaics_by_dither, layout_matrix = matrix(c(1,2), ncol=2))
The input brightness can be used to scale up or down the RGB values of the image before color matching. The default is 1.
Use the warhol input to swap the R, G, or B channels in the original image. This can be used to create pop-art style mosaics.
png::readPNG("../Images/mf_unicorn.PNG") %>%
+ image_to_mosaic(32, brightness = 0.9, warhol = c(2, 3, 1))%>%
+ build_mosaic()
With rayshader installed, passing the mosaic object to bricks_from_mosaic() will render a 3D object, stacking layers of bricks on each other to create an elevated mosaic. By default, the lightest color bricks will be on top, but this can be changed using the highest_el = 'dark' option.
png::readPNG("../Images/mf_unicorn.PNG") %>%
+ image_to_mosaic(32) %>%
+ bricks_from_mosaic(highest_el = "dark") %>%
+ build_bricks(brick_type = "plate")
+
+rayshader::render_camera(theta = 15)
+rayshader::render_snapshot(clear = TRUE)
brickr is a package for bringing the LEGO® experience into the R and tidyverse ecosystem.
+The package is divided into 3 separate systems:
+brickr also includes tools help users create the Mosaics and 3D model output using real LEGO® elements.
+brickr is developed using publicly available information about LEGO® products and is not officially affiliated with The LEGO Group
+The mosaic functions renders an imported JPG or PNG file using LEGO colors and bricks.
+mosaic1 <- png::readPNG("Images/mf_unicorn.PNG") %>%
+ image_to_mosaic(img_size = 36) #Length of each side of mosaic in "bricks"
+
+#Plot 2D mosaic
+mosaic1 %>% build_mosaic()
In general, any {brickr} function that begins with build_ generates a graphical output from a {brickr} list object from other functions.
image_to_mosaic() can take a few important arguments. See ?image_to_mosaic() for full detail.
img_size Providing a single value, such as 48, crops the image to a square. Inputting a 2-element array, c(56, 48), will output a rectangular image of c(width, height).
color_table & color_palette Options to limit the color of bricks used in mosaics, as not all colors produced by LEGO are readily available. Set color_palette to ‘universal’ or c('universal', 'generic') to limit colors to the most common ones. Use a subset of the data frame lego_colors as the color_table to specify a custom palette.
method Technique used to map image colors into the allowed brick colors. Defaults to ‘cie94`, but other options include ’cie2000’ and ‘euclidean’. Also includes the option ‘brickr_classic’, used in previous version of the package.
The bricks_from_* series of functions creates 3D models of LEGO bricks from a variety of input formats. These models are rendered using Tyler Morgan-Wall’s rayshader package.
bricks_from_table() & bricks_from_excel() convert a matrix-shaped table of integers into LEGO bricks. For simple models, this table can be made manually using data.frame() or tibble::tribble(). For more advanced models, it’s recommended you use MS Excel or a .csv file. The left-most column in the table is associated with the Level or z-axis of the model. bricks_from_excel() is a wrapper function to more easily build models designed using a Microsoft Excel template. Please see this repo: brickr toybox.
bricks_from_coords() takes a data frame with x, y, & z integer values, and Color columns, where each combination of x, y, & z is a point in 3-dimensional space. Color must be an official LEGO color name from build_colors(). This format is much more flexible than bricks_from_table() and allows the programmatic development of 3D models.
-bricks_from_mosaic() converts a 2D mosaic object from an image into 3D LEGO models, respectively. bricks_from_rayshader() creates a LEGO model from the same input as rayshader::plot_3d().
Pass the output from any bricks_from_*() function to build_bricks() to see the 3D model. The brick_res option allows for higher resolution bricks in ‘hd’ or ‘uhd’, which will take longer to render.
library(brickr)
+
+#This is a brick
+brick <- data.frame(
+ Level="A",
+ X1 = rep(3,4), #The number 3 is the brickrID for 'bright red'
+ X2 = rep(3,4)
+)
+
+brick %>%
+ bricks_from_table() %>%
+ build_bricks(brick_res = "uhd")
+
+rayshader::render_snapshot()
The Level column in the input table determines the elevation of the bricks. bricks_from_table() will convert alphanumeric levels into a z coordinate.
For larger models, use tibble::tribble() to more easily visualize the model. For very large models, use a csv or Excel.
my_first_model <- tibble::tribble(
+ ~Level, ~X1, ~X2, ~X3, ~x4, ~x5, ~X6, ~x7, ~x8,
+ "A", 1, 1, 1, 0, 1, 1, 1, 1,
+ "A", 1, 0, 0, 0, 0, 0, 0, 1,
+ "A", 1, 0, 0, 0, 0, 0, 0, 1,
+ "A", 1, 1, 1, 1, 1, 1, 1, 1,
+ "B", 1, 0, 1, 0, 1, 1, 0, 1,
+ "B", 1, 0, 0, 0, 0, 0, 0, 1,
+ "B", 1, 0, 0, 0, 0, 0, 0, 1,
+ "B", 1, 0, 1, 0, 0, 1, 0, 1,
+ "C", 1, 1, 1, 1, 1, 1, 1, 1,
+ "C", 1, 0, 0, 0, 0, 0, 0, 1,
+ "C", 1, 0, 0, 0, 0, 0, 0, 1,
+ "C", 1, 1, 1, 1, 1, 1, 1, 1,
+ "D", 2, 2, 2, 2, 2, 2, 2, 2,
+ "D", 1, 0, 0, 0, 0, 0, 0, 1,
+ "D", 1, 0, 0, 0, 0, 0, 0, 1,
+ "D", 2, 2, 2, 2, 2, 2, 2, 2,
+ "E", 0, 0, 0, 0, 0, 0, 0, 0,
+ "E", 2, 2, 2, 2, 2, 2, 2, 2,
+ "E", 2, 2, 2, 2, 2, 2, 2, 2,
+ "E", 0, 0, 0, 0, 0, 0, 0, 0
+)
+
+brick_colors <- tibble::tribble(
+ ~`.value`, ~Color,
+ 1, "Bright blue",
+ 2, "Dark orange"
+)
+
+my_first_model %>%
+ bricks_from_table(brick_colors) %>%
+ build_bricks(theta = 210, brick_res = "hd")
+
+rayshader::render_snapshot()
Use bricks_from_coords() to programmatically build 3D LEGO models instead of manually drawing them in a spreadsheet or table. Here you must provide whole number coordinates for x, y, and z, along with an official LEGO color name for each point.
radius <- 4
+sphere_coords <- expand.grid(
+ x = 1:round((radius*2.5)),
+ y = 1:round((radius*2.5)),
+ z = 1:round((radius/(6/5)*2.5)) #A brick is 6/5 taller than it is wide/deep
+) %>%
+ mutate(
+ #Distance of each coordinate from center
+ dist = (((x-mean(x))^2 + (y-mean(y))^2 + (z-mean(z))^2)^(1/2)),
+ Color = case_when(
+ #Yellow stripes on the surface with a 2to4 thickness
+ between(dist, (radius-1), radius) & (x+y+z) %% 6 %in% 0:1 ~ "Bright yellow",
+ #Otherwise, sphere is blue
+ dist <= radius ~ "Bright blue"
+ ))
+
+sphere_coords %>%
+ bricks_from_coords() %>%
+ build_bricks(phi = 30, theta = 30)
+
+rayshader::render_snapshot()
More examples using bricks_from_table() and bricks_from_coords() can be found at the links below.
bricks_from_table() by manually placing each brick.bricks_from_coords() and {purrr}.brickr includes some elements for a ggplot2 extensions to convert ggplot bar charts into bricks and LEGO themes. The main function is geom_brick_col(), which is the brickr equivalent of geom_col(). Additional functions are highly recommended to ensure that proper the chart is rendered in the proper functions and proportions.
df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
+
+#For official LEGO colors, use with scale_fill_brick and theme_brick.
+ggplot(df, aes(trt, outcome)) +
+ geom_brick_col(aes(fill = trt)) +
+ scale_fill_brick() +
+ coord_brick() +
+ theme_brick()
Both scale_fill_brick() and theme_brick() take an input ‘brick_theme’, which ensures all colors match official LEGO brick colors. See build_themes() for a sample of all available brick theme.
df <- data.frame(trt = letters[1:6], outcome = rnorm(6, mean = 5, sd = 2))
+
+use_theme <- "hp"
+
+ggplot(df, aes(trt, outcome)) +
+ geom_brick_col(aes(fill = trt), two_knob = F) +
+ scale_fill_brick(use_theme) +
+ coord_brick_flip() +
+ theme_brick(use_theme) +
+ theme(legend.position = "none")
Additional functions assist in the translation of brickr objects into actual LEGO bricks.
+Use build_instructions() to break the mosaics and 3D models into easier-to-read steps for building the set. This defaults to 6 steps, but passing any integer value will generate that many steps.

Use build_pieces() to generate a graphic and count of all required plates or bricks (for stacked mosaics). These are sorted by color and size for easy purchase on LEGO.com’s Pick-a-Brick section using the advanced search option. Alternatively, use table_pieces() to produce a data frame table of all required bricks.

NEWS.md
+ Breaking: Pretty much EVERY function. Seriously, check out the README and start fresh.
Breaking: Data “lego_colors.rda” has been updated with more accurate RGB values and new brickrID numbers. This will impact previously created mosaics and 3D models.
Mosaics
+bricks_from_mosaic().3D Models
+brick_res input options to render models in higher definition (‘sd’, ‘hd’, ‘uhd’)bricks_from_rayshader() to render LEGO models from rayshader plot_3d() input.ggplot Extension
+geom_brick_col for bar charts in the shape of bricks. Negative values are fine, but position = stack is not available.coord_brick to prevent chart brick distortion. coord_brick_flip for horizontal bars.scale_fill_brick and theme_brick for different LEGO color options.bricks_from_excel() as a way to decrease the starting cost of using brickr.bricks_from_coords() function to convert a long data frame with x, y, z, and Color columns into input for display_bricks().NEWS.md file to track changes to the package.R/coord_brick.R, R/draw_key.R, R/geom-brick-col.R, and 4 more
+ brickr-ggproto.Rdggproto for brickr geoms
+ +draw_key_brick(data, params, size) + +GeomStud + +resizingTextGrob(...) + +drawDetails.resizingTextGrob(x, recording = TRUE) + +preDrawDetails.resizingTextGrob(x) + +postDrawDetails.resizingTextGrob(x)+ +
An object of class GeomStud (inherits from Geom, ggproto, gg) of length 5.
R/bricks_from.R
+ bricks_from_coords.RdConvert a data frame with x, y, & z coordinates & Color into bricks for 3D Model
+ +bricks_from_coords(coord_table, color_guide = lego_colors, + increment_level = 0, max_level = Inf, increment_x = 0, + max_x = Inf, increment_y = 0, max_y = Inf, exclude_color = NULL, + exclude_level = NULL)+ +
| coord_table | +A data frame of a 3D brick model design. Contains x, y, and z (vertical height) dimensions, as well as Color from official LEGO color names. See |
+
|---|---|
| increment_level | +Default '0'. Use in animations. Shift Level/z dimension by an integer. |
+
| max_level | +Default 'Inf'. Use in animations. Any Level/z values above this value will be cut off. |
+
| increment_x | +Default '0'. Use in animations. Shift x dimension by an integer. |
+
| max_x | +Default 'Inf'. Use in animations. Any x values above this value will be cut off. |
+
| increment_y | +Default '0'. Use in animations. Shift y dimension by an integer. |
+
| max_y | +Default 'Inf'. Use in animations. Any y values above this value will be cut off. |
+
| exclude_color | +Numeric array of color ID numbers to exclude. |
+
| exclude_level | +Numeric array of Level/z dimensions to exclude. |
+
A list with elements Img_lego to pass to collect_bricks().
R/bricks_from.R
+ bricks_from_excel.RdConvert an Excel brickr template into a 3D model. https://github.com/ryantimpe/brickr_toybox
+ +bricks_from_excel(excel_table, repeat_levels = 1, increment_level = 0, + max_level = Inf, increment_x = 0, max_x = Inf, increment_y = 0, + max_y = Inf, exclude_color = NULL, exclude_level = NULL)+ +
| excel_table | +Sheet imported from a brickr Excel template to build model. Contains stud placement and colors. |
+
|---|---|
| repeat_levels | +How many times to repeat a level. Can save time in model planning. Default is 1. |
+
| increment_level | +Default '0'. Use in animations. Shift Level/z dimension by an integer. |
+
| max_level | +Default 'Inf'. Use in animations. Any Level/z values above this value will be cut off. |
+
| increment_x | +Default '0'. Use in animations. Shift x dimension by an integer. |
+
| max_x | +Default 'Inf'. Use in animations. Any x values above this value will be cut off. |
+
| increment_y | +Default '0'. Use in animations. Shift y dimension by an integer. |
+
| max_y | +Default 'Inf'. Use in animations. Any y values above this value will be cut off. |
+
| exclude_color | +Numeric array of color ID numbers to exclude. |
+
| exclude_level | +Numeric array of Level/z dimensions to exclude. |
+
A list with elements Img_lego to pass to collect_bricks().
R/bricks-from-mosaic.R
+ bricks_from_mosaic.RdConvert image output from scale_image() to bricks
+ +bricks_from_mosaic(image_list, mosaic_height = 6, highest_el = "light")+ +
| image_list | +List output from collect_bricks() or image_to_bricks(). Contains an element |
+
|---|---|
| mosaic_height | +Number of layers in the 3D image. |
+
| highest_el | +Brick height is determined by brightness of color. Use |
+
A list with elements threed_elevation and threed_hillshade to created 3D mosiacs with the rayshader package.
R/bricks-from-rayshader.R
+ bricks_from_rayshader.RdConvert image output from scale_image() to bricks
+ +bricks_from_rayshader(hillshade, heightmap, max_height = 12, + img_size = 48)+ +
| hillshade | +Same as |
+
|---|---|
| heightmap | +Same as |
+
| max_height | +Maximum height of plot in LEGO bricks or plates. |
+
| img_size | +Size of output image in pixel, where one pixel = one 'brick'. Use a single value (e.g. |
+
A list with elements threed_elevation and threed_hillshade to created 3D mosiacs with the rayshader package.
R/bricks_from.R
+ bricks_from_table.RdConvert a data frame in 3D matrix format into bricks for 3D Model
+ +bricks_from_table(matrix_table, color_guide = lego_colors, + .re_level = TRUE, increment_level = 0, max_level = Inf, + increment_x = 0, max_x = Inf, increment_y = 0, max_y = Inf, + exclude_color = NULL, exclude_level = NULL)+ +
| matrix_table | +A data frame of a 3D brick model desigh. Left-most column is level/height/z dimension, with rows as Y axis and columns as X axis. See example. Use |
+
|---|---|
| color_guide | +A data frame linking numeric |
+
| .re_level | +Logical to reassign the Level/z dimension to layers in alphanumeric order. Set to FALSE to explicitly provide levels. |
+
| increment_level | +Default '0'. Use in animations. Shift Level/z dimension by an integer. |
+
| max_level | +Default 'Inf'. Use in animations. Any Level/z values above this value will be cut off. |
+
| increment_x | +Default '0'. Use in animations. Shift x dimension by an integer. |
+
| max_x | +Default 'Inf'. Use in animations. Any x values above this value will be cut off. |
+
| increment_y | +Default '0'. Use in animations. Shift y dimension by an integer. |
+
| max_y | +Default 'Inf'. Use in animations. Any y values above this value will be cut off. |
+
| exclude_color | +Numeric array of color ID numbers to exclude. |
+
| exclude_level | +Numeric array of Level/z dimensions to exclude. |
+
A list with elements Img_lego to pass to collect_bricks().
Build 3D brick model with rayshader.
+ +build_bricks(brick_list, brick_type = "brick", brick_res = "sd", + view_levels = NULL, solidcolor = "#a3a2a4", ...)+ +
| brick_list | +List output from collect_bricks(). Contains an element |
+
|---|---|
| brick_type | +Type of brick to use. Default is 'brick'. Other option is 'plate', which is 1/3 the height of a brick. |
+
| brick_res | +Resolution, expressed at number of pixels on one side of a 1x1 brick. Defaults to 'sd' (15px). Use 'hd' for 30px per brick, and 'uhd' for 60px. +Enter a value for a custom resolution. High resolutions take longer to render. |
+
| view_levels | +Numeric array of Levels/z values to display. Leave as |
+
| solidcolor | +Hex color of mosaic base. Only renders on bottom. |
+
| ... | +All other inputs from rayshader::plot_3d() EXCEPT |
+
3D brick model rendered in the 'rgl' package.
+ + +Generates a plot of available brick colors. Use .names_only = TRUE to get a list of color names.
+ +build_colors(.names_only = FALSE)+ +
| .names_only | +Return an array of the 41 brick color names. Does not plot. |
+
|---|
A table and ggplot of brick colors & ID numbers.
+ + ++#Generate plot of colors +build_colors()#>+#Print list of colors +build_colors(TRUE)#> [1] "White" "Brick yellow" "Bright red" +#> [4] "Bright blue" "Bright yellow" "Black" +#> [7] "Dark green" "Reddish brown" "Medium stone grey" +#> [10] "Dark stone grey" "Nougat" "Bright green" +#> [13] "Medium blue" "Bright orange" "Br. yellowish green" +#> [16] "Earth blue" "Earth green" "Dark red" +#> [19] "Bright purple" "Light purple" "Medium azur" +#> [22] "Medium lavender" "Dark orange" "Bright bluish green" +#> [25] "Bright reddish violet" "Sand blue" "Sand yellow" +#> [28] "Sand green" "Flame yellowish orange" "Light royal blue" +#> [31] "Cool yellow" "Medium lilac" "Light nougat" +#> [34] "Dark brown" "Medium nougat" "Dark azur" +#> [37] "Aqua" "Lavender" "Spring yellowish green" +#> [40] "Olive green" "Vibrant coral"
Create instruction manual for 2D image mosaics
+ +build_instructions(image_list, num_steps = 6)+ +
| image_list | +List output from collect_bricks() or image_to_bricks(). Contains an element |
+
|---|---|
| num_steps | +Number of discrete steps in instruction manual |
+
Generates a plot of available brick themes.
+ +build_themes(show_themes = "all", .names_only = FALSE)+ +
| .names_only | +Logical. Return an array of the theme names. Does not plot. |
+
|---|---|
| show_theme | +Defaults to "all". Pass an array of theme names to only plot a subset. |
+
A table and ggplot of brick colors & ID numbers.
+ + ++#Generate plot of themes +build_themes()+#Print list of themes +build_themes(.names_only = TRUE)#> [1] "classic" "city" "duplo" "elves" +#> [5] "friends" "hp" "jurassic" "movie" +#> [9] "ninja" "superhero" "sw_light" "sw_dark" +#> [13] "classy" "blue" "ducks" "ocean" +#> [17] "rainbow7" "rainbow13" "doublerainbow" "space" +#> [21] "80s"
R/collect_bricks.R
+ collect_bricks.RdCollect legoize image from individual bricks into grouped bricks.
+ +collect_bricks(image_list, use_bricks = NULL)+ +
| image_list | +List output from legoize(). Contains an element |
+
|---|---|
| use_bricks | +Array of brick sizes to use in mosaic. Defaults to |
+
A list with element Img_bricks containing a data frame of the x- & y-coordinates, R, G, B channels, and brick ID. Other helper elements.
A fixed scale coordinate system that ensures correct brick proportions are maintained regardless of device size.
+Use coord_brick_flip() for horizontal bars.
coord_brick(xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") + +coord_brick_flip(xlim = NULL, ylim = NULL, expand = TRUE, + clip = "on")+ +
| xlim | +Limits for the x and y axes. |
+
|---|---|
| ylim | +Limits for the x and y axes. |
+
| expand | +If |
+
| clip | +Should drawing be clipped to the extent of the plot panel? A
+setting of |
+
+#geom_brick_col should be used in conjunction with other brickr charting functions, especially coord_brick. +df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2)) +ggplot(df, aes(trt, outcome)) + + geom_brick_col(aes(fill = trt)) + + coord_brick()+ #horizontal bars +ggplot(df, aes(trt, outcome)) + + geom_brick_col(aes(fill = trt)) + + coord_brick_flip()+
geom_brick_col() is the brickr version of ggplot2::geom_col().
+Bar height is determined by values in the data using the y aesthetic. With the exception of fill, aesthetics available in ggplot2::geom_col() are generally not enabled here.
geom_brick_col(mapping = NULL, data = NULL, position = "dodge", + two_knob = TRUE, split_bricks = TRUE, min_radius_for_text = 0.02, + label = "brickr", label_scale = 1, ..., width = NULL, + na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)+ +
| mapping | +Set of aesthetic mappings created by |
+
|---|---|
| data | +The data to be displayed in this layer. There are three +options: +If A A |
+
| position | +It it not recommended to use |
+
| two_knob | +Logical. Each bar is two knobs / studs wide. When rendering many |
+
| split_bricks | +Logical. For simpler bars, do not split into individual bricks. Knobs still render. |
+
| min_radius_for_text | +Knob radius as a percentage of view port. If the calculated radius is lower than this value, embossed label will not appear in knobs. |
+
| label | +Character string to include as embossed text inside brick knobs. Maximum 6 characters. |
+
| label_scale | +Scale text size of label as a percentage. +Sizing for the embossed text can be off. A best attempt at the text size is calculated from the device size. Zooming a plot in the RStudio window will not update the text size. +If the automated size doesn't look correct after rendering the chart, use this scale. |
+
| ... | +Other arguments passed on to |
+
| width | +Bar width. By default, set to 90% of the resolution of the data. |
+
| na.rm | +If |
+
| show.legend | +logical. Should this layer be included in the legends?
+ |
+
| inherit.aes | +If |
+
+#geom_brick_col should be used in conjunction with other brickr charting functions, especially coord_brick. +df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2)) +ggplot(df, aes(trt, outcome)) + + geom_brick_col() + + coord_brick()+#For official LEGO colors, use with scale_fill_brick and theme_brick. +ggplot(df, aes(trt, outcome)) + + geom_brick_col(aes(fill = trt)) + + scale_fill_brick() + + coord_brick() + + theme_brick()+
geom_point, except points look like LEGO(R) studs
geom_brick_point(mapping = NULL, data = NULL, stat = "identity", + position = "identity", ..., label = "LEGO", + simplified_threshold = 24, size = 1, stud_scale = 6/5, + linejoin = "mitre", na.rm = FALSE, show.legend = NA, + inherit.aes = TRUE)+ +
| mapping | +Set of aesthetic mappings created by |
+
|---|---|
| data | +The data to be displayed in this layer. There are three +options: +If A A |
+
| stat | +The statistical transformation to use on the data for this +layer, as a string. |
+
| position | +Position adjustment, either as a string, or the result of +a call to a position adjustment function. |
+
| ... | +Other arguments passed on to |
+
| na.rm | +If |
+
| show.legend | +logical. Should this layer be included in the legends?
+ |
+
| inherit.aes | +If |
+
geom_rect, except bars look like LEGO(R) bricks.
geom_brick_rect(mapping = NULL, data = NULL, stat = "identity", + position = "identity", ..., label = "brickr", + simplified_threshold = 24 * 24, label_scale = 1, use_bricks = NULL, + linejoin = "mitre", na.rm = FALSE, show.legend = NA, + inherit.aes = TRUE)+ + +
R/image-to-mosaic.R
+ image_to_mosaic.RdConvert image raster array to a LEGO-esque mosaic. Wrapper function.
+ +image_to_mosaic(img, img_size = 48, color_table = NULL, + method = "cie94", color_palette = c("universal", "generic", + "special"), dithering = FALSE, contrast = 1, use_bricks = NULL, + brightness = 1, warhol = 1:3)+ +
| img | +Image to convert into mosaic. Usually from |
+
|---|---|
| img_size | +Size of output image in pixel, where one pixel = one 'brick'. Use a single value (e.g. |
+
| color_table | +Defaults to |
+
| method | +The method to use for comparison. Either 'brickr_classic', 'euclidean', 'cie1976', 'cie94', 'cie2000', or 'cmc'.
+'brickr_classic' is an explicit euclidean distance formula, but yield different results than 'euclidean' in farver.
+See |
+
| color_palette | +Brick color rarity to use. Defaults to all colors: 'universal' (most common), 'generic', and 'special' (least common). This is useful when trying to build the mosaic out of real bricks.
+Use "bw" for only grayscale bricks. Ignored if a |
+
| dithering | +Improves color of large, photo-realistic mosaics. |
+
| contrast | +For |
+
| use_bricks | +Array of brick sizes to use in mosaic. Defaults to |
+
| brightness | +A value >1 will increase the brightness of the image while a positive value <1 will decrease the brightness. |
+
| warhol | +Array of values |
+
A list with element Img_lego containing a data frame of the x- & y-coordinates, R, G, B channels, and mapped color of each brick (pixel).
R/image-to-prep-mosaic.R
+ image_to_scaled.RdScale an image raster array to a small number of pixels. Process into a data frame.
+ +| image | +A raster array from an image. |
+
|---|---|
| img_size | +Size of output image in pixel, where one pixel = one 'brick'. Use a single value (e.g. |
+
| brightness | +A value >1 will increase the brightness of the image while a positive value <1 will decrease the brightness. |
+
| warhol | +Array of values |
+
A list with element Img_scaled containing a data frame of the x- & y-coordinates, R, G, B channels, and hex color of each brick (pixel).
+ All functions+ + |
+ |
|---|---|
|
+
|
+ GeomBrick |
+
| + + | +Convert a data frame with x, y, & z coordinates & Color into bricks for 3D Model |
+
| + + | +Convert an Excel brickr template into a 3D model. https://github.com/ryantimpe/brickr_toybox |
+
| + + | +Convert image output from scale_image() to bricks |
+
| + + | +Convert image output from scale_image() to bricks |
+
| + + | +Convert a data frame in 3D matrix format into bricks for 3D Model |
+
| + + | +Build 3D brick model with rayshader. |
+
| + + | +Available brick colors |
+
| + + | +Create instruction manual for 2D image mosaics |
+
| + + | +Display 2D mosaic output as a plot image |
+
| + + | +Graphically display required bricks. |
+
| + + | +Available brick themes for scale_fill_brick() |
+
| + + | +Collect legoize image from individual bricks into grouped bricks. |
+
| + + | +Cartesian coordinates for bricks |
+
| + + | +Bar charts with bricks |
+
| + + | +ggplot2 Points, as Bricks |
+
| + + | +ggplot2 Bar Charts as Bricks |
+
| + + | +Convert image raster array to a LEGO-esque mosaic. Wrapper function. |
+
| + + | +Scale an image raster array to a small number of pixels. Process into a data frame. |
+
| + + | +Helper function to convert a level from a 3D model into a rayshader-friendly object. |
+
| + + | +Convert image output from scale_image() to bricks |
+
| + + | +Color scales for bricks |
+
| + + | +Generate required bricks as a data frame. |
+
| + + | +Brick color themes |
+
R/build-bricks.R
+ layer_from_bricks.RdHelper function to convert a level from a 3D model into a rayshader-friendly object.
+ +layer_from_bricks(brick_list, brick_type = "brick", lev = 1, + brick_res = "sd")+ +
| brick_list | +List output from table_to_bricks(). Contains an element |
+
|---|---|
| brick_type | +Type of brick to use. Default is 'brick'. Other option is 'plate', which is 1/3 the height of a brick. |
+
| lev | +z-level of 3D model |
+
| brick_res | +Resolution, expressed at number of pixels on one side of a 1x1 brick. Defaults to 'sd' (15px). Use 'hd' for 30px per brick, and 'uhd' for 60px. +Enter a value for a custom resolution. High resolutions take longer to render. |
+
A list with elements threed_elevation and threed_hillshade to created 3D mosiacs with the rayshader package.
See magrittr::%>% for details.
lhs %>% rhs+ + +
brickr counterpart to ggplot2::scale_fill_discrete() to map bar colors to the palette of LEGO mold colors.
scale_fill_brick(brick_theme = "classic", ...)+ +
| brick_theme | +Color palette for bricks. Same as |
+
|---|---|
| ... | +Arguments passed on to
|
+
+df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2)) +p <- ggplot(df, aes(trt, outcome)) + + geom_brick_col(aes(fill = trt)) + + coord_brick() + +p + scale_fill_brick()+#Select a brick_theme and use with theme_brick() +tm <- "hp" +p + + scale_fill_brick(tm) + + theme_brick(tm)
R/image-to-prep-mosaic.R
+ scaled_to_colors.RdConvert image output from scale_image() to bricks
+ +| image_list | +List output from scale_image(). Contains an element |
+
|---|---|
| method | +The method to use for comparison. Either 'brickr_classic', 'euclidean', 'cie1976', 'cie94', 'cie2000', or 'cmc'.
+'brickr_classic' is an explicit euclidean distance formula, but yield different results than 'euclidean' in farver.
+See |
+
| color_table | +Defaults to |
+
| color_palette | +Brick color rarity to use. Defaults to all colors: 'universal' (most common), 'generic', and 'special' (least common). This is useful when trying to build the mosaic out of real bricks.
+Use "bw" for only grayscale bricks. Ignored if a |
+
| dithering | +Improves color of large, photo-realistic mosaics. |
+
| contrast | +For |
+
A list with element Img_lego containing a data frame of the x- & y-coordinates, R, G, B channels, and mapped color of each brick (pixel).
Brick color themes
+ +theme_brick(brick_theme = "classic")+ + +