メモ:ggplot2のstat_contourとかstat_ellipse用のダミーデータ

これを調べるときにちょっとつまづいたのでメモ。

notchained.hatenablog.com

こんな感じのデータをダミーとして使ってました。

library(ggplot2)

d <- data.frame(x = 1:10, y = 1:10, z = 1:10)
ggplot(d, aes(x, y)) + stat_identity()

f:id:yutannihilation:20150629002710p:plain

けどこれは、以下のようなWarningが出て何も表示されません。

ggplot(d, aes(x, y, z = z)) + stat_contour()

#> Warning message:
#> Not possible to generate contour data 
ggplot(d, aes(x, y)) + stat_ellipse()
#> Warning message:
#> Computation failed in `stat_ellipse()`:
#> missing value where TRUE/FALSE needed 

contourとかellipseの性格上、もうちょっとgrid状のデータじゃないとダメみたいでした。

set.seed(4649)
d2 <- data.frame(x = rep(1:10, 10), y = rep(1:10, each = 10), z = sample(1:2, 100, replace = TRUE))
ggplot(d2, aes(x, y, z = z)) + stat_contour()
ggplot(d2, aes(x, y)) + geom_point() + stat_ellipse()

f:id:yutannihilation:20150629003452p:plain

f:id:yutannihilation:20150629003616p:plain