plotly / plotly/plotly.R

Problems with barcharts with barmode='overlay' and Date objects

Đang mở
#1,516 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ả

I need to plot several barcharts with Date objects on the x-axis with barmode = 'overlay' for a whole year. When there are enough values to plot, everything works fine, as I would like the bars to have equal widths along all traces.

But when one trace has few elements, the width of those traces are expanded.
And when one trace only has 1 value, it is not shown at all.

I could overcome those problems by creating fictional Dates with 0 y-values for the rest of the year, but I was hoping there is a better way of solving that.

The variable n in the example can be used to determine the amount of bars for the problematic
trace.

library(plotly)
n = 3

## All days of 2019 ###############
Days2019 <- data.frame(
  date = seq(as.Date("2019-01-01"), as.Date("2019-12-31"), by = "days")
)

## Sample n Days of 2019 ###############
df <- data.frame(
  date = sort(sample(Days2019$date, size = n, replace = F)),
  val = sample(1:100, n, replace = T)
)

## Merge fictional 0 y-Values #################
# df = merge(df, Days2019, by="date", all=T)
# df[is.na(df$val),]$val=0

## Sample 30 Days of 2019 ###############
holiday1 <- data.frame(
  date = sort(sample(Days2019$date, size = 30, replace = F))
)

## Plot Plotly ###############
maxY = max(df$val)
plot_ly() %>%
  add_trace(data = holiday1, x = ~date, y = maxY, marker = list(color = "gray"), type="bar", name="Holidays") %>% 
  add_trace(data = df, x = ~date, y = ~val, marker = list(color = "red"), type="bar", name="Values") %>% 
  plotly::layout(autosize = F,
                 barmode = 'overlay',
                 bargap = 0.05,
                 xaxis = list(
                   type = "date",
                   range = c(as.Date("2019-01-01")-1,
                             as.Date("2019-12-31")+1),
                   autorange = FALSE,
                   tickmode = "auto",
                   nticks = 14L,
                   automargin = F)
  )

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 bằng cách chạy ví dụ R được cung cấp với n=3, sau đó thay đổi n và so sánh độ rộng của các thanh cũng như khả năng hiển thị ở chế độ overlay giữa hai trace. Theo dõi cách plotly.R xử lý bar và Date được ví dụ này sử dụng, và coi issue là hoàn tất khi các trace Date thưa vẫn giữ được độ rộng phù hợp và một giá trị đơn được render mà không cần các ngày giả có giá trị bằng không.

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
35/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.