简体   繁体   English

当R中只有一条迹线时,图例未显示在可堆叠的条形图中

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

When I have only one trace added to the plotly stacked bar chart the legend is missing. 当我仅将一条迹线添加到plotly堆积的条形图中时,图例会丢失。

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 . 根据用户选择,我根据WeekMonthQuarterYear过滤此数据集。

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 在这种情况下,我看不到任何图例,因为只有一条痕迹- 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 您可以将showlegend=TRUE添加到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. 从而强制显示图例而与迹线数量无关。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM