2017-07-19 104 views
0

我有一個根據數據集中某個離散值(0,5,10,15,20,25)中的數字變量進行着色的多邊形圖。我目前有一個靜態的ggplot()輸出,它按照我的意圖「運行」。也就是說,圖例值是截斷值(0,5,10,15,20,25)。靜態情節如下 -在ggplotly()中更改圖例標籤

Static

然而,當我只是這個靜態的情節轉換成一個互動的情節,傳說值視爲十六進制值(#54278F,#756BB1等),而不是切斷值(0,5,10,15,20,25)。這種互動情節的屏幕截圖如下所示 -

Interactive

我試圖確定一種方法來改變圖例標籤的互動情節是切斷值(0,5,10,15, 20,25)。任何建議或支持將不勝感激!

下面是我用來創建靜態和交互式劇情代碼:用砍分

library(plotly) 
library(ggplot2) 
library(RColorBrewer) 

set.seed(1) 
x = abs(rnorm(30)) 
y = abs(rnorm(30)) 
value = runif(30, 1, 30) 

myData <- data.frame(x=x, y=y, value=value) 

cutList = c(5, 10, 15, 20, 25) 
purples <- brewer.pal(length(cutList)+1, "Purples") 
myData$valueColor <- cut(myData$value, breaks=c(0, cutList, 30), labels=rev(purples)) 

# Static plot 
sp <- ggplot(myData, aes(x=x, y=y, fill=valueColor)) + geom_polygon(stat="identity") + scale_fill_manual(labels = as.character(c(0, cutList)), values = levels(myData$valueColor), name = "Value") 

# Interactive plot 
ip <- ggplotly(sp) 

回答

1

標籤和使用scale_fill_manual的顏色。

cutList = c(5, 10, 15, 20, 25) 
purples <- brewer.pal(length(cutList)+1, "Purples") 
myData$valueLab <- cut(myData$value, breaks=c(0, cutList, 30), labels=as.character(c(0, cutList))) 

# Static plot 
sp <- ggplot(myData, aes(x=x, y=y, fill=valueLab)) + geom_polygon(stat="identity") + scale_fill_manual(values = rev(purples)) 

# Interactive plot 
ip <- ggplotly(sp)