美化点图

在 Tidyverse 中用数据进行沟通

Timo Grossenbacher

Data Journalist

在 Tidyverse 中用数据进行沟通

因子水平

  • 因子水平的顺序决定了在 ggplot2 中的显示顺序。
ilo_data$country
Austria        Belgium        Czech Rep.     Finland       
France         Germany        Hungary    ...
 ...
 17 Levels: Austria Belgium Czech Rep. Finland France ... United Kingdom
在 Tidyverse 中用数据进行沟通

用 forcats 包重排因子

  • 需用 library(forcats) 加载
  • fct_drop 删除水平
  • fct_rev 反转因子水平
  • fct_reorder 重新排序。

1 了解更多:tidyverse.org (http://forcats.tidyverse.org/)
在 Tidyverse 中用数据进行沟通

fct_reorder 函数

ilo_data
# A tibble: 34 x 4
      country   year hourly_compensation working_hours
        <fct>  <fct>               <dbl>         <dbl>
 1    Austria   1996               24.75      31.99808
 2    Austria   2006               30.46      31.81731
 3    Belgium   1996               25.25      31.65385
 4    Belgium   2006               31.85      30.21154
ilo_data <- ilo_data  %>% 
    mutate(country = fct_reorder(country, working_hours, mean))
ilo_data$country
17 Levels: Netherlands Norway Germany Sweden ... Czech Rep.
在 Tidyverse 中用数据进行沟通

在 Tidyverse 中用数据进行沟通

用 hjust 和 vjust 微调标签

ggplot(ilo_data) +
  geom_path(aes(...)) +
  geom_text(
        aes(...,
            hjust = ifelse(year == "2006", 
              1.4, 
              -0.4)
        )
    )
在 Tidyverse 中用数据进行沟通

开始练习!

在 Tidyverse 中用数据进行沟通

Preparing Video For Download...