[R] color heatmap according to value ranges

Dennis Murphy djmuser at gmail.com
Fri Feb 6 18:42:10 CET 2015


Hi:

You get a gradient for your response variable because it is numeric.
You need to convert it to factor. (Discrete color sets need to be
mapped to discrete variables - a factor is one way to generate a
discrete variable.)  Here is one approach to get what you appear to be
looking for.

DF <- data.frame(a = rep(1:3, each = 3),
                 b = 1:3, d = 1:9)
DF$e <- cut(DF$d, c(0, 2, 3, 5, 10), rightmost = TRUE,
             labels = c("1-2", "2-3", "3-5", ">5"))

library(ggplot2)
ggplot(DF, aes(x = a, y = b, fill = e)) +
   geom_tile() +
   scale_fill_manual(values = c("darkblue", "blue", "lightblue", "white"))

You need a border of some kind around the plot since the right side of
the graph is the same color as the default background. If you're OK
with the default graph above, that's fine. If you want to expand it to
the edges, you need to draw your own border, something like

ggplot(DF, aes(x = a, y = b, fill = e)) +
   geom_tile() +
   scale_fill_manual(values = c("darkblue", "blue", "lightblue", "white")) +
  # eliminate the padding from each scale
   scale_x_continuous(expand = c(0, 0)) +
   scale_y_continuous(expand = c(0, 0)) +
 # draw the border
   theme(panel.border = element_rect(colour = "black", fill = NA))

The problem that remains is an inability to distinguish the legend key
color in the last category from the plot background. The simplest
workaround is to change the color of the last category in the scale
specification - one suggestion is a light gray, but you can always
substitute another color:

ggplot(DF, aes(x = a, y = b, fill = e)) +
   geom_tile() +
   scale_fill_manual(values = c("darkblue", "blue", "lightblue", "grey90")) +
   scale_x_continuous(expand = c(0, 0)) +
   scale_y_continuous(expand = c(0, 0)) +
   theme(panel.border = element_rect(colour = "black", fill = NA))

Dennis

On Fri, Feb 6, 2015 at 7:13 AM,  <N.Hubner at ncmls.ru.nl> wrote:
> Probably the simplest thing there is, but I can't get it to work:
>
>
>
> Example for my data:
>
>
>
> a <- c(1,1,1,2,2,2,3,3,3)
>
> b <- c(1,2,3,1,2,3,1,2,3)
>
> c <- c(1,2,3,4,5,6,7,8,9)
>
> df <- data.frame(cbind(a,b,c))
>
>
>
> I create a heat map with c being the values:
>
>
>
> ggplot(df, aes(df$a, df$b, fill = df$c)) + geom_raster()
>
>
>
> problem:
>
> The color coding is automatically a gradient. However, I would like to color in 4 fixed colors dependent on the value in c. For example:
>
>
>
> if c<=2 color "darkblue"
>
> if 2<c<=3 color "blue"
>
> if 3<c<=5 color "lightblue"
>
> if c>5 color "white"
>
>
>
> In addition I would like to show a legend that illustrates this color coding.
>
>
>
> It must be very easy, but I just can't figure it out. Only find commands that make gradients...
>
>
>
> Thanks a lot in advance!
>
>
>
>
> ______________________________________________
>
> Dr. Nina C. Hubner
> scientist quantitative proteomics
>
> Department of Molecular Biology, Radboud University Nijmegen, The Netherlands
> e-mail: n.hubner at ncmls.ru.nl
> tel: +31-24-3613655
>
> Visiting address:
> Department of Molecular Biology, RIMLS, 2nd floor
> Geert Grooteplein 26/28
> 6525 GA Nijmegen
> The Netherlands
>
>
>
> The Radboud University Medical Centre is listed in the Commercial Register of the Chamber of Commerce under file number 41055629.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list