class: center, middle, inverse, title-slide # Layers of plots ### Heike Hofmann --- class: middle, inverse, center # Layers in `ggplot2` --- # Outline - grammar of graphics, again - layer specifications --- # Grammar of Graphics A graphical representation (plot) consists of: 1. **mappings** (`aes`): data variables are mapped to graphical elements 2. **layers**: geometric elements (`geoms`, such as points, lines, rectangles, text, ...) and statistical transformations (`stats`, are identity, counts, bins, ...) 3. **scales**: map values in the data space to values in an aesthetic space (e.g. color, size, shape, but also position) 4. **coordinate system** (`coord`): normally Cartesian, but pie charts use e.g. polar coordinates 5. **facetting**: for small multiples (subsets) and their arrangement 6. **theme**: fine-tune display items, such as font and its size, color of background, margins, ... --- # Layers each layer has several parts, the two most important are: - mapping: using the `aes()` function, we specify mappings between variables and aesthetics of the chart - `data`: the dataset used in the layer `ggplot` is the layer that sets the defaults, <br> `geom_XXX` creates a layer: for each aspect it either uses the default or has to specify its own --- # Wallabies ```r wallabies <- read.table("http://www.statsci.org/data/oz/wallaby.txt", sep="\t", header=TRUE) head(wallabies) ``` ``` ## Anim Sex Loca Leng Head Ear Arm Leg Pes Tail Weight Age ## 1 45 1 G NA 123 NA 59 69 59 93 NA 14 ## 2 45 1 G NA 178 54 90 120 92 185 NA 28 ## 3 45 1 G NA 250 92 130 210 142 307 NA 49 ## 4 45 1 G NA 324 108 174 284 205 454 290 69 ## 5 45 1 G NA 369 129 198 340 257 568 410 83 ## 6 45 1 G NA 408 155 237 411 308 648 570 97 ``` ```r wallabies <- wallabies %>% mutate( Anim = as.factor(Anim), Sex = as.factor(Sex) ) ``` --- # Plot ```r wallabies %>% ggplot(aes(x = Age, y = Weight, colour=Sex)) + geom_point() ``` ``` ## Warning: Removed 260 rows containing missing values (geom_point). ``` ![](03_layers_files/figure-html/unnamed-chunk-2-1.png)<!-- --> --- # Adding additional text ```r wallabies %>% ggplot(aes(x = Age, y = Weight, colour=Sex)) + geom_point() + geom_text(aes(label = Anim), hjust = 0, nudge_x = 5, data = wallabies %>% filter(Age > 450, Weight < 30000)) ``` ``` ## Warning: Removed 260 rows containing missing values (geom_point). ``` ![](03_layers_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- # Default versus Layer Specification ```r wallabies %>% ggplot(aes(x = Age, y = Weight)) + geom_point(aes(colour=Sex)) + geom_text(aes(label = Anim), hjust = 0, nudge_x = 5, data = wallabies %>% filter(Age > 450, Weight < 30000)) ``` ``` ## Warning: Removed 260 rows containing missing values (geom_point). ``` ![](03_layers_files/figure-html/unnamed-chunk-4-1.png)<!-- --> --- class: inverse # Your Turn (6 mins) - Load the `mojo` data from the package `classdata`, - Plot total gross by week that the movie is out (restrict the x-axis to a maximum of 30 weeks). - Label the three movies with the highest total gross. - Color the label of these three movies with a color of your choice.