```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(ggplot2)
library(magrittr)
library(dplyr)
```


## Loading Data

```{r}
SWIM <- readr::read_csv("https://github.com/slicesofdata/dataviz23/raw/main/data/swim/cleaned-2023-CMS-Invite.csv", 
                        show_col_types = F)
```

## `geom_point()`

```{r}
SWIM %>%
  ggplot(data = ., 
         mapping = aes(x = Distance, y = Time)) +
  geom_point()
```

## Another example

```{r}
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         ) %>%
  ggplot(., aes(x = Distance, y = Split50)) +
  geom_point()
```

## `Smoothing`geom_smooth()`

```{r}
SWIM %>%
  filter(., Distance < 1500) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point() +
  geom_smooth()
```


## Linear Fit

```{r}
SWIM %>%
  filter(., Distance < 1500) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point() +
  geom_smooth(method = "lm")
```


## `geom_point()` when a variable is a factor

```{r}
SWIM %>%
  filter(., Distance < 1500) %>%
  ggplot(., aes(x = factor(Distance), y = Time)) +
  geom_point()
```


## Multiclass Scatterplots

- scatterplots with more than 2 variables
- communicate relationship with a third variabl
- mapped to an aesthetic like color, size, shape


## Mapping a numeric variable to aesthetics

```
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         
         ) %>%
  ggplot(., aes(x = factor(Distance), y = Time)) +
  geom_point(mapping = aes(col = Distance))
```

## Mapping a numeric variable to aesthetics

```{r, echo=FALSE}
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         
         ) %>%
  ggplot(., aes(x = factor(Distance), y = Time)) +
  geom_point(mapping = aes(col = Distance))
```

## Mapping a categorical variable to aesthetics

```
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         
         ) %>%
  ggplot(., aes(x = factor(Distance), y = Time)) +
  geom_point(mapping = aes(col = School))
```

## Mapping a categorical variable to aesthetics

```{r, echo=FALSE}
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         
         ) %>%
  ggplot(., aes(x = factor(Distance), y = Time)) +
  geom_point(mapping = aes(col = School))
```

## Mapping a new numeric and a new categorical variable

```
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         ) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point(
    aes(size = Split50, col = Event))
```

## Mapping a new numeric and a new categorical variable

```{r, echo=FALSE}
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         ) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point(
    aes(size = Split50, col = Event))
```



## Suppress Legend

```
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         ) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point(
    aes(size = Split50, col = Event),
    show.legend = F
    )
```

## Suppress Legend

```{r, echo=FALSE}
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         ) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point(
    aes(size = Split50, col = Event),
    show.legend = F
    )
```



## Changing alpha transparency

```
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         ) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point(
    aes(size = Split50, col = Event),
    alpha = .4,
    show.legend = F
    )
```

## Changing alpha transparency

```{r, echo=FALSE}
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         ) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point(
    aes(size = Split50, col = Event),
    alpha = .4,
    show.legend = F
    )
```


## Changing point position

```
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         ) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point(
    aes(size = Split50, col = Event),
    alpha = .3,
    position = position_jitter(),
    show.legend = F
    )
```

## Changing point position

```{r, echo=FALSE}
SWIM %>%
  filter(., 
         !is.na(Name),
         Time < 1000
         ) %>%
  ggplot(., aes(x = Distance, y = Time)) +
  geom_point(
    aes(size = Split50, col = Event),
    alpha = .3,
    position = position_jitter(),
    show.legend = F
    )
```
