Learn to Live and Live to Learn

IT(たまにビジネス)に関する記事を読んで、考えて、使ってみたことをまとめる場。

Rでデータの視覚化【棒グラフ編】

棒グラフ

barplot(データ, biside=, horiz= ,col=, legend= )

※データは必須。

Rが用意してくれているデータを読み込んどきます(=data関数)。

> data(VADeaths)

一番ベーシックな書き方。

> barplot(VADeaths)

f:id:A_01:20131222205018p:plain

biside=TRUEだとこんな感じ。

> barplot(VADeaths,beside=TRUE)

f:id:A_01:20131222205028p:plain
列における行が別々の棒グラフになって表示されます。

カスタマイズ

> barplot(VADeaths,beside=TRUE,col=c("lightblue","mistyrose","lightcyan","lavender","cornsilk"),legend=rownames(VADeaths))
> title(main="Death Rates in Virginia",font=5)

f:id:A_01:20131222205037p:plain
colで棒の色指定、legendのrownamesで凡例を設定。titleで棒グラフに名前を付けます。

注意)横軸(Rural MaleやUrban Female)が出てこないなーと思ったときは棒グラフの出力されているウインドウの大きさを変えてみてください。

http://www1.doshisha.ac.jp/~mjin/R/05.html