---
title: "Functions of the `tidyverse` Package"
author: "Valerio Licursi"
date: "2024-06-26"
format: html
---

# Introduction

In this exercise, you will learn more advanced data manipulation and visualization functions provided by the `tidyverse` package in R. This includes data wrangling techniques with `dplyr` and visualizations with `ggplot2`.

## Prerequisites

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

# Step 1: Loading Libraries and Dataset

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

# Step 2: Data Manipulation with `dplyr`

## 1. Select and Rename Columns

Use the `select` and `rename` functions to choose columns and rename `Sepal.Length` to `Sepal_Length`.

```{r select-rename-columns}
iris_selected <- iris %>% select(Sepal_Length = Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species)
head(iris_selected)
```

## 2. Create Multiple New Columns

Use the `mutate` function to create two new columns: `Sepal_Ratio` (Sepal.Length / Sepal.Width) and `Petal_Ratio` (Petal.Length / Petal.Width).

```{r mutate-multiple-columns}
iris_mutated <- iris_selected %>% mutate(Sepal_Ratio = Sepal_Length / Sepal.Width, Petal_Ratio = Petal.Length / Petal.Width)
head(iris_mutated)
```

## 3. Filter and Arrange Data

Use the `filter` function to select rows where `Species` is `setosa` and arrange the data by `Sepal_Length` in descending order.

```{r filter-arrange-data}
iris_filtered <- iris_mutated %>% filter(Species == "setosa") %>% arrange(desc(Sepal_Length))
head(iris_filtered)
```

## 4. Summarize with Grouping

Use the `group_by` and `summarize` functions to get the average `Sepal_Length` and `Petal_Length` for each species.

```{r summarize-group-by}
iris_summary <- iris_mutated %>% group_by(Species) %>% summarize(avg_Sepal_Length = mean(Sepal_Length), avg_Petal_Length = mean(Petal.Length))
iris_summary
```

# Step 3: Data Visualization with `ggplot2`

## 5. Create a Boxplot

Use `ggplot2` to create a boxplot of `Sepal_Length` for each species.

```{r boxplot}
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  labs(title = "Boxplot of Sepal Length by Species",
       x = "Species",
       y = "Sepal Length")
```

## 6. Create a Scatter Plot with Linear Trend Line

Use `ggplot2` to create a scatter plot of `Sepal_Length` vs `Petal_Length` and add a linear trend line.

```{r scatter-plot-trend-line}
ggplot(data = iris, aes(x = Petal.Length, y = Sepal.Length)) +
  geom_point() +
  geom_smooth(method = "lm", col = "red") +
  labs(title = "Scatter plot of Sepal Length vs Petal Length with Linear Trend Line",
       x = "Petal Length",
       y = "Sepal Length")
```

## 7. Create a Faceted Plot

Use `ggplot2` to create a faceted plot of `Sepal_Length` vs `Sepal.Width`, faceted by `Species`.

```{r faceted-plot}
ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length)) +
  geom_point() +
  facet_wrap(~ Species) +
  labs(title = "Faceted Plot of Sepal Length vs Sepal Width by Species",
       x = "Sepal Width",
       y = "Sepal Length")
```

# 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(iris)

# Data manipulation
iris_selected <- iris %>% select(Sepal_Length = Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species)
iris_mutated <- iris_selected %>% mutate(Sepal_Ratio = Sepal_Length / Sepal.Width, Petal_Ratio = Petal.Length / Petal.Width)
iris_filtered <- iris_mutated %>% filter(Species == "setosa") %>% arrange(desc(Sepal_Length))
iris_summary <- iris_mutated %>% group_by(Species) %>% summarize(avg_Sepal_Length = mean(Sepal_Length), avg_Petal_Length = mean(Petal.Length))

# Data visualization
# Boxplot
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  labs(title = "Boxplot of Sepal Length by Species",
       x = "Species",
       y = "Sepal Length")

# Scatter plot with trend line
ggplot(data = iris, aes(x = Petal.Length, y = Sepal.Length)) +
  geom_point() +
  geom_smooth(method = "lm", col = "red") +
  labs(title = "Scatter plot of Sepal Length vs Petal Length with Linear Trend Line",
       x = "Petal Length",
       y = "Sepal Length")

# Faceted plot
ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length)) +
  geom_point() +
  facet_wrap(~ Species) +
  labs(title = "Faceted Plot of Sepal Length vs Sepal Width by Species",
       x = "Sepal Width",
       y = "Sepal Length")
```

# 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/)
