簡體   English   中英

圖例不會出現 Plotly 跟蹤

[英]Legend Won't Appear for Plotly Trace

我的 plot 不會顯示我使用“add_markers”添加的符號。 我希望圖例出現在 plot 的右側,但由於某種原因,它沒有出現。

library(plotly)
library(tidyverse)

my_data = data.frame(
  Y_LABEL = c("Label1", "Label2"),
  START = c(66, 72),
  END = c(20, 28), 
  COLOR_VAR = c("Color1", "Color2"),
  SYMBOL_TIME = c(7.2, 7.2)
)


my_plot <- plot_ly() %>%
  add_segments(data=my_data, 
               x=~START, xend=~END, y=~Y_LABEL, yend=~Y_LABEL,
               color=~COLOR_VAR, line=list(width=9), showlegend=F)


my_plot = my_plot %>% add_markers(x = ~SYMBOL_TIME, y = "Symbol", 
                                  showlegend = T, inherit = F,
                                  marker=list(symbol = "diamond", 
                                              size = 9, 
                                              color = "white", 
                                              line = list(color = "blue", 
                                                          width = 1)),
                                  name = "My Symbol")

您可以使用layout function 為相應的跡線添加圖例,如下所示:

library(plotly)
library(tidyverse)

my_plot <- plot_ly() %>%
  add_segments(data=my_data, 
               x=~START, xend=~END, y=~Y_LABEL, yend=~Y_LABEL,
               color=~COLOR_VAR, line=list(width=9), showlegend=F) %>%
  add_markers(x = ~SYMBOL_TIME, y = "Symbol", 
              showlegend = T, inherit = T,
              marker=list(symbol = "diamond", 
                          size = 9, 
                          color = "white", 
                          line = list(color = "blue", 
                                      width = 1)),
              name = "My Symbol") %>%
  layout(showlegend = T)

my_plot

創建於 2023-01-28,使用reprex v2.0.2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM