ggpmisc

グラフ・図の中に数式を埋め込む

回帰直線などを描画したときに、回帰式やR^2(決定係数)の値を説明として加えなければならないが、別々にするよりはグラフの中に同居させると、グラフの一人歩きを防ぐ意味もある。

ggpmiscパッケージはggplotを補完する目的で作られたもので、ggplotで作図したオブジェクトに+演算子で追加できる。

ggplot() + data(cars) + aes(x = speed, y = dist) + geom_point() + geom_smooth(method = "lm", formula = y~x)  -> smoothlm
smoothlm + ggpmisc::stat_poly_eq(
 formula = y ~ x,
 aes(label = str_c(stat(eq.label), stat(rr.label), stat(p.value.label), sep = "~~~")),
 parse = TRUE
 )
参考