Exercises for session 7
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.
7.1 Adding effects with ggfx
Use with_motion_blur() to add directional blur to a scatterplot of bill length vs. flipper length in the penguins dataset. Experiment with different values for the sigma and angle arguments to see how it affects the appearance.
You need to use with_motion_blur() to wrap around the geom_point() layer. The sigma parameter controls the amount of blur - try values between 1 and 5 using grid::unit() with ‘mm’ or ‘pt’ units. Angle gives the direction of the blur
ggplot(penguins, aes(x = bill_len, y = flipper_len, color = species)) +
with_motion_blur(
geom_point(size = 3),
sigma = unit(2, 'mm'),
angle = 35
)Create a plot using as_reference() and use with_blend() to create a masking effect. Make a density plot of bill depth vs. body mass, then punch holes in it where the data points are
You’ll need one layer inside an as_reference() and one layer inside a with_blend(). which layer you put in which depend on the value of flip_order in with_blend()
The "out" blend mode only draws the content where the destination is empty
ggplot(penguins, aes(x = bill_dep, y = body_mass)) +
as_reference(
geom_point(size = 2),
id = "points"
) +
with_blend(
geom_density_2d_filled(),
"points",
blend_type = "out"
)7.2 Working with ggforce
Use geom_shape() from ggforce to create a polygon plot with rounded corners. Start with the positions data from the slides and experiment with different radius values.
To create rounded corners with geom_shape(), use the radius parameter. Try setting it to different values like grid::unit(0.1, "npc") or grid::unit(5, "mm"). Larger values create more rounded corners.
If the radius get’s to large the shapes will disappear due to how it is implemented.
ggplot(positions, aes(x = x, y = y, group = id)) +
geom_shape(
aes(fill = id),
radius = unit(5, "mm")
)Use the mark geoms from ggforce (geom_mark_ellipse(), geom_mark_circle(), geom_mark_hull(), etc.) to highlight groups in a scatter plot. Add informative labels and descriptions to at least one of the groups.
Experiment with the various setings of the mark geom to alter its look
Use one of the geom_mark_*() functions like geom_mark_ellipse(), geom_mark_hull(), or geom_mark_rect().
In the aes(), use: - filter to select only certain species (e.g., filter = species == "Adelie") - label for a short title - description for additional information
You can also set parameters like fill, color, and alpha to control appearance.
If no label appears try to resize the plot or make the text smaller
base_plot +
geom_mark_ellipse(
aes(
filter = species == "Gentoo",
label = paste0(species, " Penguins"),
description = "Species with the longest flippers and shortest bill depth",
group = species
),
fill = "lightblue",
color = "blue",
alpha = 0.2
)7.3 Animating with gganimate
Create an animated scatter plot of bill length vs. flipper length using transition_states() to transition between different penguin species. Add appropriate enter and exit effects.
Use transition_states(species) to create transitions between different penguin species. Then add enter and exit effects like enter_fade(), enter_grow(), exit_fade(), or exit_shrink() to control how points appear and disappear during transitions.
You can also customize the animation speed using transition_length and state_length inside transition_states().
p_animated <- p +
transition_states(species, transition_length = 2, state_length = 1) +
enter_fade() +
exit_shrink()
p_animatedCreate a line plot that gradually reveals itself over time. Use the penguins dataset to show how body mass changes across years for different species. Add a title that shows how far along the reveal is in the animation.
Use transition_reveal() to make the line plot reveal itself over time.
Read the documentation to see which variables can be used in titles
p_animated <- p +
labs(title = "{frame_along}")
transition_reveal(year)
p_animated