简体   繁体   English

在ggplotly()中更改图例标签

[英]Changing legend labels in ggplotly()

I have a plot of polygons that are colored according to a quantitative variable in the dataset being cut off at certain discrete values (0, 5, 10, 15, 20, 25). 我有一个多边形图,这些多边形根据在某些离散值(0、5、10、15、20、25)处被切除的数据集中的定量变量着色。 I currently have a static ggplot() output that "works" the way I intend. 我目前有一个静态ggplot()输出,可以按照我的意图“工作”。 Namely, the legend values are the cut off values (0, 5, 10, 15, 20, 25). 即,图例值是截止值(0、5、10、15、20、25)。 The static plot is below - 静态图如下-

静态的

However, when I simply convert this static plot to an interactive plot, the legend values become hexadecimal values (#54278F, #756BB1, etc.) instead of the cut off values (0, 5, 10, 15, 20, 25). 但是,当我简单地将此静态图转换为交互式图时,图例值变为十六进制值(#54278F,#756BB1等),而不是截止值(0、5、10、15、20、25)。 A screenshot of this interactive plot is shown below - 此交互式剧情的屏幕截图如下所示-

互动

I am trying to determine a way to change the legend labels in the interactive plot to be the cut off values (0, 5, 10, 15, 20, 25). 我试图确定一种方法来将交互式绘图中的图例标签更改为截止值(0、5、10、15、20、25)。 Any suggestions or support would be greatly appreciated! 任何建议或支持将不胜感激!

Below is the code I used to create the static and interactive plot: 下面是我用来创建静态和交互式绘图的代码:

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)

Label using the cut points and use scale_fill_manual for the colors. 使用切点进行标记,并使用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