---
title: "III.\\ Graphics with ggplot2 (solution)"
author: "Data Science Lab, University of Copenhagen"
date: August 2025
output: html_document
---

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

Loading the core tidyverse packages and the 'readxl' package for data import from .xlsx.

```{r}
library(tidyverse)
library(readxl)
```

Importing the climate data from **climate.xlsx**^[Contains public sector information licensed under the [Open Government Licence v3.0](http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/).]. (Change the path to the Excel file below so that it matches the path to the file saved on your own computer, or use *Import Dataset* in RStudio to obtain the relevant code.)

```{r}
climate <- read_excel("climate.xlsx")
climate
```

### Scatter plot I

```{r scatter_plot_i}
ggplot(climate, aes(x = sun, y = rain, colour = station)) + 
  geom_point() +
  facet_wrap(~station) +
  theme_bw() +
  theme(legend.position = "none")
```

### Graphic files

```{r}
ggsave(file="weather.jpeg")

ggsave(file="weather.png",width=10,height=8,units="cm")
```

### Line plot

```{r}
ggplot(climate, aes(x = month, y = rain, colour = station)) + geom_line()

climate <- mutate(climate, month = factor(month))

ggplot(climate, aes(x = month, y = rain, group = station, colour = station)) +
  geom_line() +
  geom_point() +
  theme(legend.position = "top") +
  geom_hline(yintercept = mean(climate$rain), linetype = "dashed") +
  labs(x = "Month", y = "Rainfall (mm)", colour = "Weather station")
```

### Box plot(s)

```{r}
ggplot(climate, aes(x = station, y = sun)) +
  geom_boxplot(fill = "lightgreen")

ggplot(climate, aes(x = station, y = sun)) +
  geom_boxplot(aes(fill = station)) +
  theme(legend.position = "none")
```

### Histogram

```{r}
ggplot(climate, aes(x = rain)) +
  geom_histogram(binwidth = 25, colour = "red", fill = "orange")
```

### Bar chart I - a plot often used in papers!

```{r}
summary_stats <- 
  climate %>%
  group_by(month) %>% 
  summarize(sun_avg = mean(sun), sun_sd = sd(sun))
```

```{r}
ggplot(summary_stats, aes(x = month, y = sun_avg)) +
  geom_col(fill = "lightblue") +
  geom_errorbar(aes(ymin = sun_avg - sun_sd, ymax = sun_avg + sun_sd), width = 0.2)
```

### Scatter plot II

```{r}
p <- ggplot(summary_stats, aes(x = month, y = sun_avg)) +
  geom_point() +
  geom_errorbar(aes(ymin = sun_avg - sun_sd, ymax = sun_avg + sun_sd), width = 0.3)
p

p + coord_flip()
```

### Bar chart II

```{r}
ggplot(climate, aes(x = month, y = sun, fill = station)) +
  geom_col() +
  labs(x = "Month", y = "Sunshine (hours)", fill = "Weather station")
```

### Bar chart III

```{r}
ggplot(climate, aes(x = station, y = rain)) +
  geom_col()
```

```{r}
annual_rain <- 
  climate %>%
  group_by(station) %>% 
  summarize(rain = sum(rain)) %>% 
  arrange(rain)

climate <- mutate(climate, station = factor(station, levels = annual_rain$station))

ggplot(climate, aes(x = station, y = rain)) +
  geom_col() +
  geom_label(mapping = aes(x = station, y = rain, label = rain), data = annual_rain, nudge_y = 60)
```

### Scatter plot III

```{r}
example_data <- tibble(X = 1:5, Y = 2*X, Z = letters[1:5])
example_data
```

```{r}
ggplot() +
  geom_point(mapping = aes(x = X, y = Y), data = example_data)

ggplot() +
  geom_text(mapping = aes(x = X, y = Y, label = Z), data = example_data)

ggplot() +
  geom_text(mapping = aes(x = X, y = Y, label = Z), data = example_data, nudge_y = 0.3)
```

**End of solution**
