tata色々な備忘録

データ解析、画像解析、化学分析などなど

ggplot2メモ書き(ヒストグラム)

データの可視化を目的に、ヒストグラムの検討。

表現を変えて見比べてみる。

まずはデータ取り込みと準備。 fillでグルーピング設定をする。

require(reshape)
require(ggplot2)
syukei <- read.table("clipboard",header=TRUE)
syukei.m <- melt(syukei,id="camera_No")
#fill オプションでグルーピング項目を設定
p1 <- ggplot(syukei.m, aes(x = camera_No, y = value,fill=variable))

……個人データでテストしているので、お試しするならこちら(iris)。

require(reshape)
require(ggplot2)
data(iris)
names(iris) = gsub("\\.", "", names(iris))
iris.m <- melt(iris,id="Species")
#fill オプションでグルーピング項目を設定
iris_test1 <- ggplot(iris.m, aes(x = Species, y = value,fill=variable))

1. 積み上げ

alphaの値は透過性を示す。

#show_guide=FALSEで凡例を非表示
p2 <- p1 + geom_histogram(stat="identity",alpha=0.8,show_guide = FALSE)
p2

f:id:tatabox2000:20130610001357p:plain

2. 分割

fact_grid(.~ variable) で、variable各々のグラフを描画

詳細はここ http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/

p2 <- p1 + geom_histogram(stat="identity",alpha=0.8,show_guide = FALSE)
p3 <- p2 + facet_grid(. ~ variable)
p3

f:id:tatabox2000:20130610014246p:plain

3. 同時比較

position="dodge" で同時比較。ちょっと分かり難いかな。

#position="dodge"で同時表示になる。
p2 <- p1 + geom_histogram(stat="identity",alpha=0.8,show_guide =FALSE,position="dodge")
p2

f:id:tatabox2000:20130610020604p:plain

散布図行列等と使い分けながら可視化したい。