fifthace
fifthace

Reputation: 546

Plotly not showing legend for two confidence intervals in R

I am using plotly (version 4.6.0) in R (version 3.4) to create two lines with confidence intervals around them. The legend is not showing. Anyone have a guess what's up?

Here's the plot: Output from plotly without legend

It seems that the legend switch is being ignored. It is false for the filled (confidence intervals) and true for the main plots. Turning them all to true gives six legend entries, but I only want two.

Here's the code:

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))

Upvotes: 1

Views: 2014

Answers (1)

Federico Manigrasso
Federico Manigrasso

Reputation: 1200

The first showlegend in plot_ly should always be TRUE,otherwise it will mask the others, try to swap the traces.

This example take from plotly website show the issue (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')

Upvotes: 1

Related Questions