plotly / plotly/plotly.R

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

Đang mở
#2,326 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
R
Star
2.7k
Fork
641
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu từ các điểm vào add_trace và add_lines trong R plotting API và tái hiện ví dụ hồi quy iris được cung cấp. So sánh cách hai hàm xử lý các scatter trace với mode = 'lines', sau đó xác minh rằng việc thay thế add_lines bằng add_trace tạo ra các đường khoảng tin cậy tuần tự và phần tô đầy giống nhau.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
r
Lĩnh vực
data-visualization
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.