[R] Heatmap in R and/or ggplot2

baptiste auguie baptiste.auguie at googlemail.com
Mon Jun 13 23:32:22 CEST 2011


Hi,

Try this

ggplot(df, aes(x,y)) + geom_tile(aes(fill=height), colour="white") +
scale_fill_gradientn(colours = c("red", "gold", "green")) +
geom_text(aes(lab=height))

HTH,

baptiste

On 14 June 2011 07:12, idris <idris.raja at gmail.com> wrote:
> I have a dataframe df with columns x, y, and height. I want to create a
> heatmap-like plot that creates a grid of x by y, and then color codes the
> grid depending on the value of height.
>
> Is there a ggplot2 object to do this? I'm able to easily do this in Excel
> with pivot tables and conditional formatting so I'm including an image that
> is close to the output I want. I want to be able to customize what colors
> are used for different values of height.
>
> http://r.789695.n4.nabble.com/file/n3594590/heatmap.png
>
> Sample data included below.
>
> library(ggplot2)
> df <-structure(list(x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
> 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
> 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L,
> 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
> 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), y = c(1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L,
> 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L,
> 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L,
> 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), height = c(0L, 0L, 0L,
> 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L,
> 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L,
> 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L,
> 15L, 16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L,
> 20L, 20L, 20L, 21L, 21L, 21L, 22L, 22L, 22L, 23L, 23L, 23L, 24L,
> 24L, 24L, 25L, 25L, 25L, 26L, 26L, 26L, 27L, 27L, 27L, 28L, 28L,
> 28L, 29L, 29L, 29L, 30L, 30L, 30L, 31L, 31L, 31L, 32L, 32L, 32L,
> 33L)), .Names = c("x", "y", "height"), class = "data.frame", row.names =
> c(NA,
> -100L))
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Heatmap-in-R-and-or-ggplot2-tp3594590p3594590.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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