2016-12-14 134 views
4

我試圖從數據繪製堆積柱形圖plotly (plotly_4.5.6)喜歡這裏:Plotly堆疊條形圖 - 不正確的顏色在傳說

> 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 

使用此代碼:

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 = "")) 

我得到:

enter image description here

但是,當我修改我的data.frame a (僅排除一行),傳說中的某些顏色變成黑色。喜歡這裏:

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

當我爲每個yearvisit_source所有levelsvalue等於0,那麼它的工作原理,但也出現在hover,我不喜歡。

你知道這裏有什麼問題嗎?謝謝你的幫助!

我的數據:

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 
+2

因爲'戴恩時髦的東西是怎麼回事[c(-1,-5),]'產生一個OK結果(缺少一個等級)。如果你製作一個ggplot並使用'ggplotly',它會產生一個OK結果。 –

+1

可能是一個錯誤。正如@RomanLuštrik所提到的,如果你完全放棄「有機」這個層次,它會產生正確的結果...... – Sotos

回答

0

嘗試安裝的Plotly(4.5.6.9000)最新的開發者版本,它應該工作

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 = "")) 
相關問題