ggplot2 1.1.0を使ってみる

そろそろ使ってみてね、とhadleyがアナウンスしていました。

が、変更点がめちゃくちゃ多くて追いきれません。たぶん、この変更点を追うよりは、ggplot2 bookで一から学びなおす方がいいんじゃないでしょうか...。

とかいいつつmajor changeを途中まで見たのでとりあえずメモっておきます。詳しくは原文↓を読んでください。

github.com

Geomを指定しないときの挙動

  • ggplot no longer throws an error if you your plot has no layers. Instead it automatically adds geom_blank() (#1246).

これは、

ggplot(iris(x = Species))

とかで、何のgeomも指定しないとこれまではエラーになってましたが、空っぽのプロットを出力する、という挙動に変わりました。まあうれしいことも困ることもないでしょう。

cut_width()

  • New cut_width() is a convenient replacement for the verbose plyr::round_any(), with the additional benefit of offering finer control.

cut_width()というcut()の強化版の関数が増えました。(cut_interval()cut_number()というのもあるけどこれは昔から?)

  • cut_width(): 指定した長さの区間に分割
  • cut_inteval(): 指定した個数の区間に分割(各区間の長さが同じ)
  • cut_number(): 指定した個数の区間に分割(各区間に含まれる個数がだいたい同じ個数になるように)

という感じです。

geom_count()

  • New geom_count() is a convenient alias to stat_sum(). Use it when you have overlapping points on a scatterplot. stat_sum() now defaults to using counts instead of proportions.

geom_count()は、geom_point(stat = "sum")エイリアスです。散布図の点がかぶっているとき、同じ座標にあるデータの個数に応じて点の大きさが変わります。

f:id:yutannihilation:20151016132040p:plain:w400

ちょっとでもずれてるとダメなので、数値データよりはカテゴリカルなデータのときに有用な気がします。

geom_curve()

  • New geom_curve() adds curved lines, with a similar specification to geom_segment() (@veraanadi, #1088).

曲線を引けるらしいです。

df <- data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 15.0)
ggplot(df) +
  geom_curve(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "curve")) +
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "segment"))

f:id:yutannihilation:20151016143258p:plain:w400

曲線の曲がり具合はcurvatureという変数でコントロールできます。(これはaes()マッピングはできないみたいです)

df <- data_frame(x = 1, y = 1, xend = 2, yend = 2)
ggplot(df) +
  lapply(
    seq(-1, 1, 0.1),
    function(c) geom_curve(aes(x = x, y = y, xend = xend, yend = yend),
                           curvature = c)
  )

f:id:yutannihilation:20151016144143p:plain:w400

stat_count()

  • geom_bar() now has it's own stat, distinct from stat_bin() which was also used by geom_histogram(). geom_bar() now uses stat_count() which counts values at each distinct value of x (i.e. it does not bin the data first). This can be useful when you want to show exactly which values are used in a continuous variable.

geom_bar()のstatがstat_bin()からstat_count()に変更されました。stat_count()は、連続値の場合でも離散値の場合でも、そのX値のデータ数をカウントしてくれます。

df <- data.frame(x = rep(c(2.9, 3.1, 4.5), c(5, 10, 4)))
ggplot(df, aes(x)) + geom_bar()

f:id:yutannihilation:20151016145103p:plain:w400

geom_point()stroke

  • geom_point() gains a stroke aesthetic which controls the border width of shapes 21-25 (#1133, @SeySayux). size and stroke are additive so a point with size = 5 and stroke = 5 will have a diameter of 10mm. (#1142)

geom_point()で、点の輪郭(というんでしょうか?)の太さをコントロールできるようになりました。

ggplot(mtcars, aes(wt, mpg)) +
    geom_point(shape = 21, colour = "black", fill = "white", size = 5, stroke = 5)

f:id:yutannihilation:20151016150010p:plain:w400

fillcolourが設定できるかは点の種類によります。ggplot2 specsというVignetteに点の種類を一覧で出すコードが載っていますが、このうち21-25の点にのみ使えます。

f:id:yutannihilation:20151016154222p:plain:w300

position_nudge()

  • New position_nudge() allows you to slightly offset labels (or other geoms) from their corresponding points (#1109).

位置を微妙にずらしてくれます。ラベルをつけたいときとかに便利です。これまでは、aes(x = value + 1)のように手動でずらしていましたが、positionにこれを設定すれば多少楽になりそうです。

df <- data.frame(
  x = c(1,3,2,5),
  y = c("a","c","d","c")
)

p1 <- ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y))

p2 <- ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y), position = position_nudge(y = -0.1))

gridExtra::grid.arrange(p1, p2)

f:id:yutannihilation:20151016155053p:plain:w400

nudge_x, nudge_yというパラメータで指定することもできます。

p2 <- ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y), nudge_y = -0.1)

scale_size()の挙動変更

  • scale_size() now maps values to area, not radius. Use scale_radius() if you want the old behaviour (not recommended, except perhaps for lines).

sizeマッピングされるのが、点の半径ではなく面積になりました。半径にマッピングしたいときは、scale_radius()を使います。

p1 <- ggplot(mpg, aes(displ, hwy, size = hwy)) +
  geom_point()

p2 <- ggplot(mpg, aes(displ, hwy, size = hwy)) +
  geom_point() +
  scale_radius()

gridExtra::grid.arrange(p1, p2)

f:id:yutannihilation:20151016155824p:plain:w400

stat_summary_bin()

  • New stat_summary_bin() works like stat_summary() but on binned data. It's a generalisation of stat_bin() that can compute any aggregate, not just counts (#1274).

stat_bin()の、summarizeする関数を自分で関数を選べる版、みたいなやつです。

ggplot(data.frame(x=1:100, y=1:100), aes(x = x)) +
  stat_summary_bin(aes(y = y), fun.y = "mean", geom = "bar", binwidth = 10)

f:id:yutannihilation:20151016160503p:plain

引数のチェックがより厳しくなった

  • Layers are now much stricter about their arguments - you will get an error if you've supplied an argument that isn't an aesthetic or a parameter. This is likely to cause some short-term pain but in the long-term it will make it much easier to spot spelling mistakes and other errors (#1293).

これはちょっと不安...。「バージョンあげたら動かなくなった!!!」みたいな悲鳴が続出する気がします。

あと、

This change does break a handful of geoms/stats that used `...` to pass 
additional arguments on to the underlying computation. Now 
`geom_smooth()`/`stat_smooth()` and `geom_quantile()`/`stat_quantile()` 
use `method.args` instead (#1245, #1289); and `stat_summary()` (#1242), 
`stat_summary_hex()`, and `stat_summary2d()` use `fun.args`.

と書いてあったんですが、よく理解できなかったのでパスします。

感想

変更点多すぎて力尽きました...。あとは気になるところがあればちょこちょこブログに書いていこうと思います。