分面圖層

ggplot2 中級資料視覺化

Rick Scavetta

Founder, Scavetta Academy

分面(Facets)

  • 簡單實用
  • 小倍圖概念
    • 由 Edward Tufte 推廣
    • Visualization of Quantitative Information,1983
ggplot2 中級資料視覺化

ggplot2 中級資料視覺化

ggplot2 中級資料視覺化

iris.wide

p <- ggplot(iris.wide, aes(x = Length, 
                           y = Width, 
                           ccolorol = Part)) +
  geom_jitter(alpha = 0.7) +
  scale_color_brewer(palette = "Set1") +
  coord_fixed()

p

ggplot2 中級資料視覺化

iris.wide 與 facet_grid()

p <- ggplot(iris.wide, aes(x = Length, y = Width, color = Part)) +
  geom_jitter(alpha = 0.7) +
  scale_color_brewer(palette = "Set1") +
  coord_fixed()
p + facet_grid(cols = vars(Species))

ggplot2 中級資料視覺化

公式表示法

p <- ggplot(iris.wide, aes(x = Length, y = Width, color = Part)) +
  geom_jitter(alpha = 0.7) +
  scale_color_brewer(palette = "Set1") +
  coord_fixed()
p + facet_grid(. ~ Species)

ggplot2 中級資料視覺化

iris.wide2

ggplot(iris.wide2, aes(x = Part, y = setosa, color = Measure)) +
  geom_jitter()
ggplot(iris.wide2, aes(x = Part, y = versicolor, color = Measure)) +
  geom_jitter()  
ggplot(iris.wide2, aes(x = Part, y = virginica, color = Measure)) +
  geom_jitter()

ggplot2 中級資料視覺化

iris.tidy

ggplot(iris.tidy, aes(x = Measure, y = Value, color = Part)) +
  geom_jitter() +
  facet_grid(cols = vars(Species))

ggplot2 中級資料視覺化

iris.tidy 錯誤的分面方式:

ggplot(iris.tidy, aes(x = Measure, 
                      y = Value, 
                      color = Part)) +
  geom_jitter() +
  facet_grid(rows = vars(Species))

ggplot2 中級資料視覺化

其他選項

  • 依列與欄切分
ggplot2 中級資料視覺化

一起來練習吧!

ggplot2 中級資料視覺化

Preparing Video For Download...