geom_tile heatmap with different high fill colours based on factor

前端 未结 1 1730
滥情空心
滥情空心 2020-12-28 09:25

I\'m interested in building a heatmap with geom_tile in ggplot2 that uses a different gradient high color based on a factor.

The plot b

相关标签:
1条回答
  • 2020-12-28 09:34

    In general, ggplot2 does not permit multiple scales of a single type (i.e. multiple colour or fill scales), so I suspect that this isn't (easily) possible.

    The best nearest approximation I can come up with is this:

    df <- data.frame(expand.grid(1:5,1:5))
    df$z <- runif(nrow(df))
    df$grp <- rep(letters[1:2],length.out = nrow(df))
    
    ggplot(df,aes(x = Var1,y = Var2,fill = factor(grp),alpha = z)) + 
        geom_tile() + 
        scale_fill_manual(values = c('red','blue'))
    

    enter image description here

    But it's going to be tough to get a sensible legend.

    0 讨论(0)
提交回复
热议问题