---
title: "Homework 1"
subtitle: "Psychology 2812B FW23"
output: html_document
---

# Lab Component

## 1. Install/load the penguins dataset

In the RStudio console, install the [`palmerpenguins`](https://allisonhorst.github.io/palmerpenguins) dataset (you will only have to do this once)

```{r, eval=FALSE}
install.packages("palmerpenguins")
```

Load the `palmerpenguins` dataset into memory (to access the `penguins` dataset you will have to do this each time you start RStudio)

```{r}
library("palmerpenguins")
```

Have a look at the first few rows of the data table

```{r}
penguins
```

Also have a read through the documentation for the dataset by typing `?penguins` in RStudio (it will show the documentation in the Help panel).

## 2. Scatterplot

Make a plot of flipper length as a function of body mass. Use the `theme_bw()` theme. Make the labels on the x-axis and y-axis more human friendly than the default column names in the data tibble---use the `labs()` instruction with `x=" "` and `y=" "` arguments to specify labels for the x- and y-axes.

don't forget to load the tidyverse into memory each time you startup RStudio, using `library(tidyverse)`

```{r}
library(tidyverse)
# your code goes here

```

## 3. Colour by Species

Re-plot using different colours to code the three different pengiun species.

```{r}
# your code goes here

```


# Home Component

## 4. Smoothing lines

Re-plot and add a smoother line using `geom_smooth()`

```{r}
# your code goes here

```

## 5. Linear fit

Re-plot using a linear fit smoothing line. Hint: the `geom_smooth()` function takes a `method` argument and if you give it `method = "lm"` it will use a linear fit.


```{r}
# your code goes here

```

## 6. Facet by island

Re-plot using different facets for each island, with the facets arranged along 1 row. Hint: use the `facet_wrap()` function with `nrow = 1`.

```{r}
# your code goes here

```

## 7. Histogram

Generate a histogram of flipper lengths. Hint: there is a geometric object called `geom_histogram()`. Set the x label to `Flipper Length (mm)` (hint: there is a layer instruction for labels called `labs()`):

```{r}
# your code goes here

```

## 8. Colour by species

Replot the histogram above but specify that the fill color of the bars should be coded by species (`fill = species`).

Hint: this is an **aesthetic** mapping (a mapping of variables in the dataset onto different aesthetic qualities of the plot) and so you will have to put the `fill = species` instruction within an `aes()` function call. Where should this aesthetic mapping live? Play around with different possibilities. Can you simply add `fill = species` to an `aes()` call that lives within the call to `ggplot()`? Could you instead add a new `aes()` call within the `geom_histogram()` instruction? Decide on one solution and write a sentence explaining your choice.

Specify the `alpha` transparency of the bars to be `alpha = 0.8` so that you can see the different colors overlapping.

```{r}
# your code goes here

```

## 9. Boxplot

Here is some code that layers a box plot of flipper lengths across species together with jittered points of each penguin. Study each component and play with it to understand how it works.

```{r}
ggplot(data = penguins,
       mapping = aes(x = species, y = flipper_length_mm)) + 
  geom_boxplot(aes(color = species), width = 0.3, show.legend = FALSE) + 
  geom_jitter(aes(color = species), alpha = 0.4, show.legend = FALSE, width = 0.2) + 
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  labs(x = "",
       y = "Flipper length (mm)") + 
  theme_bw()    
```

Re-plot this figure using a violin plot instead of a boxplot. Hint: there is a `geom` object for violin plots. Set the width of the violins to be equal to `0.4`. Also add a `title` and a `caption` to the plot. Also change the `base_size` of the fonts to `16`.

Note: since we are using a random jitter for the points, the location of the points on your plot may not look exactly like they do on mine. That is ok.

```{r}
# your code goes here

```

# Help!
## how to look things up when you don't know what do to?
- check the assigned readings (the [R for Data Science](https://r4ds.hadley.nz) online book has a search box)
- check the [ggplot2 documentation](https://ggplot2.tidyverse.org), it also has a search box
- when you find the correct command, take a moment to try to understand how/why it works and how it fits with the ggplot2 concept of the [layered grammar of graphics](https://r4ds.hadley.nz/layers.html#the-layered-grammar-of-graphics)
