plotly / plotly/plotly.R

`add_trace` with `type = 'scatter', mode = 'lines'` works differently from `add_lines`

Offen
#2,326 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
R
Sterne
2.7k
Forks
641
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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:
add_lines_works

Plot 2:
add_trace_doesnt_work

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne bei den Einstiegspunkten add_trace und add_lines in der R plotting API und reproduziere das bereitgestellte iris-Regressionsbeispiel. Vergleiche, wie die beiden Funktionen Streuungstraces mit mode = 'lines' behandeln, und überprüfe anschließend, dass das Ersetzen von add_lines durch add_trace dieselben sequenziellen Konfidenzintervall-Linien und dieselbe Füllung erzeugt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
r
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.