```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Prepare the data

```{r, eval = FALSE}
# Install the package
install.packages("palmerpenguins")
```

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

```{r}
# check the data
?penguins
```

```{r}
# look at first few rows
head(penguins)
```

```{r}
View(penguins)
```

```{r}
summary(penguins$species)
```

# Scatter Plot
## Task 1: A scatter plot of flipper length and body mass for species = "Adelie"
```{r}
# prepare the subset of data
## Generation X style
pdata <- penguins[penguins$species == "Adelie", ]

## Generation Z style
pdata <- penguins %>% filter(species == "Adelie")
```

```{r}
# Quick plot using basic R
plot(x = pdata$flipper_length_mm, y = pdata$body_mass_g)
```

```{r}
# change x-axis and y-axis labels
plot(x = pdata$flipper_length_mm, y = pdata$body_mass_g, xlab = "Length", ylab = "Mass", col = 'red')
```

```{r}
# Using ggplot instead
penguins %>% 
  filter(species == "Adelie") %>%
  ggplot()
```

```{r}
# add points
penguins %>% 
  filter(species == "Adelie") %>%
  ggplot() +
  geom_point(aes(x=flipper_length_mm, y = body_mass_g))
```

```{r}
# change color of point
penguins %>% 
  filter(species == "Adelie") %>%
  ggplot() +
  geom_point(aes(x=flipper_length_mm, y = body_mass_g), color = "orchid")
```

```{r}
# change x and y labels
penguins %>%
  filter(species == "Adelie") %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g), color = "orchid") +
  xlab("Flipper length (mm)") +
  ylab("Body mass (g)") 
```

```{r}
# change x and y labels
penguins %>%
  filter(species == "Adelie") %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g), color = "orchid") +
  xlab("Flipper length (mm)") +
  ylab("Body mass (g)") +
  ggtitle("Penguin size of Adelie") +
  theme(plot.title = element_text(hjust = 0.5))
```

```{r}
# change the background of the plot
penguins %>%
  filter(species == "Adelie") %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g), color = "orchid") +
  xlab("Flipper length (mm)") +
  ylab("Body mass (g)") +
  ggtitle("Penguin size of Adelie") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_bw()
```

```{r}
# change the background of the plot
penguins %>%
  filter(species == "Adelie") %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g), color = "orchid") +
  xlab("Flipper length (mm)") +
  ylab("Body mass (g)") +
  ggtitle("Penguin size of Adelie") +
  theme_bw() + # The order of theme_bw() and theme() matters
  theme(plot.title = element_text(hjust = 0.5))
```

## Task 2: A scatter plot of flipper length and body mass for ALL species
```{r}
# Basic
# No need filter
penguins %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g))
```
```{r}
# This is bad as I dont know what specieis a point is from
penguins %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species))
```
```{r}
# You should take a moment to see now the color is inside the aes()
# Change x, y axis labels as before
penguins %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species))  +
  xlab("Flipper length (mm)") +
  ylab("Body mass (g)") +
  ggtitle("Penguin size of Adelie") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5))
```

```{r}
# Change the color manually for each group
penguins %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species))  +
  xlab("Flipper length (mm)") +
  ylab("Body mass (g)") +
  ggtitle("Penguin size of Adelie") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_color_manual(values = c("darkorange","purple","cyan4"))
```

```{r}
# Change legend position
penguins %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species))  +
  xlab("Flipper length (mm)") +
  ylab("Body mass (g)") +
  ggtitle("Penguin size of Adelie") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  theme(legend.position = "bottom")
```

```{r}
# Change legend title
penguins %>%
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species))  +
  xlab("Flipper length (mm)") +
  ylab("Body mass (g)") +
  ggtitle("Penguin size of Adelie") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5))+
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  theme(legend.position = "bottom") + 
  labs(color = "Species")
```

# Histograms
## Task 1: Plot a histgoram of flipper length
```{r}
# Baisc 
penguins %>%
   ggplot() + 
   geom_histogram(aes(x = flipper_length_mm))
