Exercises for session 3
The codeblocks here are interactive and can be executed in the browser. You can also copy them over and solve the exercises locally. In the existing code ______
denotes areas that need to be substituted with your own code to solve the exercise.
Each exercise has one or more hints, as well as a full solution if you click through all the hints. Do try to solve the exercises on your own first.
3.1 Working with fonts
Exercise 3.1.1
Download this font file and add the font to your R session. You can confirm that is has been added by calling systemfonts::match_fonts("cirquee")
Use systemfonts::add_fonts()
to make systemfonts aware of the font file
::add_fonts(______)
systemfonts
::match_fonts("cirquee") systemfonts
::add_fonts("Cirquee.otf")
systemfonts
::match_fonts("cirquee") systemfonts
Exercise 3.1.2
Find and install a font from Google Fonts or Font Squirrel using systemfonts. Again, verify it has been added using match_fonts()
If you use Google font you need to use get_from_font_squirrel()
::get_from_font_squirrel(______)
systemfonts
::match_fonts(______) systemfonts
If you use Google font you need to use get_from_google_fonts()
::get_from_google_fonts(______)
systemfonts
::match_fonts(______) systemfonts
::get_from_font_squirrel("Raleway")
systemfonts
::match_fonts("Raleway") systemfonts
or
::get_from_google_fonts("Raleway")
systemfonts
::match_fonts("Raleway") systemfonts
If you chose to get the Raleway font
Read the documentation of systemfonts::get_from_google_fonts()
and systemfonts::require_font()
. How do they differ in where they place the font file and what consequence does it have?
Exercise 3.1.3
Use the two new fonts to style the following plot
You can use the family name of the new fonts inside element_text()
in the theme specification
ggplot(mpg) +
geom_bar(aes(class, fill = factor(cyl)), position = "dodge") +
theme(
text = element_text("Raleway"),
title = element_text("Cirquee")
)
3.2 Basic markdown formatting with marquee
Exercise 3.2.1
Create a marquee text that uses various formatting options like bold, italic, lists, and code blocks. Display it using grid::grid.draw(marquee_grob())
. Include at least three different types of formatting.
<- "
text # Headings are important
However, *noone* wants to miss a `nice piece of code`
```
1 + 1
#> [1] 2
```
**WOW**
"
::grid.draw(marquee_grob(text)) grid
Exercise 3.2.2
Create a marquee text that uses custom spans to change text colors, sizes, and add superscripts or subscripts. Try to show that styles inherits from their parent by placing spans inside spans
Custom spans are added using the {.<class> ...}
syntax. If <class>
is a valid color name (from colors()
) or a hex color code (in which case the preceeding .
is changed to a #
) than the given color is applied to the span. If <class>
is an integer then that text size is applied to the span
<- "
text Styling {.red can be {.20 nested}}. This is *also {#123456 true}* when you mix
custom and standard syntax
"
::grid.draw(marquee_grob(text)) grid
Exercise 3.2.3
Create a marquee text that includes either an image (like a plot) or a table. If using a plot, create a simple scatterplot first, then include it in your marquee text.
Use the standard markdown image syntax but provide the name of a variable holding a plot, grob, or table instead of the path/URL to an image.
library(gt)
<- "2010-06-07"
start_date <- "2010-06-14"
end_date <- sp500[sp500$date >= start_date & sp500$date <= end_date, 1:6] |>
tbl gt() |>
tab_header(
title = "S&P 500",
subtitle = glue::glue("{start_date} to {end_date}")
|>
) fmt_currency() |>
fmt_date(columns = date, date_style = "wd_m_day_year") |>
fmt_number(columns = volume, suffixing = TRUE)
<- "
text `{gt}` is much more powerful than any markdown table syntax!
*Just look at this:*

Pretty neat, huh!
"
::grid.draw(marquee_grob(text)) grid
3.3 marquee styling
Exercise 3.3.1
Read the documentation of marquee::classic_style()
and experiment with how different a look you can get by only modifying the arguments that it takes
As well as code blocks
>And notes
"
grid::grid.draw(marquee_grob(text, style = style))
No wrong answers here. Just experiment with the various arguments and its effect on the provided text
Exercise 3.3.2
Change the look of inline code so that it is displayed with an outlined serif font on a transparent background with a lightblue border. You can read the documentation for marquee_parse()
to see the tags that are asigned to various markdown elements.
Inline code uses the "code"
tag. See the documentation for style()
to figure out the different styles that needs to be changed to get the requested look
There are two ways to call modify_style()
. Either pass the different styles directly:
<- classic_style(base_size = 20) |>
style modify_style(
"code",
family = "serif",
color = "white",
background = NA,
border = "lightblue",
border_size = trbl(grid::unit(1, "pt")),
outline = "black"
)
or pass in a style object as the first argument after the tag name
<- classic_style(base_size = 20) |>
style modify_style(
"code",
style(
family = "serif",
color = "white",
background = NA,
border = "lightblue",
border_size = trbl(grid::unit(1, "pt")),
outline = "black"
) )
Can you see what the difference is between the two approaches (it’s described in the documentation for modify_style()
)
Exercise 3.3.3
Define a new tag ("fancy"
), which makes text over-the-top unreadable due to its styling. Consider using the features
argument as a means to that end.
Test out the new style on a piece of text of your own choice
Font features are a huge subject and if you have never heard of it before you may choose to use other arguments to make the text over-the-top. If you do use the features
argument, remember that not all fonts support all features. You can see which features are supported by a font using textshaping::get_font_features()
An example of a horrible style could be
::require_font("EB Garamond")
systemfonts<- classic_style(base_size = 20) |>
style modify_style(
"fancy",
family = "EB Garamond",
weight = "ultrabold",
features = systemfonts::font_feature(
swsh = 0,
dlig = 1,
hlig = 1,
hist = 1,
kern = 0,
aalt = 2
), outline = "black",
color = "white",
background = "green",
tracking = 300
)
<- "{.fancy This is a test of readability (& style)}" text
but I’m sure you can do it better/worse
3.4 Using geom_marquee() in ggplot2
Exercise 3.4.1
Create a scatter plot of bill length vs flipper length from the penguins dataset. Use geom_marquee()
to label at least two points with formatted text (including italics or bold). Experiment with the width
aesthetic to force automatic line breaking
You can use geom_marquee()
with some coordinates to place formatted text labels. To choose points to label, you might want to filter the penguins data for specific individuals.
# Find some interesting points to label
<- subset(penguins, species == "Adelie" & bill_len > 45)[1, ]
adelie <- subset(penguins, species == "Gentoo" & flipper_len > 230)[1, ]
gentoo <- rbind(adelie, gentoo)
penguins2 $label <- c(
penguins2"*Adelie* penguin with **large bill**",
"*Gentoo* penguin with **long flippers**"
)
ggplot(penguins, aes(bill_len, flipper_len)) +
geom_point(aes(color = species)) +
geom_marquee(
aes(label = label),
data = penguins2,
width = grid::unit(3, "cm"),
hjust = 0,
vjust = 1
+
) theme(legend.position = "bottom")
Exercise 3.4.2
Create a plot and use element_marquee()
to customize the plot title, subtitle, and axis titles. Make the titles include formatted text (like bold or italic words) and use a custom font.
You can use element_marquee()
to replace standard text elements in the theme. For example, plot.title = element_marquee(...)
for the title. Remember to use markdown formatting in your text.
::require_font("Spectral")
systemfonts::require_font("Exo 2")
systemfonts
ggplot(penguins, aes(bill_len, flipper_len)) +
geom_point(aes(color = species)) +
labs(
title = "**Penguin** *Measurements*",
subtitle = "Analysis of *bill length* vs *flipper length*",
x = "*Bill Length* (mm)",
y = "*Flipper Length* (mm)"
+
) theme(
plot.title = element_marquee(family = "Spectral", size = 16),
plot.subtitle = element_marquee(family = "Exo 2", size = 12),
axis.title.x = element_marquee(family = "Exo 2", size = 10),
axis.title.y = element_marquee(family = "Exo 2", size = 10)
)
Exercise 3.4.3
Create a scatter plot with a color aesthetic for species. Use guide_marquee()
to create a custom legend that includes text describing the penguin species colouring the species name and includind the keys as well.
You need to take care of two things:
- Tell ggplot2 to use
guide_marquee()
for the color aesthetic - Provide the markdown text that will be used for the legend
The two things can be accomplished in different ways
- The guide type can be specified with
guides()
or withinscale_color_*()
- The text can be provided in the
guide_marquee()
constructor, in the scale constructor, or withlabs()
<- "
legend_text An overview of the relationship between flipper and bill length between
{.Adelie *Adelie*} <<Adelie>>, {.Chinstrap *Chinstrap*} <<Chinstrap>>, and
{.Gentoo *Gentoo*} <<Gentoo>> penguins
"
ggplot(penguins, aes(bill_len, flipper_len, color = species)) +
geom_point() +
guides(color = "marquee") +
labs(color = legend_text)