ggplot2パッケージを使って、棒グラフを作成してみる-1
R グラフィックス クックブック 4回目
ggplot2パッケージを利用して、棒グラフを作成していきます。
連続値/離散値
軸が連続値か離散値かで描画のされ方が異なります。
データセットは、BODを利用します。
R: Biochemical Oxygen Demand
> BOD Time demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8
Time = 6がないです。
また、Time列は数値になっています。
'data.frame': 6 obs. of 2 variables: $ Time : num 1 2 3 4 5 7 $ demand: num 8.3 10.3 19 16 15.6 19.8 - attr(*, "reference")= chr "A1.4, p. 270"
軸にTime列を、軸にdemand列をマッピングします。
> bodMapping = ggplot(BOD,aes(x = Time, y = demand))
幾何オブジェクトに、棒グラフを指定します。
bodMapping + geom_bar(stat = "identity")
Time = 6に該当する棒がありませんが、
軸のTime列が、数値のため、連続値として扱われ、
最小値~最大値までの、全ての値に棒が生成されます。
連続値を、離散値として扱うためには、ファクタに変換します。
ファクタで、再度マッピングを行い、幾何オブジェクトに棒グラフを指定します。
bodMappingFactor = ggplot(BOD, aes(x = factor(Time), y = demand)) bodMappingFactor + geom_bar(stat = "identity")
Time = 6が描画されなくなりました。
棒の塗りつぶし色と、線の色を変更するには、
幾何オブジェクトの指定の際に、fill引数とcolour引数を指定します。
bodMapping + geom_bar(stat = "identity", fill="lightblue", colour = "blue")
塗りつぶし色にlightBlueを、線の色にblueを指定しています。
グループ化された棒グラフ
データを分類するために、軸に、カテゴリカル変数を追加します。
データセットは、ToothGrowthを利用します。
R: The Effect of Vitamin C on Tooth Growth in Guinea Pigs
> head(ToothGrowth) len supp dose 1 4.2 VC 0.5 2 11.5 VC 0.5 3 7.3 VC 0.5 4 5.8 VC 0.5 5 6.4 VC 0.5 6 10.0 VC 0.5
グループ化を行う際は、マッピングを指定する際に、fill引数を指定します。
tgMapping = ggplot(ToothGrowth,aes(x = factor(dose),y = len, fill=supp)) tgMapping + geom_bar(stat="identity",position = "dodge")
幾何オブジェクトの指定部分で、積み上げ棒グラフにされないために、
position = "dodge"としています。(積み上げ棒グラフは後ほど。)
さて、OJとVCの値ですが、それぞれ最大値が設定されています。
一意に決まらない場合は、勝手に最大値だけを表示してくれるみたいですね。
> max(ToothGrowth[ToothGrowth$dose == 0.5 & ToothGrowth$supp == "VC",]$len) [1] 11.5 > max(ToothGrowth[ToothGrowth$dose == 0.5 & ToothGrowth$supp == "OJ",]$len) [1] 21.5
このグループ化された棒グラフも、見た目を変更できます。
tgMapping + geom_bar(stat="identity",position = "dodge" ) + scale_fill_brewer(palette = "Pastel1")
scale_fill_brewerを追加しています。
グループ化された軸に該当するデータが無い場合を見てみます。
dose = 2 、supp = "VC"のデータを除きます。
> tgDat <- ToothGrowth[ToothGrowth$dose != 2 | ToothGrowth$supp != "VC",]
このデータでグループ化された棒グラフを作成していみます。
tgMapping_New = ggplot(tgDat,aes(x = factor(dose),y = len, fill=supp)) tgMapping_New + geom_bar(stat="identity",position = "dodge")
この様に、グループ化されたデータが無い場合、棒が太くなります。
ビミョウ。。。
個数を示す棒グラフ
1行毎に、個々の情報を持つ形式の場合、個数だけを表示したくなります。
この場合には、軸に何も指定する必要がありません。
データセットは、ggplot2パッケージに含まれるdiamondsを利用します。
diamonds. ggplot2 0.9.3.1
carat cut color clarity depth table price x y z 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
diaMap = ggplot(diamonds, aes(x=cut)) diaMap + geom_bar()
上の棒グラフの軸は、離散値です。
cut列がファクタです。
> str(diamonds) 'data.frame': 53940 obs. of 10 variables: $ carat : num 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ... $ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ... $ color : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ... $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ... $ depth : num 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ... $ table : num 55 61 65 58 58 57 57 55 61 61 ... $ price : int 326 326 327 334 335 336 336 337 337 338 ... $ x : num 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ... $ y : num 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ... $ z : num 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
連続値(数値)であるcaratを、軸に指定すると、
ggplot(diamonds, aes(x=carat)) + geom_bar()
この様に、該当するデータが無い軸も描画されます。
棒グラフに色を付ける
上でも、塗りつぶし色や線の色をつけてきましたが、色の付け方に注目してみます。
データは、下のデータを組み合わせて作ります。
R: US State Facts and Figures
- state.name
- state.abb
- state.division
- state.region
- state.x77
> stateInfo <- data.frame("Name" = state.name, "Abb" = state.abb, + "Division" = state.division, "Region" = state.region, "Income" = state.x77[, 2])
> head(stateInfo) Name Abb Division Region Income Alabama Alabama AL East South Central South 3624 Alaska Alaska AK Pacific West 6315 Arizona Arizona AZ Mountain West 4530 Arkansas Arkansas AR West South Central South 3378 California California CA Pacific West 5114 Colorado Colorado CO Mountain West 4884
stateMap = ggplot(stateInfo, aes(x=Abb, y=Income,fill = Division)) stateMap + geom_bar(stat = "identity", colour = "black")
軸が多すぎました。。。
ちょっとデータを少なくします。
> stateInfo <- stateInfo[1:10,] > stateInfo Name Abb Division Region Income Alabama Alabama AL East South Central South 3624 Alaska Alaska AK Pacific West 6315 Arizona Arizona AZ Mountain West 4530 Arkansas Arkansas AR West South Central South 3378 California California CA Pacific West 5114 Colorado Colorado CO Mountain West 4884 Connecticut Connecticut CT New England Northeast 5348 Delaware Delaware DE South Atlantic South 4809 Florida Florida FL South Atlantic South 4815 Georgia Georgia GA South Atlantic South 4091
再度、棒グラフを作成します。
stateMap = ggplot(stateInfo, aes(x=Abb, y=Income,fill = Division)) stateMap + geom_bar(stat = "identity", colour = "black")
何度みてもこの棒グラフの色使いが受け入れることができません。
そこで色を変更します。
まず、幾何オブジェクトの指定の部分を何度も記載するのがメンドクサイため、
オブジェクトに格納します。
> barGraph <- geom_bar(stat = "identity", colour = "black")
これで、下のコマンドで棒グラフが描画できます。
> stateMap + barGraph
塗りつぶしは、Divisionで分けようと思います。
scale_fill_manualを追加し、valuesにRGB指定の色をDivision数指定します。
stateMap + barGraph + scale_fill_manual(values = c("#669933","#FFCC66","#66CC33","#FFFF66","#666633","#FF9966"))
もちろん、塗りつぶし部分をオブジェクトに格納することも可能です。
(上のグラフと同じです。)
scallFill <- scale_fill_manual(values = c("#669933","#FFCC66","#66CC33","#FFFF66","#666633","#FF9966")) stateMap + barGraph + scallFill
軸が、Abbとなっていますので、パッと見で分かるように変更します。
軸の見出しの指定には、xlabを追加します。
> stateMap + barGraph + scallFill + xlab("StateAbbName")
軸の見出しが、StateAbbNameに変更されました。
もちろん、この軸の見出しの指定もオブジェクトに格納できます。
(上のグラフと同じです。)
xAxisDisplayName <- xlab("StateAbbName") stateMap + barGraph + scallFill + xAxisDisplayName
次の記事も、棒グラフですが、
軸に対して正負の値があるグラフや、積み上げ棒グラフを扱います。