gpt4 book ai didi

r - 在 ggplotly() 中更改图例标签

转载 作者:行者123 更新时间:2023-12-05 07:41:46 25 4
gpt4 key购买 nike

我有一个多边形图,这些多边形根据数据集中的定量变量着色,并在某些离散值(0、5、10、15、20、25)处被截断。我目前有一个静态的 ggplot() 输出,它按照我想要的方式“工作”。即,图例值是截止值(0、5、10、15、20、25)。静态图如下 -

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)

最佳答案

使用切点标记并使用 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)

关于r - 在 ggplotly() 中更改图例标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45182356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com