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
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")
Is there a way to show the legend when only one trace is available in the data to identify the color?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With