座標レイヤー

ggplot2 中級データ可視化

Rick Scavetta

Founder, Scavetta Academy

座標レイヤー

  • プロットの寸法を制御
  • coord_
    • 例: coord_cartesian()
ggplot2 中級データ可視化

拡大表示

  • coord_cartesian(xlim = ...)
  • scale_x_continuous(limits = ...)
  • xlim(...)
ggplot2 中級データ可視化

元のプロット

iris.smooth <- ggplot(
  iris, 
  aes(x = Sepal.Length, 
      y = Sepal.Width, 
      color = Species)
  ) + 
  geom_point(alpha = 0.7) +
  geom_smooth()

iris.smooth 

ggplot2 中級データ可視化

scale_x_continuous()

iris.smooth + 
  scale_x_continuous(limits = c(4.5, 5.5))
Removed 95 rows containing non-finite values 
(stat_smooth).
Removed 95 rows containing missing values 
(geom_point).

ggplot2 中級データ可視化

scale_x_continuous()

元のプロット

scale_x_continuous()で拡大

元データの一部が除外されます。

ggplot2 中級データ可視化

xlim()

iris.smooth +
  xlim(c(4.5, 5.5))
Removed 95 rows containing non-finite values 
(stat_smooth).
Removed 95 rows containing missing values 
(geom_point).

ggplot2 中級データ可視化

coord_cartesian()

iris.smooth +
  coord_cartesian(xlim = c(4.5, 5.5))

ggplot2 中級データ可視化

アスペクト比

  • 縦横比
  • 誤認に注意
  • 普遍的な標準は未確立
  • 同一スケールなら通常は1:1
ggplot2 中級データ可視化

黒点

library(zoo)
sunspots.m <- data.frame(
    year = index(sunspot.month),
    value = reshape2::melt(sunspot.month)$value)
)

ggplot(sunspots.m, aes(x = year, y = value)) +
  geom_line() +
  coord_fixed() # 既定は1:1のアスペクト比

ggplot2 中級データ可視化

黒点

ggplot(sunspots.m, aes(x = year, y = value)) +
  geom_line() +
  coord_fixed(0.055)

ggplot2 中級データ可視化

Passons à la pratique !

ggplot2 中級データ可視化

Preparing Video For Download...