簡體   English   中英

在ggplotly()中更改圖例標簽

[英]Changing legend labels in ggplotly()

我有一個多邊形圖,這些多邊形根據在某些離散值(0、5、10、15、20、25)處被切除的數據集中的定量變量着色。 我目前有一個靜態ggplot()輸出,可以按照我的意圖“工作”。 即,圖例值是截止值(0、5、10、15、20、25)。 靜態圖如下-

靜態的

但是,當我簡單地將此靜態圖轉換為交互式圖時,圖例值變為十六進制值(#54278F,#756BB1等),而不是截止值(0、5、10、15、20、25)。 此交互式劇情的屏幕截圖如下所示-

互動

我試圖確定一種方法來將交互式繪圖中的圖例標簽更改為截止值(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)

使用切點進行標記,並使用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) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM