調整長條圖與點圖

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()
# 移除垂直格線
 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))) +
  # 加大點的尺寸
  geom_point(size = 2) +
  # 淺色背景的 theme_minimal
  theme_minimal()
R 視覺化最佳實務

R 視覺化最佳實務

一起來練習吧!

R 視覺化最佳實務

Preparing Video For Download...