In this practical, you will visualize the development of wealth in Basel across time.
tidyverse,
patchwork, and the taxation.csv data.library(tidyverse)
library(patchwork)
basel <- read_csv('1_data/taxation.csv')
year. Save the result as avg_wealth.avg_wealth <- basel %>%
group_by(year) %>%
summarize(avg_wealth_mean = mean(wealth_mean),
avg_wealth_median = mean(wealth_median),
avg_wealth_gini = mean(wealth_gini))
ggplot() by piping the
avg_wealth data set into the function’s data
argument.avg_wealth %>%
ggplot()
mapping argument and the aes()
helper function. Place year on the x-axis and
avg_wealth_mean on the y-axis.avg_wealth %>%
ggplot(mapping = aes(x = year,
y = avg_wealth_mean))
geom_point().avg_wealth %>%
ggplot(mapping = aes(x = year,
y = avg_wealth_mean)) +
geom_point()
geom_line() help emphasize the trend?avg_wealth %>%
ggplot(mapping = aes(x = year,
y = avg_wealth_mean)) +
geom_point() + geom_line()
geom_smooth() would be an even better choice? Try
adding it instead of geom_line(). You can set
se = FALSE inside geom_smooth(), to omit the
confidence interval.avg_wealth %>%
ggplot(mapping = aes(x = year,
y = avg_wealth_mean)) +
geom_point() + geom_smooth(se = FALSE)
geom_point() and either geom_line() or
geom_smooth() for avg_wealth_median. Inside
these additional geoms specify that they should show
avg_wealth_median on the y-axis using the
aes(). Note that for geoms the first argument usually is
mapping, which is why we can omit its name.avg_wealth %>%
ggplot(mapping = aes(x = year,
y = avg_wealth_mean)) +
geom_point() + geom_line() +
geom_point(aes(y = avg_wealth_median)) +
geom_line(aes(y = avg_wealth_median))
avg_wealth to
the long format. Let the new variables be called "variable"
and "value". Save the result as
avg_wealth_long.avg_wealth_long <- avg_wealth %>%
pivot_longer(c(avg_wealth_mean, avg_wealth_median, avg_wealth_gini),
names_to = "variable",
values_to = "value")
avg_wealth_long and facet_wrap() to
create a plot that illustrates the development for all three variables
with points and lines. To do this, value must go on the
y-axis and variable must be used as the faceting
variable.avg_wealth_long %>%
ggplot(mapping = aes(x = year,
y = value)) +
geom_point() + geom_line() +
facet_wrap(~ variable)
ggplot tries to use the same scales for the different
facets. Set scales = "free" inside the
facet_wrap function. Depending on your screen layout you
may also want to set ncol = 1 to have the facets sit on top
of each other.avg_wealth_long %>%
ggplot(mapping = aes(x = year,
y = value)) +
geom_point() + geom_line() +
facet_wrap(~ variable, scales = "free", ncol = 1)
patchwork package to create a plot with rows where the
development of the Gini takes up the entire upper row, while the other
two metrics share the lower row. To do this, first create three
different plots, one for each of the three panels.gini_plot <- avg_wealth_long %>%
filter(variable == "avg_wealth_gini") %>%
ggplot(mapping = aes(x = year,
y = value)) +
geom_point() + geom_line()
mean_plot <- avg_wealth_long %>%
filter(variable == "avg_wealth_mean") %>%
ggplot(mapping = aes(x = year,
y = value)) +
geom_point() + geom_line()
median_plot <- avg_wealth_long %>%
filter(variable == "avg_wealth_median") %>%
ggplot(mapping = aes(x = year,
y = value)) +
geom_point() + geom_line()
patchwork syntax to compose the plot.gini_plot / (mean_plot + median_plot)
plot_annotation(tag_levels = "A") so that
the labels are lettered according to the alphabet.gini_plot / (mean_plot + median_plot) + plot_annotation(tag_levels = "A")