2017-05-05 117 views
0

我在R(版本3.4)中使用plotly(版本4.6.0)在它們周圍創建兩個置信區間的線。傳說沒有顯示。任何人都有猜測發生了什麼?Plotly在R中沒有顯示兩個置信區間的圖例

這裏的情節: Output from plotly without legend

看來,傳說開關被忽略。對於填充(置信區間)而言是錯誤的,對於主要情節則是正確的。把它們全部變爲真實給出了六個圖例條目,但我只想要兩個。

下面的代碼:

plot_ly(x = ~observed$time, y = ~observed$interval_upper, 
    type = 'scatter', 
    mode = 'lines', 
    line = list(color = 'transparent'), 
    showlegend = FALSE, 
    name = 'Upper bound') 

    %>% add_trace(x = ~observed$time, y = ~observed$interval_lower, 
    type = 'scatter', 
    mode = 'lines', 
    fill = 'tonexty', 
    fillcolor='rgba(255,127,14,0.2)', 
    line = list(color = 'transparent'), 
    showlegend = FALSE, 
    name = 'Lower bound') 

    %>% add_trace(x = ~observed$time, y = ~observed$observed_power, 
    type = 'scatter', 
    mode = 'lines', 
    line = list(color='rgb(255,127,14)'), 
    showlegend = TRUE, 
    name = 'Observed') 

    %>% add_trace(x = ~forecast$time, y = ~forecast$interval_upper, 
    type = 'scatter', 
    mode = 'lines', 
    line = list(color = 'transparent'), 
    showlegend = FALSE, 
    name = 'Upper bound') 

    %>% add_trace(x = ~forecast$time, y = ~forecast$interval_lower, 
    type = 'scatter', 
    mode = 'lines', 
    fill = 'tonexty', 
    fillcolor='rgba(31,119,180,0.2)', 
    line = list(color = 'transparent'), 
    showlegend = FALSE, 
    name = 'Lower bound') 

    %>% add_trace(x = ~forecast$time, y = ~forecast$baseline_power, 
    type = 'scatter', 
    mode = 'lines', 
    line = list(color='rgb(31,119,180)'), 
    showlegend = TRUE, 
    name = 'Forecast') 

    %>% layout(legend = list(x = 0.80, y = 0.90)) 

回答

1

在plot_ly第一showlegend應始終是TRUE,否則會掩蓋別人,儘量換的痕跡。

這個例子從拿網站plotly顯示問題 (https://plot.ly/r/legend/

library(plotly) 
library(tidyr) 
library(plyr) 

data <- spread(Orange, Tree, circumference) 
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5")) 

#hiding entries 

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1') %>% 
    add_trace(y = ~Tree2, name = 'Tree 2') %>% 
    add_trace(y = ~Tree3, name = 'Tree 3', showlegend = FALSE) %>% 
    add_trace(y = ~Tree4, name = 'Tree 4') %>% 
    add_trace(y = ~Tree5, name = 'Tree 5') 

##no legend 

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1',showlegend = FALSE) %>% 
    add_trace(y = ~Tree2, name = 'Tree 2') %>% 
    add_trace(y = ~Tree3, name = 'Tree 3', showlegend = TRUE) %>% 
    add_trace(y = ~Tree4, name = 'Tree 4') %>% 
    add_trace(y = ~Tree5, name = 'Tree 5') 
+0

謝謝。那就是訣竅。 – fifthace

+0

很高興聽到這一點 –