`add_trace` with `type = 'scatter', mode = 'lines'` works differently from `add_lines`
オープン
まだ誰も着手していません。
- 主要言語
- R
- スター
- 2.7k
- フォーク
- 641
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
When plotting a regression line with confidence intervals, add_trace with type = 'scatter', mode = 'lines' works differently from add_lines. The latter works while the former fails (see examples).
Looks like add_trace(type = 'scatter', mode = 'lines', ...) attempts to connect datapoints pairwise instead of sequentially.
# load packages
require(dplyr)
require(plotly)
# prep data
setosa <- iris |>
filter(Species == "setosa") |>
mutate(fit = predict(lm(Sepal.Width ~ Sepal.Length), interval = "conf")[, "fit"],
lwr = predict(lm(Sepal.Width ~ Sepal.Length), interval = "conf")[, "lwr"],
upr = predict(lm(Sepal.Width ~ Sepal.Length), interval = "conf")[, "upr"])
## Compare plots ##
# plot 1: add_lines works
plot_ly(data = setosa, x = ~Sepal.Length) |>
add_trace(y = ~Sepal.Width,
type = 'scatter',
mode = 'markers',
showlegend = FALSE) |>
add_lines(y = ~upr, # add_lines works
color = I("#2ca02c"),
name = 'CI upper') |>
add_lines(y = ~lwr, # add_lines works
color = I("#2ca02c"),
alpha = 0.2,
fill = 'tonexty',
name = 'CI lower') |>
add_trace(y = ~fit, # add_trace works
type = 'scatter',
mode = 'lines',
name = 'Predicted')
# plot 2: replaced add_lines with add_trace, no other changes
plot_ly(data = setosa, x = ~Sepal.Length) |>
add_trace(y = ~Sepal.Width,
type = 'scatter',
mode = 'markers',
showlegend = FALSE) |>
add_trace(y = ~upr, # add_trace doesn't work
type = 'scatter',
mode = 'lines',
color = I("#2ca02c"),
name = 'CI upper') |>
add_trace(y = ~lwr, # add_trace doesn't work
type = 'scatter',
mode = 'lines',
color = I("#2ca02c"),
alpha = 0.2,
fill = 'tonexty',
name = 'CI lower') |>
add_trace(y = ~fit, # here add_trace works as it should
type = 'scatter',
mode = 'lines',
name = 'Predicted')
Output
Plot 1:
Plot 2:
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
R plotting API の add_trace と add_lines のエントリポイントから始め、提供された iris 回帰例を再現します。2 つの関数が mode = 'lines' の散布トレースをどのように処理するかを比較し、その後、add_lines を add_trace に置き換えると同じ連続的な信頼区間の線と塗りつぶしが生成されることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- r
- 領域
- data-visualization
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100