Brickset.Rmd
library(LegoR)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
https://brickset.com/ contains data on historical lego sets as well as current sets. Unlike Lego.com, we can access Brickset data using an API (application programming interface). This does require registering for a brickset account and requesting an API key. All functions for the brickset.com data start with the brickset_
prefix.
Once you have your credentials, you can save them to your Rprofile using brickset_save_credentials()
. This will also save the credentials as global variables.
Then, you can access brickset’s data by authenticating. You may have to periodically reauthenticate depending on your internet configuration, but most functions should refresh the authentication automatically.
As with the Lego store, sets on brickset are organized by theme.
We can see what themes existed at the beginning…
# Oldest themes
arrange(themes, yearfrom)
#> # A tibble: 140 x 5
#> theme setcount subthemecount yearfrom yearto
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 System 433 10 1949 1970
#> 2 Promotional 441 16 1958 2019
#> 3 Dacta 165 10 1960 2003
#> 4 Samsonite 269 7 1961 1979
#> 5 Books 295 36 1966 2020
#> 6 Trains 206 7 1966 2006
#> 7 Duplo 1198 44 1969 2019
#> 8 LEGOLAND 162 6 1969 1978
#> 9 Universal Building Set 43 3 1969 1987
#> 10 Minitalia 21 0 1970 1977
#> # … with 130 more rows
Or the themes that have been around the longest…
# Longest running themes
arrange(themes, desc(yearto - yearfrom)) %>%
head(10)
#> # A tibble: 10 x 5
#> theme setcount subthemecount yearfrom yearto
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Promotional 441 16 1958 2019
#> 2 Books 295 36 1966 2020
#> 3 Duplo 1198 44 1969 2019
#> 4 Gear 2196 134 1971 2019
#> 5 Dacta 165 10 1960 2003
#> 6 Technic 419 11 1977 2019
#> 7 Trains 206 7 1966 2006
#> 8 Castle 281 23 1978 2016
#> 9 Space 322 21 1978 2015
#> 10 Basic 410 5 1972 2003
Most of the functions described in the API documentation have been wrapped; the exception is functions which concern a user’s personal collection.
Some themes have sub-themes:
# Themes with sub-themes
filter(themes, subthemecount > 0) %>%
arrange(desc(subthemecount))
#> # A tibble: 90 x 5
#> theme setcount subthemecount yearfrom yearto
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Gear 2196 134 1971 2019
#> 2 Duplo 1198 44 1969 2019
#> 3 Bionicle 389 38 2001 2016
#> 4 Books 295 36 1966 2020
#> 5 Star Wars 728 35 1999 2020
#> 6 Town 647 35 1978 2004
#> 7 Collectable Minifigures 614 32 2010 2019
#> 8 City 636 30 2005 2020
#> 9 Ninjago 328 28 2011 2020
#> 10 Castle 281 23 1978 2016
#> # … with 80 more rows
(brickset_get_subthemes("Duplo"))
#> # A tibble: 45 x 5
#> theme subtheme setcount yearfrom yearto
#> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 Duplo {None} 776 1972 2019
#> 2 Duplo Accessories 1 2012 2012
#> 3 Duplo Airport 5 2005 2006
#> 4 Duplo Baby 21 1983 1989
#> 5 Duplo Basic Set 5 1969 1998
#> 6 Duplo Batman 2 2015 2017
#> 7 Duplo Bob the Builder 25 2001 2009
#> 8 Duplo Bonus/Value Pack 6 1985 2009
#> 9 Duplo Cars 14 2010 2015
#> 10 Duplo Cars 3 3 2017 2017
#> # … with 35 more rows
Not all themes are available in all years:
(brickset_get_years("Pirates"))
#> # A tibble: 13 x 3
#> theme year setcount
#> <chr> <dbl> <dbl>
#> 1 Pirates 1989 12
#> 2 Pirates 1991 5
#> 3 Pirates 1992 9
#> 4 Pirates 1993 6
#> 5 Pirates 1994 13
#> 6 Pirates 1995 4
#> 7 Pirates 1996 8
#> 8 Pirates 1997 4
#> 9 Pirates 2001 3
#> 10 Pirates 2002 1
#> 11 Pirates 2009 11
#> 12 Pirates 2013 1
#> 13 Pirates 2015 7
We can get sets as well, searching using the set search api: https://brickset.com/api/v2.asmx?op=getSets
brickset_auth() # At this point I had to reauthenticate
#> [1] TRUE
sets_2015 <- brickset_get_sets(year = 2015)
sets_2015
#> # A tibble: 200 x 47
#> setid number numbervariant name year theme themegroup subtheme pieces
#> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl>
#> 1 24063 10246 1 Dete… 2015 Crea… Model mak… Modular… 2262
#> 2 24153 10247 1 Ferr… 2015 Crea… Model mak… Fairgro… 2464
#> 3 24154 10248 1 Ferr… 2015 Crea… Model mak… Vehicles 1158
#> 4 24156 10249 1 Wint… 2015 Crea… Model mak… Winter … 898
#> 5 24076 10581 1 Ducks 2015 Duplo Pre-school Forest … 13
#> 6 24077 10582 1 Anim… 2015 Duplo Pre-school Forest … 39
#> 7 24078 10583 1 Fish… 2015 Duplo Pre-school Forest … 32
#> 8 24079 10584 1 Fore… 2015 Duplo Pre-school Forest … 105
#> 9 23989 10585 1 Mom … 2015 Duplo Pre-school "" 13
#> 10 23990 10586 1 Ice … 2015 Duplo Pre-school "" 11
#> # … with 190 more rows, and 38 more variables: minifigs <dbl>,
#> # image <chr>, imagefilename <chr>, thumbnailurl <chr>,
#> # largethumbnailurl <chr>, imageurl <chr>, brickseturl <chr>,
#> # released <chr>, owned <chr>, wanted <chr>, qtyowned <dbl>,
#> # usernotes <chr>, acmdatacount <dbl>, ownedbytotal <dbl>,
#> # wantedbytotal <dbl>, ukretailprice <dbl>, usretailprice <dbl>,
#> # caretailprice <dbl>, euretailprice <dbl>, usdateaddedtosah <chr>,
#> # usdateremovedfromsah <chr>, rating <dbl>, reviewcount <dbl>,
#> # packagingtype <chr>, availability <chr>, instructionscount <dbl>,
#> # additionalimagecount <chr>, agemin <dbl>, agemax <chr>, height <chr>,
#> # width <chr>, depth <chr>, weight <chr>, category <chr>,
#> # userrating <dbl>, ean <chr>, upc <chr>, lastupdated <chr>
Note that the setNumber
parameter is not equivalent to the lego set number, it is an internal Brickset number.
(brickset_get_sets(setNumber = 71040))
#> # A tibble: 0 x 0
(disney_castle <- brickset_get_sets(query = 71040))
#> # A tibble: 1 x 47
#> setid number numbervariant name year theme themegroup subtheme pieces
#> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl>
#> 1 25848 71040 1 Disn… 2016 Disn… Licensed Disney … 4080
#> # … with 38 more variables: minifigs <dbl>, image <chr>,
#> # imagefilename <chr>, thumbnailurl <chr>, largethumbnailurl <chr>,
#> # imageurl <chr>, brickseturl <chr>, released <chr>, owned <chr>,
#> # wanted <chr>, qtyowned <dbl>, usernotes <chr>, acmdatacount <dbl>,
#> # ownedbytotal <dbl>, wantedbytotal <dbl>, ukretailprice <dbl>,
#> # usretailprice <dbl>, caretailprice <dbl>, euretailprice <dbl>,
#> # usdateaddedtosah <chr>, usdateremovedfromsah <chr>, rating <dbl>,
#> # reviewcount <dbl>, packagingtype <chr>, availability <chr>,
#> # instructionscount <dbl>, additionalimagecount <chr>, agemin <dbl>,
#> # agemax <chr>, height <chr>, width <chr>, depth <chr>, weight <chr>,
#> # category <chr>, userrating <dbl>, ean <chr>, upc <chr>,
#> # lastupdated <chr>
We can also get reviews and instructions for a set:
The first link is here and shows the full instruction set for building a disney castle.
disney_castle_reviews <- brickset_get_reviews(disney_castle$setid)
disney_castle_reviews %>%
gather(key = "ratingType", value = "ratingValue", overallrating:valueformoney) %>%
mutate(ratingValue = factor(ratingValue)) %>%
ggplot(aes(x = ratingType, fill = ratingValue)) +
geom_bar(position = "stack") +
scale_fill_brewer(palette = "BuGn")