2017-01-22 79 views
-1

這裏是我的數據彩條傳說中的使用時plotly

set.seed(42) 
mydata = data.frame(A = rnorm(20), B = rnorm(20), Index = sample(190:400,20))  

我試圖將數據分成基礎上,Index值20個不同的區間,然後根據自己的時間間隔value色彩散射點。以下是我的代碼。它並不完美。

cols = colorRampPalette(c("red", "black"), space = "rgb")(20) 
mydata$interval = cut(mydata$Index,breaks = 20) 
mydata$cols = cols[mydata$interval] 
require(plotly) 
x = list(title = "A") 
y = list(title = "B") 
plot_ly(mydata, x = ~A, y = ~B, color = ~cols, type = "scatter", 
         mode = 'markers', hoverinfo = 'text', 
         text = ~paste(interval)) %>% 
         layout(xaxis = x, yaxis = y) 

如何獲取圖例中colorbar,其中顏色是基於Index值。

回答

2

您正在尋找這樣的:

plot_ly(mydata, x = ~A, y = ~B, type = "scatter", 
     mode = 'markers', hoverinfo = 'text', colors = colorRampPalette(c("red", "black"), space = "rgb")(20), color = ~Index, text = ~paste(interval), marker = list(size=14)) %>% 
     layout(xaxis = x, yaxis = y) %>% 
     colorbar(title = "My Legend") 
+1

@Darshan巴拉爾,看到我的編輯 – MLavoie