---
title: "Exercise 1.3"
author: "Put Your Name Here"
output:
  html_document:
    toc: true
---

<!-- You can knit this document at any time to see the draft of your answer. 
     An html file will be produced in the same folder as the Rmd file. -->

<!-- When you have completed the exercise, knit the document for the final time. -->

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  echo = TRUE,
  comment = NA,
  fig.width = 7,
  fig.height = 4,
  dpi = 130
)
```

In this exercise, we will be using functions from the `tidyverse` package.
You can see we've added the chunk option `message = FALSE` to hide the version
information that `tidyverse` normally displays.

```{r packages, message = FALSE}
library(tidyverse)
```

### (a) Read in the ice core data

> The file `icecore.csv` contains CO2 concentration measurements made in
> ice cores from Antarctica over time, where time is defined as the age of
> the air in years before 2008. That is, an air age of 0 would mean 2008,
> while an air age of -10008 would be 8000 BC.
>
> Read it into a data frame called `icecore`.
>
> Hints:
>
> - You'll need to insert a code chunk and then add code inside that chunk.
> - Check back with Exercise 1.2 if you can't remember how to read a CSV file.
> - You can hide the list of columns by changing `{r}` at the start of your code chunk to `{r, message = FALSE}`.

DELETE THIS AND WRITE YOUR ANSWER HERE, ADDING AN R CODE CHUNK AS NEEDED.

### (b) Make a scatter plot

> Use ggplot to make a simple scatter plot of the `air_age_AD` and 
> `CO2_ppm` variables in the `icecore` data frame. (Think: which of these is
> more appropriate for the x axis and which is more appropriate for the y axis?)
>
> Hint: you will need to specify the data frame to plot, provide mappings from
> columns to aesthetic attributes, and add a geom layer.
>
> Look at the axis labels. Is there anything about them you don't understand
> or don't like?

DELETE THIS AND WRITE YOUR ANSWER HERE, ADDING AN R CODE CHUNK AS NEEDED.

### (c) Show the different core samples separately

> The `icecore` dataset includes samples from multiple ice cores, which are
> recorded in the `core` variable. Use an appropriate aesthetic (e.g. colour
> or shape) or display.

DELETE THIS AND WRITE YOUR ANSWER HERE, ADDING AN R CODE CHUNK AS NEEDED.

### (d) Make a line graph

> Make a copy of your code above and change it so it produces a line graph.
> Try making a line graph with and without points.

DELETE THIS AND WRITE YOUR ANSWER HERE, ADDING AN R CODE CHUNK AS NEEDED.

### (e) Histogram

> Make a histogram of the `CO2_ppm` variable.
>
> Choose an appropriate width for the bins (`binwidth = XXX`) and make sure
> the bins line up on a round number (`boundary = XXX`).

DELETE THIS AND WRITE YOUR ANSWER HERE, ADDING AN R CODE CHUNK AS NEEDED.

### (f) Histogram with facets

> Copy and paste your code from the last question, then use the `facet_wrap()`
> function to facet the data by `core`.
>
> What can you say about the distribution of CO2 concentrations? Is it the same
> in every core?

DELETE THIS AND WRITE YOUR ANSWER HERE, ADDING AN R CODE CHUNK AS NEEDED.

### (g) Box plot

> Make a box plot showing the distribution of CO2 concentration by core.
>
> Decide whether the box plots should be vertical and one horizontal.
>
> Experiment adding the `width = 0.2` option inside `geom_boxplot()`.

DELETE THIS AND WRITE YOUR ANSWER HERE, ADDING AN R CODE CHUNK AS NEEDED.

### (h) Make a bar chart

> The file `afl_grand_finals.csv` contains information on every Australian
> Football League (AFL) grand final played, from 1898 to 2019. 
>
> Read it into a data frame called `afl_grand_finals` and make a bar chart
> of the variable `winner`. (Think: should the winning team be displayed
> on the x or the y axis?)
>
> What do you make of the team "NA"? (What happens when an AFL grand final
> ends in a draw?)
>
> What order are the teams shown in?

DELETE THIS AND WRITE YOUR ANSWER HERE, ADDING AN R CODE CHUNK AS NEEDED.

### (i) Extension: scatter plot with a discrete axis

> Conventionally, a scatter plot is used to display the relationship between
> two continuous variables. Sometimes it's also appropriate to plot points
> where one axis shows a categorical variable.
>
> Make a scatter plot with `year` on the x axis and `winner` on the y axis.
>
> Add a line connecting the points.
>
> How do you think ggplot know which points to join with lines? (There is
> actually a complicated set of rules in place, which can be overridden if
> needed.)
>
> If you're starting to feel comfortable with ggplot, try using the code
> on the slides to reverse the order of categories.

DELETE THIS AND WRITE YOUR ANSWER HERE, ADDING AN R CODE CHUNK AS NEEDED.

### (j) Extension: axis labels

> If you've finished early, go back to the plots you've made and make sure they
> all have appropriate axis labels, using the `labs()` function demonstrated
> in the lecture slides.
>
> By default, ggplot uses variable names for axis labels. This is very helpful
> when you're making plots for your own consumption, but if you want to share
> your graphics with others, you would usually want to provide something more
> descriptive.


---

&copy; 2021 Statistical Consulting Centre, The University of Melbourne.
