forked from ac812/reproducibility-training
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRNotebookExample4.Rmd
92 lines (65 loc) · 3.2 KB
/
RNotebookExample4.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: "RNotebookExample"
author: "Alexia Cardona"
output: html_document
date: "`r format(Sys.time(), '%d %B %Y')`"
---
This report contains the analysis of the `gapminder` dataset and contains the results of the top Countries with the largest life expectancy in Europe. The analysis is based on data from 2007. The report also analyzes how life expectancy changed over the years in Europe.
This report was generated using R and the code to extract the European 2007 data is as follows:
```{r load-data, message=FALSE}
#load tidyverse library
library(tidyverse) # used for data manipulation
library(rmarkdown) # used for paged_table function
library(kableExtra) # used for table
library(ggpubr) #used for ggarrange function
#read file into R
pop_data <- read_csv("data/gapminder_data.csv")
#create a table with data from European countries in 2007 showing the countries with the largest life expectancy at the top
euro_data_tbl <- pop_data %>%
filter(continent == "Europe" & year == 2007) %>%
select(-continent, -year) %>%
arrange(desc(lifeExp)) %>%
rename(Country = country, "Population Size" = pop, "Life Expectancy" = lifeExp, "GDP" = gdpPercap)
```
The results in euro_data_tbl are displayed in the Table below:
```{r kbl-table}
euro_data_tbl %>%
kable(caption="European countries ordered by greatest life expectancy from 2007 data") %>%
kable_styling(bootstrap_options = "striped", full_width = F) %>%
scroll_box(width = "100%", height = "200px")
```
A better way to display this table is with pagination as follows:
```{r paged-table}
paged_table(euro_data_tbl)
```
Next, the life expectancy in Europe is observed across different years. The aim of this is to check if there was a change in life expectancy over the years. The data used for the life expectancy over the years analysis is the gapminder data and the following data manipulation was performed:
```{r fig-data}
#keep on European data and change year to factor
euro_data_fig <- pop_data %>%
filter(continent == "Europe") %>%
mutate(year=as_factor(year))
#keep only United Kingdom data and change year to factor
uk_data_fig <- pop_data %>%
filter(country == "United Kingdom") %>%
mutate(year=as_factor(year))
```
The life expectancy for Europe over the years is plotted as following:
```{r fig, warning=FALSE, fig.cap="Life Expectancy in Europe over the years"}
#Euro plot
euro_plot <- euro_data_fig %>%
ggplot(mapping=aes(x=year, y=lifeExp)) +
geom_violin() +
stat_summary(fun.y = median, geom = "point")
#draw euro plot
euro_plot
```
\
Comparison of life expectancy from Europe and United Kingdom:
```{r fig-comp, warning=FALSE, fig.cap="Life Expectancy in European and UK over the years"}
#UK plot
uk_plot <- uk_data_fig %>%
ggplot(mapping=aes(x=year, y=lifeExp)) +
geom_point()
#draw euro plot next to UK plot
ggarrange(euro_plot, uk_plot, ncol=2, nrow=1, labels="AUTO")
```