---
title: "Learning Basic Functions of the `tidyverse` Package"
author: "Valerio Licursi"
date: "`r Sys.Date()`"
format: html
---

# Introduction

By the end of this exercise, you will be familiar with basic data manipulation and visualization functions provided by the `tidyverse` package in R. This includes functions from `dplyr` and `ggplot2` for data wrangling and visualization.

## Prerequisites

-   `tidyverse` package installed (`install.packages("tidyverse")`)

# Step 1: Loading Libraries and Dataset

```{r load-libraries}
library(tidyverse)
data(mtcars)
```

# Step 2: Data Manipulation with `dplyr`

## 3. Select Specific Columns

Use the `select` function to choose columns `mpg`, `cyl`, and `hp` from `mtcars`.

```{r select-columns}
mtcars_selected <- mtcars %>% select(mpg, cyl, hp)
head(mtcars_selected)
```

## 4. Filter Rows

Use the `filter` function to select cars with more than 6 cylinders.

```{r filter-rows}
mtcars_filtered <- mtcars_selected %>% filter(cyl > 6)
head(mtcars_filtered)
```

## 5. Create a New Column

Use the `mutate` function to create a new column `hp_per_cyl` which is the horsepower divided by the number of cylinders.

```{r mutate-column}
mtcars_mutated <- mtcars_filtered %>% mutate(hp_per_cyl = hp / cyl)
head(mtcars_mutated)
```

## 6. Summarize Data

Use the `summarize` function to calculate the average `mpg` and `hp_per_cyl`.

```{r summarize-data}
mtcars_summary <- mtcars_mutated %>% summarize(avg_mpg = mean(mpg), avg_hp_per_cyl = mean(hp_per_cyl))
mtcars_summary
```

## 7. Group by a Column

Use the `group_by` and `summarize` functions to get the average `mpg` for each `cyl`.

```{r group-by}
mtcars_grouped <- mtcars_selected %>% group_by(cyl) %>% summarize(avg_mpg = mean(mpg))
mtcars_grouped
```

# Step 3: Data Visualization with `ggplot2`

## 8. Create a Scatter Plot

Use `ggplot2` to create a scatter plot of `mpg` vs `hp`.

```{r scatter-plot}
ggplot(data = mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  labs(title = "Scatter plot of MPG vs Horsepower",
       x = "Horsepower",
       y = "Miles per Gallon")
```

## 9. Create a Bar Plot

Use `ggplot2` to create a bar plot showing the average `mpg` for each `cyl`.

```{r bar-plot}
ggplot(data = mtcars_grouped, aes(x = factor(cyl), y = avg_mpg)) +
  geom_bar(stat = "identity") +
  labs(title = "Average MPG for Each Cylinder",
       x = "Number of Cylinders",
       y = "Average MPG")
```

## 10. Create a Histogram

Use `ggplot2` to create a histogram of the `mpg` values.

```{r histogram}
ggplot(data = mtcars, aes(x = mpg)) +
  geom_histogram(binwidth = 2, fill = "blue", color = "black") +
  labs(title = "Histogram of MPG",
       x = "Miles per Gallon",
       y = "Frequency")
```

# Step 4: Combining Everything

Combine all steps into a single script to practice the workflow from data manipulation to visualization.

```{r combined-script, eval=FALSE}
library(tidyverse)

# Load dataset
data(mtcars)

# Data manipulation
mtcars_selected <- mtcars %>% select(mpg, cyl, hp)
mtcars_filtered <- mtcars_selected %>% filter(cyl > 6)
mtcars_mutated <- mtcars_filtered %>% mutate(hp_per_cyl = hp / cyl)
mtcars_summary <- mtcars_mutated %>% summarize(avg_mpg = mean(mpg), avg_hp_per_cyl = mean(hp_per_cyl))
mtcars_grouped <- mtcars_selected %>% group_by(cyl) %>% summarize(avg_mpg = mean(mpg))

# Data visualization
# Scatter plot
ggplot(data = mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  labs(title = "Scatter plot of MPG vs Horsepower",
       x = "Horsepower",
       y = "Miles per Gallon")

# Bar plot
ggplot(data = mtcars_grouped, aes(x = factor(cyl), y = avg_mpg)) +
  geom_bar(stat = "identity") +
  labs(title = "Average MPG for Each Cylinder",
       x = "Number of Cylinders",
       y = "Average MPG")

# Histogram
ggplot(data = mtcars, aes(x = mpg)) +
  geom_histogram(binwidth = 2, fill = "blue", color = "black") +
  labs(title = "Histogram of MPG",
       x = "Miles per Gallon",
       y = "Frequency")
```

# Bonus: "but I use *GraphPad*, this plot looks bad..."

```{r}
library(ggprism)
ggplot(data = mtcars, aes(x = mpg)) +
  geom_histogram(binwidth = 2, fill = "blue", color = "black") +
  labs(title = "Histogram of MPG",
       x = "Miles per Gallon",
       y = "Frequency") +
 theme_prism(base_size = 16)
```

# Exercise for you:

-   Apply the *GraphPad* theme to the **scatter plot of MPG vs Horsepower**


```{r}


```

# References

-   [R for Data Science by Hadley Wickham & Garrett Grolemund](https://r4ds.had.co.nz/)
-   [DataCamp - Introduction to the Tidyverse](https://www.datacamp.com/courses/introduction-to-the-tidyverse)
-   [RStudio Cheatsheets](https://www.rstudio.com/resources/cheatsheets/)
