--- title: "cm005 Worksheet: Exploring Geometric Objects" output: html_document: keep_md: true theme: paper --- ## Preliminary Begin by loading the required packages. If you don't have these installed (or don't know whether you have them installed), you can install them by executing the following code in your console: ``` install.packages("tidyverse") install.packages("scales") install.packages("tsibble") ``` Now run this code chunk to load the packages: ```{r load packages, warning = FALSE} suppressPackageStartupMessages(library(tidyverse)) suppressPackageStartupMessages(library(gapminder)) suppressPackageStartupMessages(library(scales)) suppressPackageStartupMessages(library(tsibble)) knitr::opts_chunk$set(fig.align = "center") ``` ```{r allow errors, echo = FALSE} knitr::opts_chunk$set(error = TRUE) ``` ## Exercise 1: Bar Chart Grammar (Together) Consider the following plot. Don't concern yourself with the code at this point. ```{r, fig.width = 5, fig.height = 2} gapminder %>% filter(year == 2007) %>% mutate(continent = fct_infreq(continent)) %>% ggplot(aes(continent)) + geom_bar() + theme_bw() ``` Fill in the seven grammar components for this plot. | Grammar Component | Specification | |-----------------------|---------------| | __data__ | `gapminder` | | __aesthetic mapping__ | FILL_THIS_IN | | __geometric object__ | FILL_THIS_IN | | scale | FILL_THIS_IN | | statistical transform | FILL_THIS_IN | | coordinate system | FILL_THIS_IN | | facetting | FILL_THIS_IN | ## Exercise 2: `ggplot2` Syntax (Your Turn) The following is a tsibble (a special type of tibble containing time series data, which we'll see more of later), stored in the variable `mauna`, of CO$_2$ concentrations collected monthly at the Mauna Loa station. Execute this code to store the data in `mauna`: ```{r} (mauna <- tsibble::as_tsibble(co2) %>% rename(month = index, conc = value)) ``` ### 2(a) Produce a line chart showing the concentration over time. Specifically, the plot should have the following grammar components: | Grammar Component | Specification | |-----------------------|---------------| | __data__ | `mauna` | | __aesthetic mapping__ | x: month, y: conc | | __geometric object__ | lines | | scale | linear | | statistical transform | none | | coordinate system | rectangular | | facetting | none | Fill in the blanks to obtain the plot: ```{r, fig.width = 5, fig.height = 2} ggplot(FILL_THIS_IN, aes(FILL_THIS_IN, FILL_THIS_IN)) + FILL_THIS_IN() ``` ### 2(b) It turns out that you're allowed to specify the aesthetic mappings in a `geom` layer instead of, or in addition to, in the `ggplot()` function, with the following rules: - Aesthetics appearing in a `geom` layer apply only to that layer. - If there are conflicting aesthetics in both the `ggplot()` function and the `geom` layer, the `geom` layer takes precedence. The following code mistakenly puts the month variable on the y-axis. Fill in the `FILL_THIS_IN` so that you still obtain the same result as above. ```{r, fig.width = 5, fig.height = 2} ggplot(mauna, aes(y = month)) + geom_point(aes(FILL_THIS_IN)) ``` ### 2(c) You can store the output of the plot in a variable, too. Store the plot from 2(a) in the variable named `p`, then add a layer to `p` that adds green points to the plot. ```{r, fig.width = 5, fig.height = 2} p + FILL_THIS_IN(colour = FILL_THIS_IN) ``` ### 2(d) What's wrong with the following code? Fix it. ```{r, fig.width = 5, fig.height = 2} ggplot(gapminder) + geom_point(x = gdpPercap, y = lifeExp, alpha = 0.1) ``` ### 2(e) BONUS So you're a ggplot2 pro? Then, let's see this plot adapted to polar coordinates. Specifically: - angle is month (January through December) - radius is CO$_2$ concentration The plot should look like a spiral, or concentric circles. ```{r, fig.width = 5, fig.height = 2} FILL_THIS_IN ```