plotly / plotly/plotly.R

add_text() interferes with plot's color animation

Đang mở
#1,865 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 have a gauge plot (following this tutorial), with the color of the plot changing according to the current value. I also want an annotation* which displays the current value of the gauge (I don't want the standard value displayed in the sector).

I tried the following:

library(plotly)
library(RColorBrewer)

riskToHex <- function(x) {
  x <- colorRamp(rev(brewer.pal(11, "RdYlBu")))(x / 100)
  rgb <- paste(x[,1], x[,2], x[,3], sep = ",")
  paste0("rgb(", rgb, ")")
}

dd <- data.frame(values = c(90, 60, 20))

dd <- dd %>%
  mutate(colors = riskToHex(dd$values),
         frame = seq.int(nrow(dd)))
dd <- merge(dd, data.frame(values = 200 - dd$values,
                           colors = "white",
                           frame = dd$frame),
            all = TRUE) %>%
  arrange(frame)

plot_ly() %>%
  add_pie(values = dd$values,
          frame = dd$frame,
          rotation = -90,
          marker = list(colors = dd$colors),
          textinfo = "none",
          title = list(text = "Risk score",
                       font = list(size = 20)),
          hoverinfo = "skip",
          sort = FALSE,
          showlegend = FALSE,
          order = "clockwise",
          hole = 0.6) %>%
  add_markers(x = 0,
             y = c(0, 1),
             color = 'rgba(0,0,0,1)') %>%
  add_text(x = 0,
           y = 0.6,
           yshift = 100,
           text = filter(dd, colors != "white")$values,
           frame = distinct(dd, frame)[[1]],
           showlegend = FALSE,
           textfont = list(size = 20)) %>%
  animation_opts(frame = 500) %>%
  layout(
    xaxis = list(showgrid = FALSE,
                 zeroline = FALSE,
                 showticklabels = FALSE
                 ),
    yaxis = list(showgrid = FALSE,
                 zeroline = FALSE,
                 showticklabels = FALSE
    )
  )

This is rather messy and very hacky. I had to use add_text() because I couldn't figure out how to get annotations to work in this case with changing values in each frame (whether through add_annotations or layout(annotations = ...)). Unfortunately, the add_text() forced me to add the layout block disabling the axes (which otherwise start to appear). And since the pie chart is seemingly printed on paper space, I also had to add two invisible points so that I could actually position the text (otherwise the axes would always keep the text centered).

Regardless, this successfully generates an animated chart. However, the color only seems to change to the correct value on the following frame (by which time it's too late).

enter image description here

Howeverf, this only happens due to the add_text() block. If I comment that out, the colors on the bar work correctly.

enter image description here

Is there any explanation and solution to this?


* I'm open to other solutions!

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 với ví dụ R có thể tái hiện trong issue và so sánh hoạt ảnh có và không có add_text(). Điều tra lý do màu của biểu đồ tròn bị trễ khi có văn bản; được xem là hoàn thành khi màu của gauge và giá trị hiển thị được cập nhật trong cùng một frame mà không cần axis workaround.

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ó
3/5
Thời gian dự kiến
1-2 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
25/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.