Rのggplot2のスニペット(主に自分用)
主に自分用のメモです.ggplot2って,コマンドがややこしいので,いったん覚えてもすぐ忘れちゃうんですよね.なのでスニペットでもつくって楽したいですね,というのが今回の趣旨です.
基本的な使い方
まずggplot()の中で,使用するデータフレームや変数名を指定します.そして描画するグラフをgeom_XXX()で指定した上で,ggplot()の後に+でつないであげればOKです.具体的には,以下のような形です.
ggplot(data=mtcars, aes(x=wt, y=mpg))+geom_point()
あとは,ひたすら具体例をさらしていきます.今日の内容はそんだけ.
前準備
まずは,ggplot2パッケージをインストールしておきましょう.それに付随して,関連するパッケージもまとめて入れておくことにします.それが終わったらデータセットの用意ですが,今回はデフォルトで入ってるmtcarsデータセットとdiamondsデータセットを使うことにします.データセットの詳細はこちらとこちらを参照してください.
install.packages("ggplot2") install.packages("hexbin") install.packages("quantreg") library(ggplot2) library(hexbin) library(quantreg) library(reshape2) data(mtcars)
> head(mtcars) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 > head(diamonds) 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
グラフ一覧
散布図系
# 散布図 ggplot(data=mtcars, aes(x=wt, y=mpg))+geom_point()
# 折れ線グラフ ggplot(data=mtcars, aes(x=wt, y=mpg))+geom_line()
# 階段グラフ ggplot(data=mtcars, aes(x=wt, y=mpg))+geom_step()
# エリアプロット ggplot(data=mtcars, aes(x=wt, y=mpg))+geom_area()
# スムージング ggplot(data=mtcars, aes(x=wt, y=mpg))+geom_smooth()
# 隙間を埋めた四角形プロット ggplot(data=mtcars, aes(x=wt, y=mpg))+geom_bin2d()
# 隙間を埋めた六角形プロット ggplot(data=mtcars, aes(x=wt, y=mpg))+geom_hex()
# 四分位点 ggplot(data=mtcars, aes(x=wt, y=mpg))+geom_quantile()
カテゴリごとのプロット
# 箱ひげ図 ggplot(data=mtcars, aes(x=as.factor(gear), y=mpg))+geom_boxplot()
# ドットプロット ggplot(data=mtcars, aes(x=as.factor(gear), y=mpg))+geom_dotplot()
棒グラフ
# 棒グラフ ## stat="bin"とすると,サンプル数が自動的にy軸に設定される ggplot(data=diamonds, aes(x=cut)) + geom_bar(stat="bin")
# 積み上げ棒グラフ ## 積み上げ可能な形にreshapeパッケージのmelt()を使って整形 ## グルーピングを可視化するためにfillオプションを使用 d <- diamonds[, c("cut","x", "y", "z")] d <- melt(d) ggplot(data=d, aes(x=cut, y=value, fill=variable))+geom_bar(stat="identity")
# 横並び棒グラフ ## positionをdodgeにすればOK ggplot(data=d, aes(x=cut, y=value, fill=variable))+geom_bar(stat="identity", position="dodge")
ヒストグラム
# binwidthで幅を指定できる ggplot(data=diamonds, aes(x=x))+geom_histogram(binwidth=0.1)
# fillでグルーピングすれば複数のグラフを重ね合わせできる # 重なりを見やすくするため透明度を50%にする ggplot(data=d, aes(x=value, fill=cut))+geom_histogram(alpha=0.5, position="identity")+xlim(0, 10)
## 複数グラフをならべる形にすることで,melt(0でデータを加工しなくてもよくなる ggplot(data=diamonds, aes(x=x))+geom_histogram(binwidth=0.1, alpha=0.5)+geom_histogram(aes(x=y), binwidth=0.1, alpha=0.5)+xlim(0,10)
# 縦軸を密度表示にできる ggplot(data=diamonds, aes(x=x, y=..density..))+geom_histogram(binwidth=0.1)
# 密度曲線を追加できる ## 見やすくするため,ヒストグラムを半透明にした ggplot(data=diamonds, aes(x=x, y=..density..))+geom_histogram(binwidth=0.1, alpha=0.5)+geom_density()
グラフの装飾あれこれ
グラフの分割
# 横分割 ## facet_gridオプションを指定 d <- diamonds[, c("cut","x", "y", "z")] d <- melt(d) ggplot(data=d, aes(x=cut, y=value, fill=variable))+geom_bar(stat="identity", position="dodge")+facet_grid(variable~.)
# 縦分割 ggplot(data=d, aes(x=cut, y=value, fill=variable))+geom_bar(stat="identity", position="dodge")+facet_grid(.~variable)
# グリッド ggplot(data=d, aes(x=cut, y=value, fill=variable))+geom_bar(stat="identity", position="dodge")+facet_wrap(~variable, nrow=2, ncol=2)
グラフの並べ方
# 積み上げ ggplot(data=d, aes(x=value, fill=cut))+geom_histogram(alpha=0.5, position="stack")+xlim(0, 10)
# 割合 ggplot(data=d, aes(x=value, fill=cut))+geom_histogram(alpha=0.5, position="fill")+xlim(0, 10)
# 横並び ggplot(data=d, aes(x=value, fill=cut))+geom_histogram(alpha=0.5, position="dodge")+xlim(0, 10)
# 独立 ggplot(data=d, aes(x=value, fill=cut))+geom_histogram(alpha=0.5, position="identity")+xlim(0, 10)
グラフの重ね合わせ
# 基本的には,どんどんgeom_XXXを+でつないでいけばよいだけ ## data=XXXと書くことで,2個目以降のグラフに違うデータフレームを使うことも可能 ggplot(data=diamonds, aes(x=x))+geom_histogram(binwidth=0.1, alpha=0.5)+geom_histogram(aes(x=y), binwidth=0.1, alpha=0.5)+xlim(0,10)
タイトルや表示範囲
# ラベル + xlab("X軸のラベル") + ylab("Y軸のラベル") + labs(x="X軸のラベル", y="Y軸のラベル") # 軸の表示範囲 + xlim(1, 10) + ylim(0, 100) # 凡例の位置変更 + theme(legend.position = c(0.95, 0.95)) + theme(legend.position = "top") + theme(legend.position = "right") + theme(legend.position = "bottom") + theme(legend.position = "left") + theme(legend.position = "none")