首页 文章

错误:提供给离散比例的连续值

提问于
浏览
6

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

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

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

1 回答

  • 14

    如果要手动提供颜色,则需要将 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

相关问题