plotly question

When there are more than 1 items in the FileID, it is able to display the legend.
However, when there is only 1 item in the FileID, its legend cannot be displayed, as seen in the last picture.
See pictures.

How do I enable legend even if my FileID contains only 1 item?

fig <- data %>% plot_ly()
  fig <- fig %>% add_trace(x = ~x, y = ~y1, type = 'bar',name = bytewiseID[1],
                           text = y1,textposition = 'auto',
                           marker = list(color = '#98c0ed',
                                         line = list(color = 'rgb(8,48,107)', width = 1.5)))
  if (length(bytewiseID)>=2){
    fig <- fig %>% add_trace(x = ~x, y = ~y2, type = 'bar',name = bytewiseID[2],
                             text = y2,textposition = 'auto',
                             marker = list(color = '#95e89e',
                                           line = list(color = 'rgb(8,48,107)', width = 1.5)))
  }
  if (length(bytewiseID)==3){
    fig <- fig %>% add_trace(x = ~x, y = ~y3, type = 'bar',name = bytewiseID[3],
                             text = y3,textposition = 'auto',
                             marker = list(color = '#db6b63',
                                           line = list(color = 'rgb(8,48,107)', width = 1.5)))
    }
  fig <- fig %>% layout(title = title,barmode = 'stack',
                        barmode = 'group',
                        xaxis = list(title = ""),
                        yaxis = list(title = ""))
  fig <- fig %>% layout(legend=list(title=list(text='<b> FileID </b>')))

  fig

Thanks.

fig <- fig %>% layout(showlegend = TRUE) should do it.

1 Like

FANTASTIC, THANK YOU VERY MUCH.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.