Assignment B

Prepare File

In RStudio, choose File > New File > R Markdown…

Choose PDF as your Default Output Format.

Your header and the first chunk should look like this (but with your name and today’s date):

---
title: "Assignment B"
author: "Your Name"
date: "YYYY-MM-DD"
output: pdf_document
---

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

Keep the above code (the header and the first chunk), and delete everything below.

Change the setting on the last row to echo = FALSE. This will prevent R from showing your code, and just show the result of the code (like graphs and tables), in the PDF output.

Select File > Save as… and save your document in a folder that you can easily find on your computer.

Suggested file name: R Assignment B.Rmd

Data Set

Download the following data set and move it to the same folder as your Rmd file:

Electricity Production in Sweden

In the data file you find the yearly production (GWh) of electricity in Sweden for the years 2001 – 2022 by type of production: hydro, wind, solar, nuclear, and other sources of power.

IMPORTANT! The R Assignment B.Rmd file and the Electricity Production in Sweden.xlsx file must be located in the same folder on your computer before you proceed!

Packages

Open your R Markdown file (R Assignment B.Rmd).

In the Console (the lower left window), type the following code and run it by pressing Enter:

install.packages("readxl")

Wait a while until the package is installed. Then run the following code the same way as above.

install.packages("ggplot2")

Import Data

Choose Code > Insert Chunk.

Load the data into R with the following code:

```{r}
library("readxl")
data<-read_excel("Electricity Production in Sweden.xlsx")
```

Click the Run button (green play button) in the top right corner of the code chunk to load the Excel file into R. Now, you can view the data in the Environment tab (located in the upper right window of RStudio). You can click the data to see its contents.

a)

Find the total yearly production of electricity. Plot the time series.

To find the total yearly production of electricity (and add the total to the data) you can use the following code:

```{r}
data$Total <- rowSums(data[, c("Hydro", 
                               "Wind", 
                               "Solar", 
                               "Nuclear", 
                               "Other")])
```

To plot the time series you can use the following code:

```{r, fig.width = 8, fig.height = 4}
library(ggplot2)
ggplot(data, aes(x = Year, y = Total)) +
  geom_line() +
  labs(x = "Year", y = "Total Electricity Production")
```

b)

Find the fraction of electricity produced by wind and solar power. The fraction must be given in percentage points. Plot the time series.

You can use the same code as above, but with a couple of changes.

First, instead of plotting the Total variable as y, you want to plot the fraction (Solar + Wind) / Total instead. Try adjusting the code.

Second, adjust the title of the y-axis.

c)

Calculate the share of each type of production for the year 2001. Illustrate the five shares in a pie chart.

To calculate the share of each type of production for the year 2001 you can use the following code:

```{r}
data_2001 <- subset(data, Year == 2001)

Hydro_2001   <- data_2001$Hydro  / data_2001$Total
Wind_2001    <- data_2001$Wind   / data_2001$Total
Solar_2001   <- data_2001$Solar  / data_2001$Total
Nuclear_2001 <- data_2001$Nuclear/ data_2001$Total
Other_2001   <- data_2001$Other  / data_2001$Total
```

To illustrate the five shares in a pie chart you can use the following code:

```{r, fig.width = 4, fig.height = 4}
labels_2001 <- c("Hydro", "Wind", "Solar", "Nuclear", "Other")
values_2001 <- c(Hydro_2001, Wind_2001, Solar_2001, Nuclear_2001, Other_2001)

pie_data_2001 <- data.frame(Type = labels_2001, Share = values_2001)

ggplot(pie_data_2001, aes(x = "", y = Share, fill = Type)) +
  geom_bar(width = 1, stat = "identity", color = "white") +
  coord_polar("y", start = 0) +
  theme_void() +
  labs(title = "Share of Electricity Production by Type 2001")
```

d)

Do the same calculations for the year 2022 and illustrate in a pie chart.

You can use the same code as above, but changing all instances of 2001 to 2022 instead.

e)

Compare the pie charts.

In assignments a) to d) you were supposed to use chunks (sections that start and end with the three tick marks) so that R knows that you are writing code. In this assignment you want to add some text to your report, and should thus not use chunks. Instead, you can type your answer directly without using any tick marks.

When you are done with assignments a) to e), knit your document into a PDF as you did in Assignment A. You do not need to hand in Assignment B.