2017-05-05 18 views
0

R(バージョン3.4)でplotly(バージョン4.6.0)を使用して、信頼区間のある2行を作成しています。伝説は表示されません。誰もが何が起こっていると思いますか?ここで2つの信頼区間の凡例が表示されないR

は、プロットです: Output from plotly without legend

伝説のスイッチが無視されているようです。塗りつぶされた(信頼区間)は真であり、主なプロットは真である。それらをすべて真にすると、6つの凡例が得られますが、私は2つだけ必要です。

は、ここでは、コードです:

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は常に真でなければ、それ以外の場合は、トレースを交換しようと、他人をマスクします。

この例で問題 (https://plot.ly/r/legend/)を示しplotlyウェブサイトから取る

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

聞いてうれしい –

関連する問題