自定义 ggplot2 误差边界图
在上一个练习中,您应该已经发现图表存在一些问题。由于各县未排序,读者很难从数据中看出模式。具体来说,误差边界图在按顺序排列点时效果更好,因为排序可以帮助读者将估计值的不确定性与其他估计进行对比理解。此外,缺少图表格式也会让读者难以把握图表的内容。在本练习中,您将清理 ggplot2 代码,绘制一个更美观、更易读的误差边界图。
本练习是课程的一部分
在 R 中分析美国人口普查数据
练习说明
- 使用 tidyverse 的
str_replace()函数,从县名中移除" County, Maine"。 - 在图中按家庭收入中位数的降序重新排列各县。
- 使用
subtitle将副标题设置为 "Counties in Maine",为所有数据点补充共同信息。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Remove unnecessary content from the county's name
maine_inc2 <- maine_inc %>%
mutate(NAME = ___(NAME, " County, Maine", ""))
# Build a margin of error plot incorporating your modifications
ggplot(maine_inc2, aes(x = estimate, y = ___(NAME, estimate))) +
geom_errorbarh(aes(xmin = ___ - moe, xmax = estimate + ___)) +
geom_point(size = 3, color = "darkgreen") +
theme_grey(base_size = 14) +
labs(title = "Median household income",
___ = "Counties in Maine",
x = "ACS estimate (bars represent margins of error)",
y = "") +
scale_x_continuous(labels = scales::dollar)