美しいペアプロット図を簡単に作る

MikuHatsune2013-04-12

図があまりにもきれいにしかも簡単に作れすぎて美しいペアプロット図を簡単に作るという記事をそのままパクる。
GGallyというパッケージでかんたんにかつかっこよくdata.frameのプロットができる。

library(GGally)
data(tips, package="reshape")
ggpairs(tips)
ggpairs(tips, upper = "blank") # 上三角行列部分をプロットしない。
# プロットの仕方は密度、分布、点、平滑化などあるようだ。
ggpairs(
	tips,
	upper = list(continuous = "density", combo = "box"),
	lower = list(continuous = "points", combo = "dot")
	)
# data.frame 内で factor 化されていたら color で色付けできるっぽい。
# continuous は 'points', 'smooth', 'density', 'cor'
# combo は 'box', 'dot', 'facethist', 'facetdensity', 'denstrip' から選ぶ。
ggpairs(
	tips,
	upper = list(continuous = "density", combo = "box"),
	lower = list(continuous = "smooth", combo = "denstrip"),
	color = "smoker"
	)



data(diamonds, package="ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1],200),]
ggpairs(
	diamonds.samp[,1:3],
	upper = list(continuous = "density", combo = "box"),
	lower = list(continuous = "points", combo = "dot"),
	color = "cut",
	title = "Diamonds"
	)
ggpairs(tips[,1:3], axisLabels="none") # 軸ラベルを描かない。


ggpairs(mtcars[,c("mpg","wt","cyl")], upper = "blank", title = "Custom Example")
# Custom Examples
custom_car <- ggpairs(mtcars[,c("mpg","wt","cyl")], upper = "blank", title = "Custom Example")
# ggplot example taken from example(geom_text)
plot <- ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars)))
plot <- plot + geom_text(aes(colour=factor(cyl)), size = 3) + scale_colour_discrete(l=40)
custom_car <- putPlot(custom_car, plot, 1, 2)
custom_car <- putPlot(custom_car, ggally_text("ggpairs allows you\nto put in your\nown plot.\nLike that one.\n <---"), 1, 3)
custom_car