purrr 与散点图
由于 ggplot() 不接受列表作为输入,您可以将它与 purrr 搭配使用,仅用几行代码就能把列表转换为数据框,再绘制 ggplot() 图形。
本练习继续使用 gh_users 数据。您将使用一个 map_*() 函数提取若干具名元素,并将它们转换为正确的数据类型。然后创建一个散点图,将用户的粉丝数与公开仓库数进行对比。
本练习是课程的一部分
使用 purrr 掌握函数式编程基础
练习说明
- 对
gh_users使用map(),调用能创建数据框的map_*()函数,生成包含 4 列、列名为"login"、"name"、"followers"和"public_repos"的数据框。 - 将该数据框通过管道传入散点图,其中
x轴为followers,y轴为public_repos。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a data frame with four columns
map___(___, `[`,
c(___,___,___,___)) %>%
# Plot followers by public_repos
ggplot(.,
aes(x = ___, y = ___)) +
# Create scatter plots
geom____()