分类 x 轴
在之前的图表中,我们看到腮腺炎直到 1999 年才开始被报告,因此在此之前的比较没有意义。
现在将数据过滤为仅包含 1999 年及之后报告的病例,然后制作一个堆叠条形图,按地区查看不同疾病所占的比例。
修改数据处理管道,使数据达到所需的结构,然后构建并绘制堆叠条形图即可!不用像上一个练习那样对条形进行排序。有没有看到让您意外的模式?
本练习是课程的一部分
R 中的可视化最佳实践
练习说明
- 将
who_disease过滤为仅包含 1999 年及之后的年份。 - 在
group_by()中添加分组,以在汇总中保留region信息。 - 在映射美学中填写
x = region、y = total_cases、fill = disease。
交互式实操练习
通过完成这段示例代码来试试这个练习。
disease_counts <- who_disease %>%
# Filter to on or later than 1999
filter(___) %>%
mutate(disease = ifelse(disease %in% c('measles', 'mumps'), disease, 'other')) %>%
group_by(disease, ___) %>% # Add region column to grouping
summarise(total_cases = sum(cases))
# Set aesthetics so disease is the stacking variable, region is the x-axis and counts are the y
ggplot(disease_counts, aes(___)) +
# Add a column geometry with the proper position value.
___(___)