6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ggplot2によるグラフ描画覚書

Last updated at Posted at 2018-03-21

背景

Oreillyの「Rグラフィックスクックブック」をもとにグラフ描画方式の習得を行っていたところ、ggplot2のバージョンが上がったためか、本の記述と実際の動作する命令とに差が出てきました。
備忘録として動作する命令をまとめることにします。

環境

  • OS: Windows 10(64bit)
  • R: Ver3.4.4
  • ggplot2: Ver2.2.1

棒グラフ

qplot()では、statがdeprecatedになりました。
そこで、geom="col"を使用します。

棒グラフ.R
# エラー
> qplot(BOD$Time, BOD$demand, geom="bar", stat="identity")
Error: stat_count() must not be used with a y aesthetic.
In addition: Warning message:
`stat` is deprecated 

# 書き直し後
> qplot(BOD$Time, BOD$demand, geom = "col")

「棒グラフのグループ化」では、geom_bar()の引数statのデフォルト値が"count"であることで本の通りではエラーとなります。
明示的に"identity"を指定します。

棒グラフグループ化.R
# エラー
> ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) + geom_bar(position = "dodge")
Error: stat_count() must not be used with a y aesthetic.

# 書き直し後
> ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) + geom_bar(stat="identity", position = "dodge")

積み上げ棒グラフの積み上げ順序を入れ替えるとき、エステティック属性のorder指定はdeprecateされました。
代わりに、forcatsパッケージのfct_rev指定を使用します。
凡例名を合わせるため、labs(fill="")を使用しています。

積み上げ順序.R
# 積み上げ入れ替え前
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) + geom_bar(stat = "identity")

# クックブックの記載
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar, order=desc(Cultivar))) + geom_bar(stat = "identity")

# 書き直し後
library(forcats)
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=fct_rev(Cultivar))) + geom_bar(stat = "identity") + labs(fill="Cultivar")
積み上げ入れ替え前 クックブックの記載(順序が変わっていない) 書き直し後
image.png image.png image.png

関数曲線

上に同じくqplot()でのstatがdeprecatedとなったのを受けて、書き方が変わります。
が、この書き直しは関数曲線っぽくないかもしれません。

関数曲線.R
> myfun <- function(xvar) {
  1 / (1 + exp(-xvar + 10))
}

# エラー
> qplot(c(0, 20), fun=myfun, stat="function", geom="line")
Warning: Ignoring unknown parameters: fun
Error: geom_line requires the following missing aesthetics: y
In addition: Warning message:
`stat` is deprecated 

# 書き直し後
> x<-c(0:20)
> qplot(x, myfun(x)) + geom_line()

散布図

散布図で使われることの多いと思われるフィッティング。
stat_smooth()を使う際のオプションの指定方法が変わっています。
methodで指定した関数に渡す引数を、method.argsオプションでリストオブジェクト形式で指定します。

フィッティング.R
# 事前準備
> library(MASS)
> b <- biopsy
> b$classn[b$class=="benign"] <- 0
> b$classn[b$class=="malignant"] <- 1

# エラー
> ggplot(b, aes(x=V1, y=classn)) +
  geom_point(position = position_jitter(width = 0.3, height = 0.06), alpha=0.4, shape=21, size=1.5) +
  stat_smooth(method = glm, family=binomial)
Warning: Ignoring unknown parameters: family

# 書き直し後
> ggplot(b, aes(x=V1, y=classn)) +
  geom_point(position = position_jitter(width = 0.3, height = 0.06), alpha=0.4, shape=21, size=1.5) +
  stat_smooth(method = glm, method.args = list(family="binomial"))

ファセット

サブグラフを複数組み合わせて1つのグラフを構築する方法としてファセットがあります。
ファセットのテキストラベルを書き替える方法として、levels()関数が用いられていますが、それではうまく書き換わりませんでした。

ファセットラベル名.R
# 事前準備
mpg2 <- mpg

# ファセットラベル名を変更できなかった方法
levels(mpg2$drv)[levels(mpg2$drv)=="4"] <- "4wd"
levels(mpg2$drv)[levels(mpg2$drv)=="f"] <- "Front"
levels(mpg2$drv)[levels(mpg2$drv)=="r"] <- "Rear"
ggplot(mpg2, aes(x=displ, y=hwy)) + geom_point() + facet_grid(drv ~ .)

# ファセットラベル名を変更できた方法
mpg2$drv[factor(mpg2$drv)=="4"] <- "4wd"
mpg2$drv[factor(mpg2$drv)=="f"] <- "Front"
mpg2$drv[factor(mpg2$drv)=="r"] <- "Rear"
ggplot(mpg2, aes(x=displ, y=hwy)) + geom_point() + facet_grid(drv ~ .)
クックブックの記載(ラベル名変更なし) 書き直し後
Facet_before.png Facet_after.png
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?