In this practical, you will in different ways animate a plot showing the link between wealth and income in Basel in the year 2017.
tidyverse and
the taxation.csv data.library(tidyverse)
basel <- read_csv('1_data/taxation.csv')
gganimate and plotly
packages.library(gganimate)
plot <- basel %>%
arrange(year, desc(wealth_mean)) %>%
mutate(quarter = as_factor(quarter)) %>%
ggplot(aes(x = wealth_mean,
y = income_mean,
col = quarter)) +
geom_point(show.legend = F,
size=4) +
labs(x = "Wealth",
y = "Income") +
theme_minimal() +
scale_color_viridis_d()
transition_states(year) to
the plot. This will start a process during which first all individual
images are created and then the images are joined in a single .gif file,
which will be displayed in the Plots tab. Know that this may take a few
seconds to a minute.plot +
XX
ggtitle(label = "XX", subtitle = "XX") to add a
title and subtitle to the plot. In either title or subtitle, add
{closest_state} directly inside the character string, so
that the changing states will be shown.plot +
XX +
XX
transition_length = 10 and state_length = 0.
As a result, you should get a smoother, more fluid animation.plot +
XX(XX, XX) +
XX
shadow_wake() to add wakes to your animation.plot +
XX(XX, XX) +
XX +
XX
anim_save(), to save your animation as a .gif to
your harddrive. To do this, first store the animation in an object. Make
sure to add the extension .gif to your filename. If you
don’t specify a folder, you should find your animation inside of your
project folder.anim <- plot +
XX(XX, XX) +
XX +
XX
anim_save(filename = XX, animation = XX)
ggplotly, to turn the base plot into a
plotly package and then run it. Inspect the plot and
observe whether the hover information includes all relevant
information.p <- XX(XX)
p
Adjust the base plot to include
text = paste0("Quarter: ", quarter,"<br>Year: ", year,"<br>Income: ", income_mean))
in the aes() function. Save the plot again as
plot.
Use the updated plot in ggplotly and verify that the
hover information has been updated.
p <- XX(XX)
p