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

For this homework, you will save the `R Markdown` (e.g., `.Rmd`) homework document and save it to your `homework` project directory on your computer. Save a new copy of this file so that its name has the prefix `2023_Lastname_FirstInitial`. Full name example: `2023_cookg_EX_09.Rmd`. Feel free to work with a partner but understand that the work submitted has to be your own.

This homework utilizes examples from these modules:

- **data frame manipulation and wrangling**
- **data subsets and summaries**
- **ggplot and the grammar of graphics**
- **legends and arrangement**
- **annotation and text**


**This homework is due on Tuesday by 2:30pm.**


*Enter your name in the author YAML code above*   


```{r}
source(here::here("r", "my_functions.R"))
```

# (1pt) **Load only used libraries**

```{r echo=TRUE}

```

**Load data**

Some questions will utility the data below. 

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

# (1pt) **Class team plot**

Include the code for the plot that you modified during class time or create a new plot based on your team data. Read the data so that the plot will render.

```{r}

```

**Fit/Smooth Example**

When we examined associations, we fit linear and Loess lines to the data using `geom_smooth()`.

```{r}
SWIM |>
  filter(Team != "Mixed") |>
  filter(Event == "Freestyle") |>
  filter(!is.na(Split50)) |>
  group_by(Team, Distance) |>
  mutate(Time_Min = min(Time, na.rm = T),
         Time_Min = max(Time, na.rm = T),
         Time_Mean = mean(Time, na.rm = T)
         ) |>
  ungroup() |>
  ggplot(mapping = aes(x = Distance,
                       y = Time
                       )) +
  geom_point(mapping = aes(col = Team),
             alpha = 5, 
             position = position_jitter(height = 0)
             ) +
  geom_smooth(mapping = aes(col = Team), 
              method = "lm",
              se = F
              )
```

Adding the color aesthetic added a legend to the plot. But we don't need a legend. We can direct label and replace the legend. To illustrate, we will use `geomtextpath::geom_textsmooth()` to add some labeling the to fit line. We can map some aesthetics to variables and set others to control the plot elements. Feel free to adjust other aesthetics for your plot but if you map them to variables, make sure they are in `aes()`.

```{r}
SWIM |>
  filter(Team != "Mixed") |>
  filter(Event == "Freestyle") |>
  filter(Distance < 500) |>
  filter(!is.na(Split50)) |>
  ggplot(mapping = aes(x = Distance,
                       y = Time
                       )) +
  geom_point(mapping = aes(col = Team),
             alpha = .3, 
             position = position_jitter(height = 0, width = 5)
             ) +
  geomtextpath::geom_textsmooth(
    mapping = aes(label = Team,
                  col = Team
                  ),
    method = "lm", 
    alpha = .7,   # line and text alpha
    size = 4,     # line and text size
    hjust = 1,    # 0 through 1 to move along the line (feel free to test)
    #vjust = 0, # 0 = flush with the line, values < 0 will move up and > 0 down by varying degrees (feel free to test)   
    linetype = "dotted",  # type of line to draw
    fontface = "bold",    # face font of text
    linewidth = 1,        # line thickeness
    fullrange = T         # the range for the fill, extend beyond the data
    ) +
  scale_colour_manual(values = c("Men" = "mediumblue" , "Women" = "red"))
```  

# (1pt) **Labeling Loess smooth**

In the code block below, modify the code from above to label a loess fit.

```{r}

```

Note: In this example, longer distances were removed. Clearly fitting these data and visualized the fits is misleading because there are only two distances. This variable is pretty much just categorical and connecting two points will result in a line. But the process for using `geomtextpath::geom_textsmooth(()` would be the same with a full data set.

A Continuous example:

```{r}
SWIM |>
  filter(Team != "Mixed") |>
  filter(Event == "Freestyle") |>
  filter(Distance < 500) |>
  filter(!is.na(Split50)) |>
  ggplot(mapping = aes(x = Split50,
                       y = Time,
                       col = Team,
                       fill = factor(Distance),
                       shape = factor(Distance)
                       )) +
  geom_point(alpha = .5,
             size = 3,
             position = position_jitter(height = 0)
             ) +
  geomtextpath::geom_textsmooth(mapping = aes(label = Team, 
                                              hjust = Team, # adjustment by mapping
                                              vjust = Team  # adjustment by mapping
                                              ),
                  method = "lm",
                  alpha = 1,
                  size = 4, 
                  linetype = "dotted", 
                  fontface = "bold", 
                  linewidth = 1,
                  fullrange = T
                  ) +
  scale_colour_manual(values = c("Men" = "mediumblue" , "Women" = "red"))
```


