---
title: "Skills Lab 2"
author: "Jennifer Mankin"
date: "04/02/2022"
output: html_document
---

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

# Essential packages
while(!require(remotes)) install.packages("remotes")
library(kableExtra)

## At home
library(tidyverse)

## On uni computers
library(magrittr)
library(dplyr)
library(tidyr)

# Helper packages
while(!require(ViewPipeSteps)) install.packages("ViewPipeSteps")
while(!require(fcuk)) install.packages("fcuk")
while(!require(introverse)) remotes::install_github("spielmanlab/introverse")
```

## As Usual...

- Load required packages in the `setup` code chunk
- Read in the data

URL: https://and.netlify.app/datasets/gensex_2022.csv
Codebook: https://and.netlify.app/docs/gensex_codebook.html

```{r}
gensex <- readr::read_csv("https://and.netlify.app/datasets/gensex_2022.csv")
```

## Quick Review: Tidyverse and Pipe

For Challenge 1, see Skills Lab 1!

### Challenge 2

- Only keep participants aged 19 to 24
- Only keep age, gender, and romantic orientation ratings
- For each person, calculate the the mean rating across the three romantic orientation ratings
- Then, for each gender group, calculate the mean of those mean ratings

```{r}
gensex %>% 
  # filter by age
  dplyr::filter(age > 18 & age < 25) %>% 
  # select variables; see ?select() for more on starts_with()
  dplyr::select(age, gender, starts_with("romantic")) %>% 
  # perform the following calculations for each row!
  # run vignette("rowwise") in the console for more
  dplyr::rowwise() %>% 
  # calculate the mean of the three romantic ratings
  dplyr::mutate(mean_romantic = mean(c(romantic_strength, romantic_freq, romantic_gender))) %>% 
  # split up the following calculations by gender category
  dplyr::group_by(gender) %>% 
  # calculate the mean of mean romantic ratings
  dplyr::summarise(
    mean_romantic_mean = mean(mean_romantic, na.rm = T)
  )
```

## Tables with Kable

*Remember*: Add `library(kableExtra)` to your `setup` code chunk!

```{r}
gensex %>% 
  dplyr::group_by(gender) %>% 
  dplyr::summarise(
    mean_attr = mean(romantic_freq),
    sd_attr = sd(romantic_freq),
    number_of_people_in_each_group = dplyr::n()
  ) %>% 
  kableExtra::kbl(
    col.names = c("Gender", "*M*", "*SD*", "*N*"),
    digits = 2,
    caption = "Summary of Ratings of Frequency of Romantic Attraction",
    align = "c"
  ) %>% 
  kableExtra::kable_styling()
```

### Want More Options?

The ultimate guide to [Awesome Tables with `kable`](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html) is a crucial resource!

## Data Visualisation

This is a **absolutely enormous topic**! We will cover the very basics today and look at more detail in the coming weeks.

### Basic Histogram

```{r}
gensex %>% 
  ggplot2::ggplot(aes(x = romantic_strength)) +
    geom_histogram()
```

#### Better Histograms

```{r}
#Add labels
gensex %>% 
  ggplot2::ggplot(aes(x = romantic_strength)) +
  geom_histogram() +
  labs(x = "Rating of Strength of Romantic Attraction",
       y = "Frequency")

# Add a theme
gensex %>% 
  ggplot2::ggplot(aes(x = romantic_strength)) +
  geom_histogram() +
  labs(x = "Rating of Strength of Romantic Attraction",
       y = "Frequency") +
  theme_classic()

#Change colours
gensex %>% 
  ggplot2::ggplot(aes(x = romantic_strength)) +
    geom_histogram(fill = "purple", colour = "seagreen") +
  labs(x = "Rating of Strength of Romantic Attraction",
       y = "Frequency") +
  theme_classic() 

#Using hex codes
gensex %>% 
  ggplot2::ggplot(aes(x = romantic_strength)) +
    geom_histogram(fill = "#195958") +
  labs(x = "Rating of Strength of Romantic Attraction",
       y = "Frequency") +
  scale_x_continuous(breaks = c(1:9)) +
  theme_classic()
```

### Basic Scatterplot

```{r}
gensex %>% 
  ggplot2::ggplot(aes(x = romantic_strength, y = romantic_freq)) +
  geom_point(position = "jitter")
```

### Better Scatterplot

```{r}
# Add labels, format the scales, add a theme
gensex %>% 
  ggplot2::ggplot(aes(x = romantic_strength, y = romantic_freq)) +
  geom_point(position = "jitter") +
  labs(x = "Strength of Romantic Attraction",
       y = "Frequency of Romantic Attraction") +
  scale_x_continuous(breaks = c(1:9), limits = c(1, 9)) +
  scale_y_continuous(breaks = c(1:9)) +
  theme_classic()

#Colour-coding by a category (here, gender)
gensex %>% 
  ggplot2::ggplot(aes(x = romantic_strength, 
                      y = romantic_freq,
                      colour = gender)) +
  geom_point(position = "jitter") +
  viridis::scale_color_viridis(discrete = T) +
  labs(x = "Strength of Romantic Attraction",
       y = "Frequency of Romantic Attraction") +
  scale_x_continuous(breaks = c(1:9), limits = c(1, 9)) +
  scale_y_continuous(breaks = c(1:9)) +
  scale_color_manual(values = c("Female" = "blue",
                                "Male" = "orange",
                                "Other" = "yellow",
                                "NA" = "grey")) +
  theme_classic() 
```