```
```{r}
# Same problem as before
# Cant distinguish between species
penguins %>%
   ggplot() + 
   geom_histogram(aes(x = flipper_length_mm, color = species))
```
```{r}
# This looks not good
penguins %>%
   ggplot() + 
   geom_histogram(aes(x = flipper_length_mm, fill = species))
```

```{r}
# Manually change color 
# Notice that now we are using fill
# so we should use scale_fill_manual() instead of scale_color_manual()
penguins %>%
   ggplot() + 
   geom_histogram(aes(x = flipper_length_mm, fill = species)) +
   scale_fill_manual(values = c("darkorange","purple","cyan4"))
```

```{r}
# Similarly if we want to change the title of the legend
penguins %>%
   ggplot() + 
   geom_histogram(aes(x = flipper_length_mm, fill = species)) +
   scale_fill_manual(values = c("darkorange","purple","cyan4")) +
   labs(fill = "Species")
```
```{r}
# Changing theme, x, y axis labels and add titles are same as before
penguins %>%
   ggplot() + 
   geom_histogram(aes(x = flipper_length_mm, fill = species)) +
   scale_fill_manual(values = c("darkorange","purple","cyan4")) +
   labs(fill = "Species") +
   theme_bw() +
   theme(legend.position = "bottom")  + 
   xlab("Flipper length (mm)") +
   ylab("Frequency") +
   ggtitle("Histogram of Penguin Flipper Lengths")
```

```{r}
# this is hard to see as the histograms are overlapping.
# one potential remedy is to use alpha, which changes the transparency of the color
penguins %>%
   ggplot() + 
   geom_histogram(aes(x = flipper_length_mm, fill = species), alpha = 0.5) +
   scale_fill_manual(values = c("darkorange","purple","cyan4")) +
   labs(fill = "Species") +
   theme_bw() +
   theme(legend.position = "bottom")  + 
   xlab("Flipper length (mm)") +
   ylab("Frequency") +
   ggtitle("Histogram of Penguin Flipper Lengths")
```
```{r}
# But this is still hard to see in this case
# Use Facet Plot 
penguins %>%
   ggplot() + 
   geom_histogram(aes(x = flipper_length_mm, fill = species), alpha = 0.5) +
   scale_fill_manual(values = c("darkorange","purple","cyan4")) +
   labs(fill = "Species") +
   theme_bw() +
   theme(legend.position = "bottom")  + 
   xlab("Flipper length (mm)") +
   ylab("Frequency") +
   ggtitle("Histogram of Penguin Flipper Lengths") + 
   facet_wrap(. ~ species)
```
```{r}
# change bins
penguins %>%
   ggplot() + 
   geom_histogram(aes(x = flipper_length_mm, fill = species), alpha = 0.5, bins = 40) +
   scale_fill_manual(values = c("darkorange","purple","cyan4")) +
   labs(fill = "Species") +
   theme_bw() +
   theme(legend.position = "bottom")  + 
   xlab("Flipper length (mm)") +
   ylab("Frequency") +
   ggtitle("Histogram of Penguin Flipper Lengths") + 
   facet_wrap(. ~ species)
```

## Task 2 Stratify it by sex
```{r}
penguins %>%
  filter(!is.na(sex)) %>%
   ggplot() +
   geom_histogram(aes(x = flipper_length_mm, fill = sex)) +
   labs(fill = "Sex") +
   theme_bw() +
   theme(legend.position = "bottom")  + 
   xlab("Flipper length (mm)") +
   ylab("Frequency") +
   ggtitle("Histogram of Penguin Flipper Lengths") + 
   facet_wrap(. ~ species)
```

```{r}
penguins %>%
  filter(!is.na(sex)) %>%
   ggplot() +
   geom_histogram(aes(x = flipper_length_mm, fill = sex)) +
   labs(fill = "Sex") +
   theme_bw() +
   theme(legend.position = "bottom")  + 
   xlab("Flipper length (mm)") +
   ylab("Frequency") +
   ggtitle("Histogram of Penguin Flipper Lengths") + 
   facet_grid(sex ~ species)
```