基础堆叠条形图
在上一些练习中,我们用饼图和华夫图把所有年份的数据合并在一起进行查看。
现在,我们想看看这些模式随时间如何变化。为此,我们将绘制一个堆叠条形图,并将观测年份映射到 x 轴。与之前制作饼图时一样,我们会把数据简化为 measles、mumps 和 other 三类。
以这种方式可视化后,您是否发现数据里有些"不对劲"的地方?
本练习是课程的一部分
R 中的可视化最佳实践
练习说明
- 修改
aes()调用,将年份映射到 x 轴。 - 在
geom_col()中调整position参数,使柱形填满整个 y 轴。
交互式实操练习
通过完成这段示例代码来试试这个练习。
disease_counts <- who_disease %>%
mutate(disease = ifelse(disease %in% c('measles', 'mumps'), disease, 'other')) %>%
group_by(disease, year) %>% # note the addition of year to the grouping.
summarise(total_cases = sum(cases))
# add the mapping of year to the x axis.
ggplot(disease_counts, aes(___, y = total_cases, fill = disease)) +
# Change the position argument to make bars full height
geom_col(___)