为图形添加分面
在这里,您将按视频中的讲解,使用 facet_grid() 为 1996 年和 2006 年各添加一个水平分面。
使用 facet_grid() 时,最好通过具名参数来指定子图的排列方式:rows = ... 或 cols = ...。这样可以避免混淆,因为 facet_grid() 还接收许多其他参数。
本练习是课程的一部分
在 Tidyverse 中用数据进行沟通
练习说明
- 使用
facet_grid()和合适的参数,生成两个水平排列的散点图。 - 左侧图展示 1996 年的数据,右侧图展示 2006 年的数据。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Again, you save the plot object into a variable so you can save typing later on
ilo_plot <- ggplot(ilo_data, aes(x = working_hours, y = hourly_compensation)) +
geom_point() +
labs(
x = "Working hours per week",
y = "Hourly compensation",
title = "The more people work, the less compensation they seem to receive",
subtitle = "Working hours and hourly compensation in European countries, 2006",
caption = "Data source: ILO, 2017"
) +
# Add facets here
___(___)
ilo_plot