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