2017-05-21 57 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