2017-05-21 40 views
1

我试图产生R的哑铃情节在这种情况下混合,有四排,他们需要有各自是不同的特定颜色。我使用colorRampPalette()将颜色定义为数据集的一部分。然后,当我制作情节时,颜色会以不适当的方式混杂在一起。请参阅下面的图片,特别是图例。R:在哑铃情节颜色似乎不恰当的方式

enter image description here 正如你所看到的,根据图例,橙色应该是#7570B3。但这是不正确的。 7570B3的颜色是紫色!出于这个原因,我在数据集中定义的颜色混合在图中。 “Alt 2”声音为橙色,“Alt 3”声音为紫色。

有谁知道如何解决这个问题?任何帮助将非常感激。

下面是代码的简单版本:

table_stats_scores <- data.frame(alt=c("alt1","alt2","alt3","alt4"), 
average=c(15,20,10,5), 
dumb_colors= colorRampPalette(brewer.pal(4,"Dark2"))(4), 
min=c(10,15,5,0),max=c(20,25,15,10) 
) 
table_stats_scores # This is the dataset 

table_stats_scores <- table_stats_scores[order(- 
table_stats_scores$average),] # ordering 

table_stats_scores$alt <- factor(table_stats_scores$alt, 
levels = table_stats_scores$alt[order(table_stats_scores$average)]) 
# giving factor status to alternatives so that plot_ly() picks up on this 

p <- plot_ly(table_stats_scores, x=table_stats_scores$average, color = ~ 
dumb_colors, 
y=table_stats_scores$alt,text=table_stats_scores$alt) %>% 

add_segments(x = ~min, xend = ~max, y = ~alt, yend = ~alt,name = "Min-Max 
range", showlegend = FALSE, line = list(width = 4)) %>% 

add_markers(x = ~average, y = ~alt, name = "Mean", 
marker=list(size=8.5),showlegend = FALSE) %>% 
add_text(textposition = "top right") %>% 

layout(title = "Scores of alternatives", 
xaxis = list(title = "scores"), 
yaxis = list(title = "Alternatives") 
) 
p 

回答

1

是颜色可以在plotly一个问题,因为有几种方法可以指定它,从数据框的各种要素的分配顺序可很难保持同步。

以下更改:

  • 加入鲜艳的颜色列表,以您的数据帧,因为我是不会轻易显现brewer.pal颜色。更好地调试一些明显的东西。
  • 改变了color参数为alt列,因为它实际上只是只用间接设置颜色,而且大多它决定在图例中的文本。
  • 添加颜色到text参数(而不是alt),所以我可以看到,如果它是正确分配的颜色。
  • 改变排序顺序默认的“上升”的table_stat_scores排序,因为否则它分配的颜色不正确的顺序(不完全理解这一点 - 好像有某种神秘的排序/重新排序正在进行内部)
  • 添加颜色参数到add_segmentsadd_markers,使得它们使用相同的列中设置在相同的方式的颜色。

我觉得这让你想你想:

library(plotly) 
library(RColorBrewer) 
table_stats_scores <- data.frame(alt=c("alt1","alt2","alt3","alt4"), 
           average=c(15,20,10,5), 
           dumb_colors= colorRampPalette(brewer.pal(4,"Dark2"))(4), 
           min=c(10,15,5,0),max=c(20,25,15,10) 
) 
table_stats_scores # This is the dataset 
table_stats_scores$bright_colors <- c("#FF0000","#00FF00","#0000FF","#FF00FF") 

table_stats_scores <- table_stats_scores[order(table_stats_scores$average),] # ordering 

table_stats_scores$alt <- factor(table_stats_scores$alt, 
         levels = table_stats_scores$alt[order(table_stats_scores$average)]) 
# giving factor status to alternatives so that plot_ly() picks up on this 

p <- plot_ly(table_stats_scores, x=~average, color = ~alt, y=~alt,text=~bright_colors) %>% 

    add_segments(x = ~min, xend = ~max, y = ~alt, yend = ~alt,name = "Min-Max range", 
       colors=~bright_colors, showlegend = FALSE, line = list(width = 4)) %>% 

    add_markers(x = ~average, y = ~alt, name = "Mean", 
       marker=list(size=8.5,colors=~bright_colors),showlegend = FALSE) %>% 
    add_text(textposition = "top right") %>% 

    layout(title = "Scores of alternatives", 
     xaxis = list(title = "scores"), 
     yaxis = list(title = "Alternatives") 
) 
p 

产生这样的:

enter image description here

+0

神奇的迈克·怀斯! – user1431694