L 01 IntroductionL 01 IntroductionTaehyung KimApril 2 , 2019 library ( tidyverse ) library ( ggstance ) library ( skimr ) DatasetWe ’ ll be using data from the lego package which is already in the / data subdirectory , along with many other processed datasets , as part of the zipped folder for this lab . Exercise 1 Let ’ s look at some interesting patterns in the history of LEGO ! We ’ ll be using data from the lego package located data / legosets . rda . We will work through this exercise together in class . load ( file = " data / legosets . rda ") Inspect the dataThe lego package provides a helpful dataset some interesting variables . Let ’ s take a quick look at the data . legosets ## # A tibble : 6 , 172 x 14 ## Item _ Number Name Year Theme Subtheme Pieces Minifigures Image _ URL ## < chr > < chr > < int > < chr > < chr > < int > < int > < chr > ## 1 10246 Dete ~ 2015 Adva ~ Modular ~ 2262 6 http : / / i ~## 2 10247 Ferr ~ 2015 Adva ~ Fairgro ~ 2464 10 http : / / i ~## 3 10248 Ferr ~ 2015 Adva ~ Vehicles 1158 NA http : / / i ~## 4 10249 Toy ~ 2015 Adva ~ Winter ~ 898 NA http : / / i ~## 5 10581 Ducks 2015 Duplo Forest ~ 13 1 http : / / i ~## 6 10582 Anim ~ 2015 Duplo Forest ~ 39 2 http : / / i ~## 7 10583 Fish ~ 2015 Duplo Forest ~ 32 2 http : / / i ~## 8 10584 Fore ~ 2015 Duplo Forest ~ 105 3 http : / / i ~## 9 10585 Mom ~ 2015 Duplo "" 13 2 http : / / i ~## 10 10586 Ice ~ 2015 Duplo "" 11 2 http : / / i ~## # . .. with 6 , 162 more rows , and 6 more variables : GBP _ MSRP < dbl >,## # USD _ MSRP < dbl >, CAD _ MSRP < dbl >, EUR _ MSRP < dbl >, Packaging < chr >,## # Availability < chr > The above shows us the item number of the lego sets alligned with the name , year , theme , subtheme , and number of pieces in distinctive rows and colums . The minifigures for each legoset and retail availabilites are also noted , and the lego sets are arranged by item number ( small to large ) . I believe this is an overview of the dataset in order to predict what consists of the data file and to get a quick glimpse of what the data has to offer . Number of variables and missing data are also available . library ( dplyr ) glimpse ( legosets ) ## Observations : 6 , 172 ## Variables : 14 ## $ Item _ Number < chr > " 10246 ", " 10247 ", " 10248 ", " 10249 ", " 10581 ", " 105 . ..## $ Name < chr > " Detective ' s Office ", " Ferris Wheel ", " Ferrari F 4 . ..## $ Year < int > 2015 , 2015 , 2015 , 2015 , 2015 , 2015 , 2015 , 2015 , 2 . ..## $ Theme < chr > " Advanced Models ", " Advanced Models ", " Advanced M . ..## $ Subtheme < chr > " Modular Buildings ", " Fairground ", " Vehicles ", " W . ..## $ Pieces < int > 2262 , 2464 , 1158 , 898 , 13 , 39 , 32 , 105 , 13 , 11 , 5 . ..## $ Minifigures < int > 6 , 10 , NA , NA , 1 , 2 , 2 , 3 , 2 , 2 , 3 , 1 , NA , NA , NA . ..## $ Image _ URL < chr > " http : / / images . brickset . com / sets / images / 10246 - 1 . j . ..## $ GBP _ MSRP < dbl > 132 . 99 , 149 . 99 , 69 . 99 , 59 . 99 , 9 . 99 , 16 . 99 , 19 . 99 , ...## $ USD _ MSRP < dbl > 159 . 99 , 199 . 99 , 99 . 99 , 79 . 99 , 9 . 99 , 19 . 99 , 24 . 99 , ...## $ CAD _ MSRP < dbl > 199 . 99 , 229 . 99 , 119 . 99 , NA , 12 . 99 , 24 . 99 , 29 . 99 , ...## $ EUR _ MSRP < dbl > 149 . 99 , 179 . 99 , 89 . 99 , 69 . 99 , 9 . 99 , 19 . 99 , 24 . 99 , ...## $ Packaging < chr > " Box ", " Box ", " Box ", " Box ", " Box ", " Box ", " Box ", . ..## $ Availability < chr > " Retail - limited ", " Retail - limited ", " LEGO exc . .. The glimpse function seems to be interesting , as it shows some variables of the data that I have missed through the print data chunk . It shows 14 variables in total in the console , such as Item Number , Name , Year , Theme , Subtheme , Pieces , Minifigures , Images , and Packaging , only to name a few . I believe by observing the variables , it is possible for the reader of the data to have a more organized approach to interpreting the dataset . skim _ with ( numeric = list ( hist = NULL ) , integer = list ( hist = NULL ) ) skim ( legosets ) ## Skim summary statistics ## n obs : 6172 ## n variables : 14 ## ## -- Variable type : character - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -----------------------## variable missing complete n min max empty n _ unique ## Availability 0 6172 6172 6 21 0 8 ## Image _ URL 0 6172 6172 46 58 0 6172 ## Item _ Number 0 6172 6172 1 13 0 5854 ## Name 0 6172 6172 2 73 0 5519 ## Packaging 0 6172 6172 3 21 0 14 ## Subtheme 0 6172 6172 0 32 2206 358 ## Theme 0 6172 6172 4 28 0 115 ## ## -- Variable type : integer - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -----------------------## variable missing complete n mean sd p 0 p 25 p 50 p 75 ## Minifigures 2672 3500 6172 2 . 85 2 . 72 1 1 2 4 ## Pieces 112 6060 6172 215 . 17 356 . 2 0 30 82 256 . 25 ## Year 0 6172 6172 2004 . 71 8 . 91 1971 2000 2006 2012 ## p 100 ## 32 ## 5922 ## 2015 ## ## -- Variable type : numeric - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -----------------------## variable missing complete n mean sd p 0 p 25 p 50 p 75 p 100 ## CAD _ MSRP 4190 1982 6172 46 . 34 58 . 46 2 . 99 12 . 99 24 . 99 54 . 99 789 . 99 ## EUR _ MSRP 4399 1773 6172 35 . 98 46 . 61 0 9 . 99 19 . 99 39 . 99 699 . 99 ## GBP _ MSRP 1980 4192 6172 23 . 45 31 . 93 0 5 . 99 12 . 99 29 . 99 509 . 99 ## USD _ MSRP 355 5817 6172 27 . 9 39 . 32 0 6 14 . 99 34 . 99 789 . 99 This skim function seems to organize data into character type variables , integer type variables and numeric type variables . It seems to be different from the preceding two functions as this function distinguishes and groups the data not only into variables , but also into types of variables . Distribution of data is a key factor that is shown through this function . Price per yearFirst , let ’ s look at the average cost of LEGO sets over time . The main variable of interest here is USD _ MSRP , or the manufacturer ’ s suggested retail price in constant dollars ( i . e . not adjusted for inflation ) . library ( dplyr ) legosets %>% group _ by ( Year ) %>% summarise ( mean ( USD _ MSRP ) ) ## # A tibble : 45 x 2 ## Year ` mean ( USD _ MSRP ) `## < int > < dbl >## 1 1971 NA ## 2 1972 NA ## 3 1973 NA ## 4 1974 NA ## 5 1975 NA ## 6 1976 13 . 7 ## 7 1977 13 . 9 ## 8 1978 10 . 8 ## 9 1979 23 . 6 ## 10 1980 19 . 7 ## # . .. with 35 more rowsavg _ price _ per _ year <- legosets %>% filter ( ! is . na ( USD _ MSRP ) ) %>% group _ by ( Year ) %>% summarise ( Price = mean ( USD _ MSRP ) ) ggplot ( data = avg _ price _ per _ year , aes ( Year , Price ) ) + geom _ line ( color = "# 666666 ", size = 0 . 5 ) + geom _ point ( size = 0 . 5 ) + labs ( x = " Year ", y = " Price ( USD ) ", title = " Average price of LEGO sets ", subtitle =" Amounts are reported in current USD ", caption =" Source : LEGO ")+ theme _ minimal ( ) The prices per year for legos seems to vary across time . According to the dataset , in 1994 , the mean price per year hit a maximum of 27 . 5 and in 1982 it seems to have hit a minimum of 6 . The price of legos seem to increase , possibly due to inflation . Pieces per yearNext , let ’ s look at how the number of pieces per set has changed over time . Because Duplo sets are much smaller ( since they ’ re designed for toddlers ) , we ’ ll make a special indicator variable for them . library ( dplyr ) legosets %>% group _ by ( Year ) %>% summarise ( mean ( Pieces ) ) ## # A tibble : 45 x 2 ## Year ` mean ( Pieces ) `## < int > < dbl >## 1 1971 99 . 9 ## 2 1972 424 ## 3 1973 108 . ## 4 1974 102 . ## 5 1975 NA ## 6 1976 137 . ## 7 1977 122 . ## 8 1978 72 ## 9 1979 128 . ## 10 1980 NA ## # . .. with 35 more rowspieces _ per _ year <- legosets %>% filter ( ! is . na ( Pieces ) ) %>% filter ( Year > 1975 ) %>% # legosize pieces vs toddler size pieces mutate ( Duplo = ifelse ( Theme == " Duplo ", " Duplo ", " Lego ")) %>% group _ by ( Year , Duplo ) %>% summarise ( Pieces = mean ( Pieces ) , num _ sets = n ( ) ) ggplot ( pieces _ per _ year , mapping = aes ( Year , Pieces ) ) + geom _ line ( ) + facet _ wrap ( ~ Duplo ) + labs ( x = " Year ", y = " Number of Pieces ", title = " Average number of pieces of LEGO sets ", caption =" Source : LEGO ")+ theme _ minimal ( ) Duplo , compared to legos , seem to have a smaller amount of pieces included in them , probably because they are made for toddlers . The number of pieces used in lego sets seem to increase substantially over time , possible due to the increasing complexity in lego structures developers have created over time . LEGO set themesIn the 1990 s , LEGO began partnering with famous brands and franchises to boost its own sales . First , let ’ s see how many different “ themes ” LEGO now offers : library ( dplyr ) legosets %>% group _ by ( Theme ) %>% summarise ( n = n ( ) ) %>% arrange ( desc ( n ) ) ## # A tibble : 115 x 2 ## Theme n ## < chr > < int >## 1 Duplo 471 ## 2 Star Wars 379 ## 3 Town 314 ## 4 Collectable Minifigures 311 ## 5 City 282 ## 6 Bionicle 276 ## 7 Technic 243 ## 8 Creator 219 ## 9 Castle 206 ## 10 Racers 195 ## # . .. with 105 more rowslegosets %>% group _ by ( Subtheme ) %>% summarise ( n = n ( ) ) %>% arrange ( desc ( n ) ) ## # A tibble : 358 x 2 ## Subtheme n ## < chr > < int >## 1 "" 2206 ## 2 Episode IV - VI 83 ## 3 Monthly Mini Model Build 74 ## 4 Trains 71 ## 5 Accessories 67 ## 6 LEGO Ville 66 ## 7 Technic 63 ## 8 9 V 60 ## 9 Police 59 ## 10 Tiny Turbos 56 ## # . .. with 348 more rowslegosets %>% distinct ( Theme ) ## # A tibble : 115 x 1 ## Theme ## < chr > ## 1 Advanced Models ## 2 Duplo ## 3 Juniors ## 4 Classic ## 5 Architecture ## 6 Minecraft ## 7 Ideas ## 8 Friends ## 9 Legends of Chima ## 10 Star Wars ## # . .. with 105 more rowstheme _ counts <- legosets %>% count ( Theme , sort = TRUE ) %>% mutate ( Theme = fct _ inorder ( Theme , ordered = TRUE ) ) theme _ counts %>% filter ( n > 150 ) %>% ggplot ( mapping = aes ( y = fct _ rev ( Theme ) , x = n ) ) + geom _ barh ( stat =" identity ") + labs ( x =" Number of Sets ", y = NULL ) + theme _ minimal ( ) From the dataset above , it seems Lego has 115 themes and 358 subthemes in its category . The bar charts , ordered from longest to shortest as we move down from the top , seem to tell us that Duplo , the ‘ toddlers ’ lego ’, seem to have the most number of sets , followed by the theme of Star Wars , Town , Collectable Minifigures , City , etc .