散布図・箱ひげ図・有意差・回帰式から、レイアウト・座標変換・内部構造まで
ヒント
ハンズオンブックでは、ブラウザで R と Python を タブで切り替えて 実行できます
ggplot2
データ・対応づけ(aes)・幾何・統計・尺度・座標・ファセット・テーマを + で足す
aes() の中、seaborn は hue=geom_point(colour = "steelblue")、seaborn は color=aes(colour = "blue") は “blue” という群 を作ってしまう(よくある誤り)人数が少なければ 点を見せる。箱に点を重ねるときは 箱の外れ値を消す(outlier.shape = NA / showfliers=False)
| 誤差棒 | 表すもの | 人数が増えると |
|---|---|---|
| SD | 1 羽ごとのばらつき | 変わらない |
| SE = SD / √n | 平均の不確かさ | 小さくなる |
| 95% CI | 平均がありそうな範囲 | 小さくなる |
errorbar= と seed= を指定| 群の位置 | dodge 2 群のずれ | |
|---|---|---|
| ggplot2 | 1, 2, 3 | ± 0.75 / 4 |
| seaborn | 0, 1, 2 | ± 0.2 |
ggsignif に test = で検定させると 補正されない → annotations = で記号を渡す
seaborn / matplotlib
アスタリスクは「閾値より小さいか」しか伝えない → p 値・差と信頼区間 も検討
geom_smooth(method = "lm")(指定しないと曲線)/ sns.regplot()(帯はブートストラップ)x = -Inf, y = Inf + hjust/vjust、matplotlib は transform=ax.transAxesitalic(R)^2、parse = TRUE)、matplotlib は mathtext($R^2$)expansion(mult = c(0.05, 0.35)) / ax.set_ylim())geom_smooth() / regplot() の帯は 直線(平均)の 95% 信頼区間
predict(interval = "prediction") / get_prediction().summary_frame())theme_classic(base_size = 8)、ggsave(width = 85, height = 65, units = "mm", device = cairo_pdf)figsize=(85/25.4, 65/25.4)、plt.rcParams["pdf.fonttype"] = 42(文字を編集可能に埋め込む)font_manager.fontManager.addfont()patchwork:| 横、/ 縦、guides = "collect"、tag_levels = "A"、& で全パネルに
plt.subplot_mosaic([["A","B"],["C","C"]], layout="constrained")
ylim() はデータを捨てるylim() は範囲外のデータを 捨ててから 箱ひげ図を計算 → ジェンツーの中央値 5000 → 4700coord_cartesian()(matplotlib の set_ylim() も表示だけ)layer_data() で統計変換の結果を見る、after_stat() で使う| 座標 | 指定 |
|---|---|
| データ | ax.transData(既定) |
| パネル (0〜1) | ax.transAxes |
| 図全体 (0〜1) | fig.transFigure |
| x はデータ・y はパネル | ax.get_xaxis_transform() |
| 点からのずれ | textcoords="offset points" |
図は Artist の木:ax.patches・ax.lines・ax.collections を後から変更。凡例は handle と label のリスト
seaborn.objects(so.Plot().add(so.Dot()))で、Python でも文法の考え方で書ける
coord_cartesian())