0.1 Lectures

0.1.1 Change the overall appearance of your report

In the YAML header, more than just the title and output format can be specified. You can also customize things such as syntax highlighting or the overall appearance by specifying a custom theme.

output: 
  html_document:
    theme: cosmo
    highlight: monochrome

0.1.2 Add a table of contents

Another cool feature of RMarkdown reports (whether HTML or PDF) is an automatically generated table of contents (TOC). And with several settings, you can customize your TOC quite a bit:

You add a table with toc: true and specify whether it should be floating (= whether it moves along as you scroll) with toc_float.

The depth of your TOC is set with toc_depth.

output: 
  html_document:
    theme: cosmo
    highlight: monochrome
    toc: true
    toc_float: false
    toc_depth: 4

0.1.3 More YAML hacks

There are many more customizations for RMarkdown reports, and most of them can be configured via the YAML header.

Before you dig deeper into custom stylesheets, let’s enable code folding with code_folding: …. This will allow your readers to completely hide all code chunks or show them.

output: 
  html_document:
    theme: cosmo
    highlight: monochrome
    toc: true
    toc_float: false
    toc_depth: 4
    number_sections: true
    code_folding: hide

0.1.4 Change style attributes of text elements

With CSS, it’s easy to change the appearance of text in your report. In this exercise, you’re going to change the font to a font with serifs, in accordance with the style of your plots. You’re also going to try out a few other CSS selectors in order to change some colors and font sizes in your report. For example, the font of the R code elements is currently a little on the larger side, compared to the surrounding prose. You’ll use CSS to reduce their size.

Here, all of your CSS should go inside the <style> tags above the Summary. In the next exercise, you’ll learn how to reference an external CSS file using the YAML header. If you need more help regarding the styling of text, you can refer to the Mozilla Developer reference.

<style>
body, h1, h2, h3, h4 {
    font-family: "Bookman", serif;
}

body {
    color: #333333;
}
a, a:hover {
    color: red;
}
pre {
    font-size: 10px;
}
</style>

0.1.5 Reference the style sheet

See the new pane in the exercise interface called styles.css? As mentioned in the previous exercise, you can reference an external CSS file in the YAML header of your RMarkdown document like so:

title: "Test"
output:
  html_document:
    css: styles.css

Your CSS from before is now contained in styles.css. It’s time to reference styles.css in your YAML header so that the CSS rules are applied to your report.

0.1.6 Beautify a table with kable

You’ve just heard it: There are two ways to beautify a table with the kable package: either directly in code chunks by calling the knitr::kable() function or in the YAML header. Here you will try out the former. my_data_frame %>% knitr::kable()

0.2 Summary

The International Labour Organization (ILO) has many data sets on working conditions. For example, one can look at how weekly working hours have been decreasing in many countries of the world, while monetary compensation has risen. In this report, the reduction in weekly working hours in European countries is analysed, and a comparison between 1996 and 2006 is made. All analysed countries have seen a decrease in weekly working hours since 1996 – some more than others.

0.3 Preparations

0.4 Analysis

0.4.1 Data

The herein used data can be found in the statistics database of the ILO. For the purpose of this course, it has been slightly preprocessed.

The loaded data contains 380 rows.

year mean_hourly_compensation mean_working_hours
1980 9.267500 33.98103
1981 8.692500 33.61923
1982 8.355000 33.47409
1983 7.809091 33.86589
1984 7.543636 33.71051
1985 7.786364 33.73358
1986 9.700000 33.97494
1987 12.146923 33.58138
1988 13.199231 33.66441
1989 13.136154 33.53312
1990 16.016923 33.41275
1991 16.548461 33.04459
1992 17.807692 32.82919
1993 16.502857 32.48288
1994 16.543333 32.92779
1995 18.420589 33.25749
1996 18.528824 33.15975
1997 16.910000 33.10471
1998 16.999412 32.94524
1999 16.865882 32.75325
2000 15.390000 32.45301
2001 15.495882 32.10170
2002 17.186471 31.89615
2003 20.969412 31.77210
2004 23.687059 31.82506
2005 24.501176 31.82508
2006 25.446470 31.78460

As can be seen from the above table, the average weekly working hours of European countries have been descreasing since 1980.

0.4.3 Results

In the following, a plot that shows the reduction of weekly working hours from 1996 to 2006 in each country is produced.

First, a custom theme is defined.

Then, the plot is produced.

## Classes 'tbl_df', 'tbl' and 'data.frame':    17 obs. of  2 variables:
##  $ country                         : Factor w/ 30 levels "Netherlands",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ median_working_hours_per_country: num  27 27.8 28.4 31 30.9 ...

0.4.3.1 An interesting correlation

The results of another analysis are shown here, even though they cannot be reproduced with the data at hand.

The relationship between weekly working hours and hourly compensation.

The relationship between weekly working hours and hourly compensation.

As you can see, there’s also an interesting relationship. The more people work, the less compensation they seem to receive, which seems kind of unfair. This is quite possibly related to other proxy variables like overall economic stability and performance of a country.