Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legend not shown in plotly stacked bar chart when only one trace in R

Tags:

r

plotly

When I have only one trace added to the plotly stacked bar chart the legend is missing.

My dataset is as below:

myDF <- structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "2017", class = "factor"), 
    Month = c("Mar", "Mar", "Mar", "Apr", "Apr", "Apr"), Date = structure(c(17241, 
    17242, 17242, 17259, 17260, 17261), class = "Date"), Quarter = c("Q1", 
    "Q1", "Q1", "Q2", "Q2", "Q2"), Week = structure(c(2L, 2L, 
    2L, 3L, 3L, 3L), .Label = c("2017-03-05", "2017-03-12", "2017-04-02"
    ), class = "factor"), NPS = structure(c(NA, NA, NA, 3L, 3L, 
    3L), .Label = c("Detractor", "Passive", "Promoter"), class = "factor")), .Names = c("Year", 
"Month", "Date", "Quarter", "Week", "NPS"), row.names = 7:12, class = "data.frame")

As per user selection I filter this dataset based on Week, Month, Quarter or Year.

The code for this is either one of these:

  myDF %>% select(one_of(c("Week","NPS"))) -> myDF
  myDF %>% select(one_of(c("Month","NPS"))) -> myDF
  myDF %>% select(one_of(c("Quarter","NPS"))) -> myDF
  myDF %>% select(one_of(c("Year","NPS"))) -> myDF
  colnames(myDF)[1] <- "Group"

For the plot this is my code:

 dcast(na.omit(melt(myDF, id.vars = 'Group')), Group ~ value, fun.aggregate = length) -> tab
 tab
#        Group  Promoter
#1  2017-04-02         3


x <- list(
  title = ""
)
y <- list(
  title = "Count"
)

p <- plot_ly(tab, x = ~Group)

if ("Detractors" %in% colnames(tab[-1])){
  p <- add_trace(p, y = ~`Detractors`, name = 'Detractors', type = 'bar',
                 marker = list(color = '#D52728')) #red
}
if ("Passive" %in% colnames(tab[-1])){
  p <- add_trace(p, y = ~`Passive`, name = 'Passive', type = 'bar', 
                 marker = list(color = '#1F78B4')) #orange
}
if ("Promoter" %in% colnames(tab[-1])){
  p <- add_trace(p, y = ~`Promoter`, name = 'Promoter', type = 'bar', 
                 marker = list(color = '#2BA02D')) #green
}
p <- layout(p, xaxis = x, yaxis = y, barmode = 'stack', legend = list(orientation = 'h'))

p

This is the plot that I get. I do not see any legend in this case since there is only one trace - Promoter enter image description here

However if use a dataset with more than one traces like below and perform the same steps, I get the legend.

myDF <- structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "2017", class = "factor"), 
        Month = c("Mar", "Mar", "Mar", "Apr", "Apr", "Apr"), Date = structure(c(17241, 
        17242, 17242, 17259, 17260, 17261), class = "Date"), Quarter = c("Q1", 
        "Q1", "Q1", "Q2", "Q2", "Q2"), Week = structure(c(2L, 2L, 
        2L, 3L, 3L, 3L), .Label = c("2017-03-05", "2017-03-12", "2017-04-02"
        ), class = "factor"), NPS = structure(c(NA, NA, NA, 2L, 3L, 
        3L), .Label = c("Detractor", "Passive", "Promoter"), class = "factor")), .Names = c("Year", 
    "Month", "Date", "Quarter", "Week", "NPS"), row.names = 7:12, class = "data.frame")

enter image description here

Is there a way to show the legend when only one trace is available in the data to identify the color?

like image 280
krish Avatar asked Apr 20 '17 17:04

krish


1 Answers

You could add showlegend=TRUE to your layout

plot_ly(x = c(1), 
        y=c(5), 
        type = 'bar',
        name = 'lonely bar') %>% 
    layout(showlegend=T)

and thereby enforcing that the legend will be shown independent of the number of traces.

enter image description here

like image 184
Maximilian Peters Avatar answered Sep 21 '22 11:09

Maximilian Peters