Flat stacked velvet - irregular colors in the legend

I am trying to build a chart in the form of columns in plotly (plotly_4.5.6)from data such as here:

> dane
visit_source variable value
1      organic     2016    32
2       social     2016    20
3           hp     2016    24
4      branded     2016    24
5      organic     2015    25
6       social     2015    25
7           hp     2015    25
8      branded     2015    25

Using this code:

library("dplyr")
library("plotly")

plot_ly(dane, y = ~variable, x = ~value, color = ~visit_source, text = ~paste0(value, "%"),
        type = "bar", hoverinfo = "text+name", marker = list(line = list(color = "white", width = 2))) %>%
    layout(barmode = "stack",
           legend = list(orientation = "h"),
           xaxis = list(ticksuffix = "%"),
           yaxis = list(title = ""))

I get:

enter image description here

But when I change my own a bit data.frame(just getting rid of one line), some colors in the legend turn black. Like here:

plot_ly(dane[-1, ], y = ~variable, x = ~value, color = ~visit_source, text = ~paste0(value, "%"),
        type = "bar", hoverinfo = "text+name", marker = list(line = list(color = "white", width = 2))) %>%
    layout(barmode = "stack",
           legend = list(orientation = "h"),
           xaxis = list(ticksuffix = "%"),
           yaxis = list(title = ""))

enter image description here

When I have everything levelsof visit_sourcefor everyone yearwith valueequal 0, then it works, but also appears in hover, which I do not like.

Do you know what the problem is? Thanks for any help!

My details:

structure(list(visit_source = structure(c(1L, 2L, 3L, 4L, 1L, 
                                          2L, 3L, 4L), .Label = c("organic", "social", "hp", "branded"), class = "factor"), 
               variable = c("2016", "2016", "2016", "2016", "2015", "2015", 
                            "2015", "2015"), value = c(32, 20, 24, 24, 25, 25, 25, 25
                            )), class = "data.frame", .Names = c("visit_source", "variable", 
                                                                 "value"), row.names = c(NA, -8L)) -> dane
+4
source share
1 answer

Try installing the latest developer version Plotly(4.5.6.9000) and it should work

enter image description here

df = data.frame(visit_source=c('organic', 'social', 'hp', 'branded', 'organic', 'social', 'hp', 'branded'), 
                variable = c(rep('2016', 4), rep('2015',4)),
                value=c(32, 20, 24, 24, 25, 25, 25, 25))


library("dplyr")
library("plotly")

plot_ly(df[-1,], y = ~variable, x = ~value, color = ~visit_source, text = ~paste0(value, "%"),
        type = "bar", hoverinfo = "text+name", marker = list(line = list(color = "white", width = 2))) %>%
  layout(barmode = "stack",
         legend = list(orientation = "h"),
         xaxis = list(ticksuffix = "%"),
         yaxis = list(title = ""))
0
source

Source: https://habr.com/ru/post/1663727/


All Articles