# (1pt) **Remove the legend**

Using the code above, create the same plot but remove the legend because direct labeling makes it unnecessary to include.

```{r}

```


# (1pt) **More direct labeling**

Remember that if you are passing aesthetics that are based on variables, you need to pass them within `aes()`. However, because aesthetics used so far have not been text, we have not needed to contatenate strings within an `aes()`. 

Remember that `paste()` can be used to concatenate strings. Each element of `paste()` will be separated by a comma and the `sep` will specify how those elements are separated. For example, `paste("Hello", "There!", sep = " + ")` will evaluate as `r paste("Hello", "There!", sep = " + ")`. 

A direct label that includes the level labels for two variables, you will want to combine them using `paste()` as illustrated above except that you will want to pass variable names rather than strings. 

Make the labels include both the team and the event distance. Remove the legends.

```{r}

```


# (1pt) **Add annotation**

Assume that you cannot get good placement of the labels for the lines using `geom_textpath()`. Whatever you do, the labels overlap or with you try to adjust their positioning, they look awkward. That function is not provide a good solution. Use `annotate()` to place the team and distance information directly on the plot at xy coordinates in a way that facilitate understanding the data. Although you can pass coordinates as vectors, an easy approach would be to use `annotate()` for each different annotation. Make sure the annotation is colored appropriately. 

```{r}

```

# (1pt) **Adjusting vertical and horizontal justification**

Test out different uses of `hjust` and `vjust` within `geomtextpath::geom_textsmooth()` (adjusts the same for all geoms though)

```{r}

```


# (1pt) **Annotation using {geomtextpath} with other geoms**

Some of you are using `geom_hline()`, `geom_vline()`,  `geom_segment()`, or `geom_rectangle()`. You may also be annotating those geoms. Because **{geomtextpath}** combines the geom and the annotation steps, you may want to try it for your plots.

Here is an example using `geomtextpath::geom_textvline()` to annotate the average split times.

```{r}
SWIM |>
  filter(Team != "Mixed") |>
  filter(Event == "Freestyle") |>
  filter(Distance < 200) |>
  filter(!is.na(Split50)) |>
  group_by(Team, Distance) |>
  mutate(Split_Mean = mean(Split50, na.rm = T)) |>
  ungroup() |> 
  ggplot(mapping = aes(x = Split50,
                       y = Time,
                       col = Team,
                       fill = factor(Distance),
                       shape = factor(Distance)
                       )) +
  geom_point(alpha = .5,
             size = 3,
             position = position_jitter(height = 0),
             stroke = 1
             ) +
  geomtextpath::geom_textvline(mapping = aes(xintercept = Split_Mean,
                                             label = Split_Mean,
                                             col = Team,
                                             
                                             ),
                               hjust = .5
                                
                               )
```

The above example is messy. Values are there but they are not clearly communicated.


# (1pt) **Annotation using {geomtextpath} with other geoms**

Use `geomtextpath::geom_texthline()` to annotate some metric along the y-axis instead of the example annotating the split times on the x-axis. Make sure your labeling clearly communicates what you are trying to communicate. Also, consider using `round()` to round your values so they are not messy.

```{r}

```



# (1pt) **Add a title**

Now modify your code to include a title layer. Interpret the data and add very informative and compelling title so that your audience can understand the main message of the visualization without having to figure that out for themselves. Put the title bold.

```{r}

```



# (2pt) **Add a colored title**

In the title layer, use some HTML code to add color to the title. Consider whether adding color in the title will allow you to change your annotation. If so, modify your annotation layer too. 

```{r}

```


**Please knit an `html` file and upload [here](https://claremontmckenna.app.box.com/f/140969a32dea44e9ab6c0c00146d66e3).** 

