ggplot2 中級データ可視化
Rick Scavetta
Founder, Scavetta Academy
一貫した描画領域を使わない理由:
| 変数型 | サブセットの内容 |
|---|---|
| 連続値 | 範囲が大きく異なる |
| カテゴリ | グループが異なる |
ggplot(msleep2, aes(bodywt_log,
brainwt_log)) +
geom_point(alpha = 0.6, shape = 16) +
coord_fixed() +
facet_grid(rows = vars(vore),
cols = vars(conservation))

ggplot(msleep2, aes(bodywt_log,
brainwt_log)) +
geom_point(alpha = 0.6, shape = 16) +
coord_fixed() +
facet_grid(rows = vars(vore),
cols = vars(conservation),
scales = "free_x")
Error: coord_fixed doesn't support free scales
ggplot(msleep2, aes(bodywt_log,
brainwt_log)) +
geom_point(alpha = 0.6, shape = 16) +
facet_grid(rows = vars(vore),
cols = vars(conservation),
scales = "free_x")

ggplot(msleep2, aes(bodywt_log,
brainwt_log)) +
geom_point(alpha = 0.6, shape = 16) +
facet_grid(rows = vars(vore),
cols = vars(conservation),
scales = "free_y")

ggplot(msleep2, aes(bodywt_log,
brainwt_log)) +
geom_point(alpha = 0.6, shape = 16) +
facet_grid(rows = vars(vore),
cols = vars(conservation),
scales = "free")

ggplot(msleep2, aes(x = bodywt_log,
y = name)) +
geom_point() +
facet_grid(rows = vars(vore))

ggplot(msleep2, aes(x = bodywt_log,
y = name)) +
geom_point() +
# y のスケールとスペースを可変に
facet_grid(rows = vars(vore),
scales = "free_y")

ggplot(msleep2, aes(x = bodywt_log,
y = name)) +
geom_point() +
# y のスケールとスペースを可変に
facet_grid(rows = vars(vore),
scales = "free_y",
space = "free_y")

msleep2 <- msleep2 %>%
# 体重の高い順に並べ替え
arrange(-bodywt_log) %>%
# 因子レベルをその順に再定義
mutate(name = as_factor(name))
# 新しい順序が y 軸に反映
ggplot(msleep2, aes(x = bodywt_log,
y = name)) +
geom_point() +
# y のスケールとスペースを可変に
facet_grid(rows = vars(vore),
scales = "free_y",
space = "free_y")

ggplot2 中級データ可視化