散佈圖

ggplot2 資料視覺化入門

Rick Scavetta

Founder, Scavetta Academy

48 種幾何圖層

geom_*
abline contour dotplot jitter pointrange ribbon spoke
area count errorbar label polygon rug step
bar crossbar errorbarh line qq segment text
bin2d curve freqpoly linerange qq_line sf tile
blank density hex map quantile sf_label violin
boxplot density2d histogram path raster sf_text vline
col density_2d hline point rect smooth
ggplot2 資料視覺化入門

常見圖形類型

圖形類型 可用 Geom
散佈圖 points、jitter、abline、smooth、count
ggplot2 資料視覺化入門

散佈圖

  • 每個 geom 都有對應的美術映射,例:geom_point():
必要
x,y
ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width)) + 
  geom_point()

ggplot2 資料視覺化入門

散佈圖

  • 每個 geom 都有對應的美術映射,例:geom_point():
必要 選用
x,y alpha、color、fill、shape、size、stroke
ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width,
                 col = Species)) + 
  geom_point()

ggplot2 資料視覺化入門

各圖層的美術映射

# 兩者會產生相同的圖!
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + 
  geom_point()

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point(aes(col = Species))

可獨立控制每個圖層的美術映射:

ggplot2 資料視覺化入門
head(iris, 3) # 原始資料
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1  setosa          5.1         3.5          1.4         0.2
2  setosa          4.9         3.0          1.4         0.2
3  setosa          4.7         3.2          1.3         0.2
iris %>%
  group_by(Species) %>% 
  summarise_all(mean) -> iris.summary

iris.summary # 摘要統計
# A tibble: 3 x 5
  Species    Sepal.Length Sepal.Width Petal.Length Petal.Width
  <fct>             <dbl>       <dbl>        <dbl>       <dbl>
1 setosa             5.01        3.43         1.46       0.246
2 versicolor         5.94        2.77         4.26       1.33 
3 virginica          6.59        2.97         5.55       2.03
ggplot2 資料視覺化入門
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
  # 從 ggplot() 繼承資料與 aes 
  geom_point() + 
  # 使用不同的資料,但沿用繼承的 aes
  geom_point(data = iris.summary, shape = 15, size = 5)

ggplot2 資料視覺化入門

Shape 屬性值

ggplot2 資料視覺化入門

範例

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + 
  geom_point() +
  geom_point(data = iris.summary, shape = 21, size = 5, 
             fill = "black", stroke = 2)

ggplot2 資料視覺化入門

ggplot2 即時計算統計量

  • 統計圖層請見第二門課。
  • 注意:避免只畫平均數,應搭配離散程度(如標準差)。

ggplot2 資料視覺化入門

position = "jitter"

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + 
  geom_point(position = "jitter")

ggplot2 資料視覺化入門

geom_jitter()

geom_point(position = "jitter") 的捷徑

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + 
  geom_jitter()

ggplot2 資料視覺化入門

別忘了調整 alpha

  • 需要時,可把抖動與透明度一起用。
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + 
  geom_jitter(alpha = 0.6)

ggplot2 資料視覺化入門

空心圓也有幫助

  • shape = 1 為空心圓。
  • 通常不必另外調整透明度。
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + 
  geom_jitter(shape = 1)

ggplot2 資料視覺化入門

一起來練習吧!

ggplot2 資料視覺化入門

Preparing Video For Download...