1
我試圖產生R的啞鈴情節在這種情況下混合,有四排,他們需要有各自是不同的特定顏色。我使用colorRampPalette()將顏色定義爲數據集的一部分。然後,當我製作情節時,顏色會以不適當的方式混雜在一起。請參閱下面的圖片,特別是圖例。R:在啞鈴情節顏色似乎不恰當的方式
正如你所看到的,根據圖例,橙色應該是#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
神奇的邁克·懷斯! – user1431694