簡體   English   中英

具有顏色數量的曲線條形圖=類別數量

[英]plotly bar chart with number of colors = number of categories

對於以下對象:

rawData <-
structure(list(obs = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), key = structure(1:8, .Label = c("sadness", 
"neutral", "contempt", "disgust", "anger", "surprise", "fear", 
"happiness"), class = "factor"), value = c(2.36220472440945, 
89.3700787401575, 0.393700787401575, 0.78740157480315, 0, 0.78740157480315, 
0, 6.2992125984252)), .Names = c("obs", "key", "value"), row.names = c(NA, 
8L), class = "data.frame")

我正在尋找創建一個情節條形圖,同時為每個條指定顏色。 不幸的是,它只需要n-1種顏色,為剩余的顏色創建自己的“漸變”。

rawData %>% 
    group_by(obs) %>%
    plot_ly(x = obs, 
            y = value, 
            type = "bar", 
            color = key,
            colors = c("blue","grey","darkred","green","orange","black","purple"))

如果我嘗試添加第8種顏色......

rawData %>% 
        group_by(obs) %>%
        plot_ly(x = obs, 
                y = value, 
                type = "bar", 
                color = key,
                colors = c("blue","grey","darkred","green","orange","black","purple","yellow"))

現在所有的酒吧都是黃色的。

請使用color參數來marker參數。

rawData %>% 
  group_by(obs) %>%
  plot_ly(x = obs, 
    y = value, 
    type = "bar", 
    color = key,
    marker = list(color=c("blue","grey","darkred","green","orange","black","purple","yellow"))) 

作為替代方案,您還可以使用ggplot2繪制情節圖,這將為您提供所需的內容。

p <- ggplot(data=rawData, aes(x=obs, y=value, color=key, group=key, fill=key)) + geom_bar(stat='identity', position='dodge') + scale_fill_manual(values=c("blue","grey","darkred","green","orange","black","purple","yellow")) + scale_color_manual(values=c("blue","grey","darkred","green","orange","black","purple","yellow"))

ggplotly(p)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM