3

对于以下对象:

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

现在所有的酒吧都是黄色的。

4

2 回答 2

4

改为使用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"))) 
于 2016-01-14T21:09:19.957 回答
1

作为替代方案,您还可以使用 ggplot2 在 plotly 中生成一个情节,这将为您提供您想要的。

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)
于 2016-01-14T19:33:10.217 回答