gpt4 book ai didi

r - 错误 : Continuous value supplied to discrete scale

转载 作者:行者123 更新时间:2023-12-04 00:53:16 30 4
gpt4 key购买 nike

我正在尝试为离散结果(每个值使用单一颜色)制作热图,以便:

df<-data.frame(x=rep(LETTERS[1:10], each=10), 
y=rep(1:10,10),
value=sample(1:8, 100,replace=T))
colors<-c("green", "lightyellow", "yellow", "orange", "orangered", "red", "darkred", "black")
ggplot(df, aes(x=x, y=y))+
geom_tile(aes(fill=value), colour="white")+
#scale_fill_gradient(low="green", high="red")
scale_fill_manual(values=colors)
Error: Continuous value supplied to discrete scale

有人知道如何修复它并将颜色变量应用于热图吗?

最佳答案

如果你想手动提供颜色,你需要映射fill为因子变量。

ggplot(df, aes(x=x, y=y))+
geom_tile(aes(fill=factor(value)), colour="white")+
scale_fill_manual(name = "Values", values=setNames(colors, 1:8))

enter image description here

关于r - 错误 : Continuous value supplied to discrete scale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26867729/

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