最佳實務:長條圖

ggplot2 中級資料視覺化

Rick Scavetta

Founder, Scavetta Academy

本章重點

  • 資料視覺化常見陷阱
  • 最佳表達方式
    • 有效的說明(溝通),以及
    • 有效的探索(研究)圖表
ggplot2 中級資料視覺化

長條圖

  • 兩種類型
    • 絕對值
    • 分佈
ggplot2 中級資料視覺化

哺乳類睡眠

Observations: 76
Variables: 3
$ vore  <chr> "carni", "omni", "herbi", "omni", "herbi", "herbi", "carni", …
$ total <dbl> 12.1, 17.0, 14.4, 14.9, 4.0, 14.4, 8.7, 10.1, 3.0, 5.3, 9.4, …
$ rem   <dbl> NA, 1.8, 2.4, 2.3, 0.7, 2.2, 1.4, 2.9, NA, 0.6, 0.8, 0.7, 1.5…
ggplot2 中級資料視覺化

Dynamite 圖

d <- ggplot(sleep, aes(vore, total)) +
 # ...

d + 
  stat_summary(fun = mean,
               geom = "bar",
               fill = "grey50") +
  stat_summary(fun.data = mean_sdl,
               fun.args = list(mult = 1),
               geom = "errorbar", 
               width = 0.2)

ggplot2 中級資料視覺化

個別資料點

# position
posn_j <- position_jitter(width = 0.2)

# plot
d +
  geom_point(alpha = 0.6, 
             position = posn_j)

ggplot2 中級資料視覺化

geom_errorbar()

d +
  geom_point(...) +
  stat_summary(fun = mean, 
               geom = "point", 
               fill = "red") +
  stat_summary(fun.data = mean_sdl, 
               fun.args = list(mult = 1), 
               geom = "errorbar", 
               width = 0.2, 
               color = "red")

ggplot2 中級資料視覺化

geom_pointrange()

d +
  geom_point(...) +
  stat_summary(fun.data = mean_sdl, 
               mult = 1, 
               width = 0.2, 
               color = "red")

ggplot2 中級資料視覺化

無資料點

d +
  stat_summary(fun = mean, 
               geom = "point") +
  stat_summary(fun.data = mean_sdl, 
               fun.args = list(mult = 1), 
               geom = "errorbar", 
               width = 0.2)

ggplot2 中級資料視覺化

不一定要用長條

ggplot2 中級資料視覺化

一起來練習吧!

ggplot2 中級資料視覺化

Preparing Video For Download...