# A tibble: 26 × 5
# Groups: country [26]
country iso2c iso3c year hour_unpaid
<chr> <chr> <chr> <int> <dbl>
1 Belgium BE BEL 2013 10.1
2 Canada CA CAN 2016 9.58
3 Chile CL CHL 2015 9.85
4 Colombia CO COL 2017 2.93
5 Estonia EE EST 2010 10.8
6 Finland FI FIN 2010 10.5
7 France FR FRA 2010 9.49
8 Germany DE DEU 2013 10.4
9 Greece GR GRC 2014 7.01
10 Hungary HU HUN 2010 7.98
# … with 16 more rows
Q4 left_join()
Merge the two data frames you got in the last question.
Code
data <- mean_fertility |>left_join(latest_unpaid, by ="country")data
# A tibble: 37 × 6
country fertility iso2c iso3c year hour_unpaid
<chr> <dbl> <chr> <chr> <int> <dbl>
1 Australia 1.82 <NA> <NA> NA NA
2 Austria 1.47 <NA> <NA> NA NA
3 Belgium 1.72 BE BEL 2013 10.1
4 Canada 1.58 CA CAN 2016 9.58
5 Chile 1.71 CL CHL 2015 9.85
6 Colombia 1.87 CO COL 2017 2.93
7 Czechia 1.57 <NA> <NA> NA NA
8 Denmark 1.74 <NA> <NA> NA NA
9 Estonia 1.60 EE EST 2010 10.8
10 Finland 1.64 FI FIN 2010 10.5
# … with 27 more rows
ggplot2
Q5 geom_point()
Plot a scatter plot of male hours of unpaid work and fertility rate. To make it better, try
Fit a linear line
Change the coordinates
Pick another theme
Code
data |>ggplot(aes(x = hour_unpaid, y = fertility)) +geom_point() +stat_smooth(formula ='y ~ x',method ="lm", se =FALSE) +coord_cartesian(ylim =c(1.1, 2.3)) +theme_minimal()
Q6 (Extra) ggrepel::geom_text_repel()
Put country labels on data points. It is known that ggrepel::geom_text_repel() works better than the original geom_text().
Code
data |>ggplot(aes(x = hour_unpaid, y = fertility, label = country)) +geom_point() + ggrepel::geom_text_repel() +stat_smooth(formula ='y ~ x',method ="lm", se =FALSE) +coord_cartesian(ylim =c(1.1, 2.3)) +theme_minimal()