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
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
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
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>
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.
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()
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.
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.
# Some summary statistics
ilo_data %>%
group_by(year) %>%
summarize(mean_hourly_compensation = mean(hourly_compensation),
mean_working_hours = mean(working_hours)) %>%
# pipe the above data frame into the knitr::kable function
knitr::kable()| 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.
The data is now filtered so it only contains the years 1996 and 2006 – a good time range for comparison.
ilo_data <- ilo_data %>%
filter(year == "1996" | year == "2006")
# Reorder country factor levels
ilo_data <- ilo_data %>%
# Arrange data frame first, so last is always 2006
arrange(year) %>%
# Use the fct_reorder function inside mutate to reorder countries by working hours in 2006
mutate(country = fct_reorder(country,
working_hours,
last))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.
# Compute temporary data set for optimal label placement
median_working_hours <- ilo_data %>%
group_by(country) %>%
summarize(median_working_hours_per_country = median(working_hours)) %>%
ungroup()
# Have a look at the structure of this data set
str(median_working_hours)## 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 ...
# Plot
ggplot(ilo_data) +
geom_path(aes(x = working_hours, y = country),
arrow = arrow(length = unit(1.5, "mm"), type = "closed")) +
# Add labels for values (both 1996 and 2006)
geom_text(
aes(x = working_hours,
y = country,
label = round(working_hours, 1),
hjust = ifelse(year == "2006", 1.4, -0.4)
),
# Change the appearance of the text
size = 3,
family = "NanumBarunGothic",
color = "gray25"
) +
# Add labels for country
geom_text(data = median_working_hours,
aes(y = country,
x = median_working_hours_per_country,
label = country),
vjust = 2,
family = "NanumBarunGothic",
color = "gray25") +
# Add titles
labs(
title = "People work less in 2006 compared to 1996",
subtitle = "Working hours in European countries, development since 1996",
caption = "Data source: ILO, 2017"
) +
# Apply your theme
theme_ilo() +
# Remove axes and grids
theme(
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
panel.grid = element_blank(),
# Also, let's reduce the font size of the subtitle
plot.subtitle = element_text(size = 9)
) +
# Reset coordinate system
coord_cartesian(xlim = c(25, 41))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.
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.