class: center, middle, inverse, title-slide .title[ # Layers of plots ] .author[ ### DS 2020 ] --- 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 --- # Previous example ```r data(nasa, package="GGally") nasa %>% filter(id=="1-1") %>% ggplot(aes(x = date, y= temperature)) + geom_point() ``` ![](03_layers-nasa_files/figure-html/unnamed-chunk-1-1.png)<!-- --> --- # Connecting the points ```r nasa %>% filter(id=="1-1") %>% ggplot(aes(x = date, y= temperature)) + geom_point() + geom_line() ``` ![](03_layers-nasa_files/figure-html/unnamed-chunk-2-1.png)<!-- --> --- # Adding a trend line ```r nasa %>% filter(id=="1-1") %>% ggplot(aes(x = date, y= temperature)) + geom_point() + geom_line() + geom_smooth(method="lm", se=FALSE) ``` ``` ## `geom_smooth()` using formula = 'y ~ x' ``` ![](03_layers-nasa_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- # Adding additional text ```r nasa %>% filter(id=="1-1") %>% ggplot(aes(x = date, y= temperature)) + geom_point() + geom_line() + geom_smooth(method="lm", se=FALSE) + geom_text(aes(label = time, x = date+weeks(4))) ``` ``` ## `geom_smooth()` using formula = 'y ~ x' ``` ![](03_layers-nasa_files/figure-html/unnamed-chunk-4-1.png)<!-- --> --- # Labelling specific text ```r nasa %>% filter(id=="1-1") %>% ggplot(aes(x = date, y= temperature)) + geom_point() + geom_line() + geom_smooth(method="lm", se=FALSE) + geom_text(aes(label = time, x = date+weeks(4)), nasa %>% filter(id=="1-1", temperature > 298)) ``` ``` ## `geom_smooth()` using formula = 'y ~ x' ``` ![](03_layers-nasa_files/figure-html/unnamed-chunk-5-1.png)<!-- --> --- # Default versus Layer Specification ```r nasa %>% filter(id=="1-1") %>% ggplot(aes(x = time, y= temperature)) + geom_point() + geom_line() + geom_text(aes(label=id, colour=id), data=nasa %>% filter(id=="1-1", time==50)) ``` ![](03_layers-nasa_files/figure-html/unnamed-chunk-6-1.png)<!-- --> --- ```r nasa %>% filter(id=="1-1") %>% ggplot(aes(x = time, y= temperature, colour=id)) + geom_point() + geom_line() + geom_text(aes(label=id), data=nasa %>% filter(id=="1-1", time==50)) ``` ![](03_layers-nasa_files/figure-html/unnamed-chunk-7-1.png)<!-- -->