`ggplot2` is a package built within the `tidyverse` package for creating awesome graphs!

![Artwork by @allisonhorst](https://cdn.myportfolio.com/45214904-6a61-4e23-98d6-b140f8654a40/9a306c0a-dac8-413d-ba2e-cc7fd4c4d5c8_rw_1200.png?h=c627fad145421b9034b413b0b68d03bb){width="50%"}

```{r}
#| warning: FALSE
#| message: FALSE
library(tidyverse)
```

```{r}
#Import the can_lang dataset 
can_lang <- read.csv("https://raw.githubusercontent.com/ttimbers/canlang/master/inst/extdata/can_lang.csv")
```

# Recall our Top 10 example:

This code gave a list of 10 Aboriginal Languages which have the most number of people who speak them as their mother tongue:

```{r}
ten_lang <- can_lang %>% 
  filter(category == "Aboriginal languages") %>%
  arrange(by=desc(mother_tongue)) %>%
  select(language, mother_tongue) %>%
  slice(1:10)
```

# Barplots

Suppose we wanted to display this information in a barplot instead of in a table. Let's take a look at the `ggplot` syntax:

![Credit: https://github.com/UBC-DSCI/introduction-to-datascience/](https://github.com/UBC-DSCI/introduction-to-datascience/blob/main/img/intro/ggplot_function.png?raw=true)

```{r}

```

Is there any improvements we could make to this graph?

# Labels, Colors, and Themes

```{r}
ggplot(ten_lang, aes(x = mother_tongue, y = language)) +
  geom_bar(stat = "identity") 
```

::: callout-tip
Barplots are good for displaying one categorical variable and one numeric variable. The number variable could be counts (as above) or they could be averages or totals or maximums or minimums (or many other things!)
:::

# ggplot: scatterplot with `geom_point`

::: callout-tip
Scatterplots are good for displaying the relationship between two numerical variables.
:::

The `mtcars` dataset was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973--74 models). It's available inside the `ggplot` package which is already installed.

```{r}
#load the data
data(mtcars)
```

```{r}

```

Note that you can change the color, shape (`pch` for plotting character) and size of these points!

### Quick update to the dataset

```{r}
#code to update `mtcars` dataset so that `am` is treated as a factor rather than a continuous numeric variable
mtcars <- mtcars %>% 
  mutate(am = as.factor(am))
```

# Inside `aes()` or outside `aes()`?

What is the difference between these two graphs?

```{r}
#color not in aesthetics
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point(color="red")
```

```{r}
#color in aesthetics
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point(aes(color=am))
```

::: callout-tip
-   If the thing you are trying to change (color, shape, size, etc.) depends on a variable, you should put in *inside* the aesthetics
-   If the thing you are trying to change (color, shape, size, etc.) should happen for all things, you should not put it inside the aesthetics.
:::

# Customizing Colors in Aesthetics

```{r}
# customizing color using scale_color_manual
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point(aes(color=am)) 
```

# Global vs. Local Aesthetics

Global aesthetic mappings apply to all geometries and can be defined when you initially call `ggplot()`. All the geometries added as layers will default to this mapping. Local aesthetic mappings add additional information or override the default mappings.

```{r}
#color = am as a global aethetic
ggplot(mtcars, aes(x=wt, y=mpg, color=am)) +
  geom_point()
```

```{r}
#color = am as a local aethetic
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point(aes(color=am))
```

```{r}
#overwriting color = am as a global aethetic with a local aesthetic
ggplot(mtcars, aes(x=wt, y=mpg, color=am)) +
  geom_point(color="purple")
```
