# Ensure that libraries are loaded. library(tidyverse) library(learnr) library(gradethis) library(knitr) library(kableExtra) library(RColorBrewer) tutorial_options(exercise.timelimit = 20, exercise.checker = gradethis::grade_learnr) knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE) # Note: A (new) R Markdown document can be knitted while a learnr tutorial is # running both if the tutorial is started from the console and from the Tutorial tab.
# Ensure that the data is loaded for the remainder of this tutorial. glbwarm <- UsingRTutorials::glbwarm
Course Content
Let us explore the main features and options of R Markdown step by step.
knitr::include_graphics("images/OutputOptions.png")
In this menu you can change several features including:
Many more YAML options can be set manually:
knitr::include_graphics("images/VisualEditor.png")
Use the visual editor for easy:
To include literature references in an R Markdown document, you need four things:
references.bib
.bibliography: references.bib
.csl: apa.csl
.knitr::include_graphics("images/InsertCitation.png")
Adding a reference within the visual editor:
references.bib
.RStudio then:
references.bib
.To get the literature references in APA style, we need a citation style file.
Figures usually demand additional attention:
Output Options... sets for all figures:
Adjust a single figure with code chunk options (R Markdown Cheat Sheet, p. 1, bottom):
fig.cap =
figure caption as string.fig.align =
'left', 'right', or 'center'.out.width =
width of figure in output in percentage of line width ("80%"
) or as a fixed width in pixels for HTML ("300px"
) or inches, centimeters for PDF and Word ("4.5in"
, "6cm"
).fig.asp =
ratio of height to width.Don't use:
fig.width
and fig.height
: specify the actual size (whatever that may be) of the figure instead of the output size.Note:
knitr
may end with the error message that the margins are too large.Table menu in visual editor:
Present summary results as a table with knitr::kable()
in a code chunk.
glbwarm %>% group_by(sex) %>% summarise(sex_mean = round(mean(govact), digits=2), sex_sd = round(sd(govact), digits=2)) %>% kable(col.names = c("Sex", "Average Score", "Standard Deviation"), caption = "Table: Support for government intervention across the sexes.")
For additional table layout options:
kableExtra::
bookdown::
Figures are not automatically numbered in HTML or Word output.
To accomplish this, you need the bookdown::
package.
Follow these steps:
bookdown
package from CRAN.date:
, add the line site: "bookdown::bookdown_site"
.output:
in YAML replace:html_document
by bookdown::html_document2
,pdf_document
by bookdown::pdf_document2
, andword_document
by bookdown::word_document2
.\@ref(<section ID>)
- Create Section ID: {#label}
.\@ref(tab:<chunk ID>)
- Chunk ID: chunk name.\@ref(fig:<chunk ID>)
- Chunk ID: chunk name.Problems with knitting to HTML:
eval=FALSE
to see if a code chunk causes the problem.Problems with knitting to Word:
Problems with knitting to PDF:
%
, \
, /
have special meaning in LaTeX, which is the language used to create the PDF.Setting plot labels:
labs()
layer: basic adjustments.theme()
layer: fine tuning of all details, including the legend. Note: features of graphical elements are specified with functions (with their own help page):element_text()
, e.g., theme(plot.title = element_text(size = 10))
element_rect()
, e.g., theme(plot.background = element_rect(fill = "green"))
element_line()
, e.g., theme(axis.line = element_line(linewidth = 3, colour = "grey80"))
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + labs(title = "Support for intervention by age", x = "Age", y = "Support for government intervention", color = "Party identification" ) + theme(legend.position = "bottom")
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + labs(title = "Support for intervention by age", x = "Age", y = "Support for government intervention", color = "Party identification") + theme(legend.position = "bottom")
gradethis::grade_code( correct = "", incorrect = "You may have the layers in a different order, e.g., first theme() and then labs(). That is OK." )
Some plot characteristics are combined into theme types, e.g.:
theme_bw()
, theme_classic()
, theme_dark()
, theme_void()
, theme_minimal()
.You can set some overall plot characteristics within these theme layers, e.g.:
base_size
(size of fonts used) base_family
(font type used: "serif"
, "sans"
, or "mono"
).ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + labs(title = "Support for intervention by age", x = "Age", y = "Support for government intervention", color = "Party identification") + theme_classic(base_size = 14, base_family = "sans") + theme(legend.position = "bottom")
Argument base_size
sets a base size for all texts displayed in a plot.
If you want to set the size of a particular type of text within the plot, for example, the text values displayed in the legend, you can do that within a theme()
layer.
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + labs(title = "Support for intervention by age", x = "Age", y = "Support for government intervention", color = "Party identification") + theme_classic(base_size = 14, base_family = "sans") + theme(legend.position = "bottom", legend. legend. )
# The arguments to change legend title or legend entries are among the ones # starting with legend. ; makes sense, doesn't it? ?theme
# The arguments that address properties of the text, use function element_text(). Check out help on this function. ?element_text
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + labs(title = "Support for intervention by age", x = "Age", y = "Support for government intervention", color = "Party identification") + theme_classic(base_size = 14, base_family = "sans") + theme(legend.position = "bottom", legend.title = element_text(size = 10), legend.text = element_text(size = 8))
gradethis::grade_code( correct = "", incorrect = "" )
For marking groups by colors , ggplot()
uses a vector of colors, assigning the first group to the first color, and so on.
Custom colors:
RColorBrewer
offers nice color palettes (book, p. 458). brewer.pal
in this package creates such a vector.Democrats should be blue, Republicans red, and let us use green for Independents.
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + labs( title = "Support for intervention by age", x = "Age", y = "Support for government intervention", color = "Party identification" ) + theme_classic(base_size = 14, base_family = "sans") + theme(legend.position = "bottom") + scale_color_manual()
# Look up the Accent palette in the book or with the code below. display.brewer.pal(8, name = "Accent") # It has 8 colors ordered from green [1] to darkgrey [8]. # Which entries do you need from this vector?
# Extract the codes of the three desired colors from the palette. brewer.pal(8, "Accent")[c( )] # Note: 8 specifies the number of colors in this palette.
# The `values =` argument in scale_color_manual() assigns colors to groups. scale_color_manual(values = brewer.pal(8, "Accent")[c(5, 1, 6)])
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + labs(title = "Support for intervention by age", x = "Age", y = "Support for government intervention", color = "Party identification") + theme_classic(base_size = 14, base_family = "sans") + theme(legend.position = "bottom") + scale_color_manual(values = brewer.pal(8, "Accent")[c(5, 1, 6)])
gradethis::grade_code( correct = "", incorrect = "" )
Fine-tuning the ticks (value labels) on axes:
scale_x_continuous()
and scale_y_continuous()
,ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + theme_bw(base_size = 14) + theme(legend.position = "none")
# Add the two layers. ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + theme_bw(base_size = 14) + theme(legend.position = "none") + scale_y_continuous() + scale_x_continuous()
# Use `breaks =` for labels with grid lines and `minor_breaks =` for grid lines only. ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + theme_bw(base_size = 14) + theme(legend.position = "none") + scale_y_continuous(breaks = , minor_breaks = ) + scale_x_continuous(breaks = , minor_breaks = )
# Use an efficient way to get sequences of numbers for the axis values. 1:7 seq(from = 20, to = 90, by = 10)
# And don't forget to switch off minor breaks on the Y axis. minor_breaks = NULL
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + geom_jitter() + theme_bw(base_size = 14) + theme(legend.position = "none") + scale_y_continuous(breaks = 1:7, minor_breaks = NULL) + scale_x_continuous(breaks = seq(from = 20, to = 90, by = 10), minor_breaks = seq(from = 15, to = 87.5, by = 2.5))
gradethis::grade_code( correct = "", incorrect = "Perhaps, you added the scale layers in a different order or in a different place. That is OK if you see the required axis values and grey lines." )
Use graphical primitives and geom_label()
or geom_text()
to touch up a graphic.
There are two ways of manually positioning graphical primitives within a plot
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + scale_color_manual(values = brewer.pal(8, "Accent")[c(5, 1, 6)]) + geom_jitter() + geom_text(x = 90, y = 4, label = "Source: Global warming data set by Eric Nisbeth. Author: Yah Tutor.", angle = 90, size = 2.5, color = "black") + scale_x_continuous(limits = c(15, 90)) + theme_classic(base_size = 11) + theme(legend.position = "bottom")
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + scale_color_manual(values = brewer.pal(8, "Accent")[c(5, 1, 6)]) + geom_jitter() + geom_text( ) + scale_x_continuous(limits = c(15, 90)) + #ensures that the text fits within the plot theme_classic(base_size = 11) + theme(legend.position = "bottom")
# Check out section Aesthetics in the help on geom_text() to find the # arguments that you need.
# You are supposed to provide the coordinates manually in the ggplot code # (instead of calculating it from the data set.) # For this reason, do not put the coordinates within an aes() function.
# Divide the label over two lines in your code to get the source and author on # different lines. label = "Source: Global warming data set by Eric Nisbeth. Author: Yah Tutor."
ggplot(data = glbwarm, mapping = aes(x = age, y = govact, color = partyid)) + scale_color_manual(values = brewer.pal(8, "Accent")[c(5, 1, 6)]) + geom_jitter() + geom_text(x = 90, y = 4, label = "Source: Global warming data set by Eric Nisbeth. Author: Yah Tutor.", angle = 90, size = 2.5, color = "black") + scale_x_continuous(limits = c(15, 90)) + theme_classic(base_size = 11) + theme(legend.position = "bottom")
gradethis::grade_code( correct = "", incorrect = "" )
papaja::
Instead of adapting a standard R Markdown document, you can use the papaja
package, which contains an APA6 article template. Once you have installed this package, an APA article template is available if you create a new R Markdown file using the From Template option (see figure).
knitr::include_graphics("images/template.png")
If you didn't do so, install papaja
. Run the command remotes::install_github("crsh/papaja")
from the RStudio console.
papaja
uses bookdown
but you do not have to load this package separately.
A manual for papaja
is available at https://crsh.github.io/papaja_man/ .
The rticles
package offers R Markdown templates for several journals and some journal publishers.
If you install this package, the journal or publisher templates are shown in the list of templates when you create a new R Markdown document.
A dashboard is used to show a group of related data visualizations. Below is a screen shot of an example created with package flexdashboard
. It uses HTML widgets to show pop-ups when you hover over a graph (in the original dashboard, not in the screen shot below).
knitr::include_graphics("images/dashboard.png")
Read more about flexdashboard
at https://RMarkdown.rstudio.com/flexdashboard/index.html .
Another package is shinydashboard
, which uses shiny to create interactive dashboards. See https://rstudio.github.io/shinydashboard/index.html for more information.
Report:
Presentation (15 or 20 minutes per group, not necessarily in R Markdown):
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.