カーネル密度推定とヒートマップ
サーモグラフィみたいなヒートマップをggplot2で描きたい。
たとえば以下のirisデータのプロット。
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+ geom_point(color = "black")
点が密集しているところを赤く、薄いところを緑で塗りたい。
こういう時はstat_density2dを使う。
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+ stat_density2d(aes(fill=..level..), geom="polygon") + scale_fill_continuous(low = "green", high = "red") + geom_point(color = "black")
カーネル密度推定した結果を色分けしてくれる。