---
title: "Exercise: Introduction to `ggplot2`"
author: "Valerio Licursi"
date: "June 26, 2024"
institute: "IBPM-CNR"
format: html
execute:
  echo: true
---

#### Objective:
Learn how to create and customize basic plots using the `ggplot2` package.

#### Prerequisites:
- Basic knowledge of R and data frames
- `ggplot2` package installed (`install.packages("ggplot2")`)

### Exercise Steps:

#### 1. Install and Load `ggplot2`
First, ensure you have the `ggplot2` package installed and loaded.

```{r}
# install.packages("ggplot2") # don't execute it if you have already installed the package
library(ggplot2)
```

#### 2. Load the `mtcars` Dataset
For this exercise, we will use the built-in `mtcars` dataset.

```{r}
data(mtcars)
head(mtcars)
```

#### 3. Create a Basic Scatter Plot
Use `ggplot` to create a scatter plot of `mpg` (miles per gallon) vs `wt` (weight of the car).

```{r}
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point()
```

#### 4. Add Aesthetics
Enhance your scatter plot by adding color to differentiate between the number of cylinders (`cyl`).

```{r}
ggplot(data = mtcars, aes(x = wt, y = mpg, color = as.factor(cyl))) +
  geom_point()
```

#### 5. Add Titles and Labels
Add a title, and labels for the x and y axes.

```{r}
ggplot(data = mtcars, aes(x = wt, y = mpg, color = as.factor(cyl))) +
  geom_point() +
  ggtitle("MPG vs Weight of Cars") +
  xlab("Weight (1000 lbs)") +
  ylab("Miles per Gallon") +
  labs(color = "Cylinders")
```

#### 6. Create a Boxplot
Create a boxplot to visualize the distribution of `mpg` for each `cyl` category.

```{r}
ggplot(data = mtcars, aes(x = as.factor(cyl), y = mpg)) +
  geom_boxplot() +
  xlab("Cylinders") +
  ylab("Miles per Gallon") +
  ggtitle("Boxplot of MPG by Cylinders")
```

#### 7. Create a Histogram
Create a histogram to visualize the distribution of `mpg`.

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

#### 8. Create a Faceted Plot
Use faceting to create multiple plots based on the number of cylinders (`cyl`).

```{r}
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  facet_wrap(~ cyl) +
  xlab("Weight (1000 lbs)") +
  ylab("Miles per Gallon") +
  ggtitle("MPG vs Weight Faceted by Number of Cylinders")
```

### Additional Customizations:
- **Change Themes**: Try different themes such as `theme_minimal()`, `theme_classic()`, etc.
- **Modify Point Shapes and Sizes**: Use the `shape` and `size` aesthetics in `geom_point()`.
- **Save the Plot**: Use `ggsave()` to save your plot to a file.

```{r}

```

#### References:
- [ggplot2 documentation](https://ggplot2.tidyverse.org/)
- [R Graphics Cookbook by Winston Chang](http://www.cookbook-r.com/Graphs/)
