ggplot2 heatmap with colors for ranged values

后端 未结 2 1294
面向向阳花
面向向阳花 2020-11-27 13:23

I want to make a heatmap in ggplot2. My toy data and code is:

set.seed(12345)
dat <- 
  data.frame(
      Row = rep(x = LETTERS[1:5], times = 10)
    , Co         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 13:41

    You have several options for something like this, but here is one as a starting point.

    First, use cut to create a factor from Y with the appropriate ranges:

    dat$Y1 <- cut(dat$Y,breaks = c(-Inf,-3:3,Inf),right = FALSE)
    

    Then plot using a palette from RColorBrewer:

    ggplot(data =  dat, aes(x = Row, y = Col)) + 
          geom_tile(aes(fill = Y1), colour = "white") +
          scale_fill_brewer(palette = "PRGn")
    

    enter image description here

    This color scheme is more purple than blue on the low end, but it's the closest I could find among the brewer palette's.

    If you wanted to build your own, you could simply use scale_fill_manual and specify your desired vector of colors for the values argument.

提交回复
热议问题