Assignment 1

Preparation

Download this Rmarkdown file, fill in your name and student number, and complete your assignment in the Rmarkdown file. “knit” your text and code into a pdf-file, and upload this pdf-file on Brightspace before the next lecture.

Packages

library(ggplot2)

Install and load new package

Install the gapminder-package which allows you to make use of the gapminder dataset:

install.packages("gapminder")

(you only have to do this once!)

Load the gapminder data:

library(gapminder)

Get a quick overview of the dataset

head(gapminder) # gapminder is name of dataset
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.

For explanation of variables, click here.

Assignment

  1. Create a histogram of lifeExp, the life-expectancy in years for the different countries in the dataset across the different years. Select an appropriate ‘binwidth’. Change colours to your liking.

  2. Describe in a few sentences what you see in the graph.

  3. Create the same histogram, but only for the last year of measurement, which is 2007. You can create a new dataset for 2007 only by running the following code:

gapminder_2007 <- gapminder[gapminder$year == 2007, ]
  1. Explain in a few sentences how the graph is different from the one you created in 1.

  2. For the dataset gapminder_2007, create density-plot for the variable pop (referring to population size of a country). Choose colours to your liking.

  3. Also create a histogram. Add a different theme!

  4. Why are both of the graphs not very informative?

  5. Do you have a suggestion how to make this graph more informative?

  6. Make a density-plot of the variable ‘countries’. Why doesn’t this work?

  7. What would be a more appropriate type of graph for this variable?

Submit your pdf-file online

When you are done with your assignment, try clicking the “Knit”-button in Rstudio so that your code will be knitted into a pdf-file. Submit this file on Brightspace.

If you see an error appear, try to fix the error and try again. If you can’t get the “knitting” to work, then you can copy your code, text, and graphs into a word-file and submit it on Brightspace.