auditR
グラフに注釈を入れる
ggplot()によって描いたグラフに、ちょっとした説明を入れたり、変数の値を追記したりするときに、あの手この手のうちannotate()を使う。
cars %>% ggplot() + aes(x = speed, y = dist) + geom_point() + annotate(geom = "text", x = 10, y = 100, label = "cars")
第一変数の"text"は注釈が文字列であることを示す。
(x, y)の値は、グラフの座標に用いられているものがそのまま。少し試行錯誤が必要。
labelに、実際に加えたい注釈の文字列を入れる。
グラフの特定領域を長方形で囲む
gg + annotate(geom = "rect", xmin = 10, ymin = 10, xmax = 50, ymax = 50, fill = "yellow", alpha = 0.2) + annotate(geom = "text", x = 55, y = 55, label = "囲んだところの説明", hjust = 0) #テキストの左にポイント0、右にポイント1
- 数式
オプションに、parse=TRUEを入れると、label=""のなかにTeX数式を入れることができる。
- 参考
- 関連
- geom_text() - 図の要素に関連したテキストを追加
Last updated 2021-02-12 | auditR (c) N.Nawata