---
title: "Exercise 2.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) Make a scatter plot with a line of best fit

> Load the `afl_grand_finals.csv` dataset we looked at previously.
>
> Make a scatterplot of `year` vs `winner_score`.
>
> Add a LOESS smoother or line of best fit.

```{r, message = FALSE}
afl_grand_finals <- read_csv("afl_grand_finals.csv")
```


### (b) Make a line-and-dot chart

> Here is a small dataset from the Victorian Electoral Commission, showing
> the number of lower house seats won by major parties in the 2018 state
> election:

```{r}
election_data <- tribble(
                    ~party, ~seats_won,
       "Australian Greens",          3,
  "Australian Labor Party",         55,
                 "Liberal",         21,
           "The Nationals",          6,
        "Other Candidates",          3
)
```

> First make a bar chart of it. You will need to specify which column to place
> on which axis, and use `geom_col()` instead of `geom_bar()`, since the data
> is already in summarised form (one observation per bar).


> Next make a line-and-dot chart, similar to the one demonstrated in the slides
> for *Five principles of good graphics*. (What do you think `geom_segment()`
> does? Look it up in R's help system to confirm your guess.)



### (c) Extension: Improve your scatterplot

> Take the scatterplot you made earlier and improve it to a standard you would
> be comfortable sharing with others.
>
> - Add appropriate axis labels, a title, and a caption indicating what
>   your line displays.
> - Improve the x axis scale. ggplot usually provides sensible axis labels, but
>   sometimes fails. In our case, the number "2020" is cut off slightly.
>   You can control the x axis using `scale_x_continuous()`. Some parameters you
>   can experiment with include `limits = c(LOWNUMBER, HIGHNUMBER)` (where
>   LOWNUMBER and HIGHNUMBER are the ranges of the scale) and
>   `breaks = seq(LOWNUMBER, HIGHNUMBER, by = INCREMENT)` which sets where the
>   tick marks go on the axs.
>   (`breaks =` is new, but we saw `seq()` briefly back in section 1.2!)
> - Extend the y axis to include zero. (Is there any reason to do this here?
>   Some people advocate all axes going to zero, but this is a complex
>   issue that deserves consideration for each plot you make.)
> - Apply your favourite theme.
> - Change the colour of the points, smoother line, and 95% confidence interval
>   for the smoother line by giving `colour =` and `fill =` options to
>   `geom_point()` and `geom_smooth()`.
>   You can use hexadecimal colour codes (like HTML), or one of the named
>   colours here: <http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf>
>   Colour names need to go inside quotes.
> - Extension: what happens if you swap the order of `geom_point()` and
>   `geom_smooth()`, if the points are not black? Look carefully at the
>   shaded confidence region.



### (d) Extension: Improve your bar or line-and-dot plot

> Pick your favourite of the plots of the Victorian election data, and get it
> to a standard you would be happy sharing with others.
>
> Some suggestions:
>
> - Sort the parties in order of number of seats won.
>   Hint: `fct_reorder(party, seats_won, .desc = TRUE)` should feature in
>   your solution.
> - Add appropriate axis labels.
> - Add party-appropriate colours.
>   Hint: these are hexadecimal codes for the colours used by Australia's
>   major political parties: 
>     `c("#DE3533", "#0047AB", "#006644", "#10C25B", "#808080")`
>   in the order: ALP, Liberal, Nationals, Greens, Other.
>   (sourced from Wikipedia!)
> - If you add colours, where should the legend go? 
>   Sometimes `theme(legend.position = "off")` is the best place.
> - Add a title and a caption indicating the source of the data.
> - Pick your favourite theme. Think about what gridlines are needed.



---

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