优化柱状图与散点图

R 中的可视化最佳实践

Nick Strayer

Instructor

拥挤的柱状图

who_disease %>% 
  filter(region == 'EMR', disease == 'measles', year == 2015) %>% 
  ggplot(aes(x = country, y = cases)) +
    geom_col()
R 中的可视化最佳实践

R 中的可视化最佳实践

翻转柱状图

  • geom_bar()geom_col() 不支持在 y 轴放类别
    busy_bars <- who_disease %>% 
    filter(region == 'EMR', disease == 'measles', year == 2015) %>% 
    ggplot(aes(x = country, y = cases)) +
    geom_col() 
    
  • 因此需要翻转!
busy_bars + coord_flip() # 交换 x 与 y 轴!
R 中的可视化最佳实践

R 中的可视化最佳实践

过多网格

  • 柱状图不需要平行网格线
  • 散点图只需与点位置对齐的网格

R 中的可视化最佳实践

R 中的可视化最佳实践

去除纵向网格

plot <- who_disease %>% 
  filter(country == "India", year == 1980) %>% 
  ggplot(aes(x = disease, y = cases)) +
    geom_col()
# Remove vertical grid lines
 plot + theme(
    panel.grid.major.x = element_blank()
  )
R 中的可视化最佳实践

R 中的可视化最佳实践

为散点图使用更亮背景

  • 默认灰色背景对散点对比度偏低
  • 使用 theme_minimal() 可快速变亮
  • 放大点也有帮助
who_subset %>% 
  ggplot(aes(y = reorder(country, cases_2016), x = log10(cases_2016))) +
  # Point size increased
  geom_point(size = 2) +
  # Theme minimal for light background
  theme_minimal()
R 中的可视化最佳实践

R 中的可视化最佳实践

Let's try it out!

R 中的可视化最佳实践

Preparing Video For Download...