3

I am new to R and trying to create a heat map or density map to show my results. I have x data for 20 different locations and for 20 years. I am trying to show differences in locations as well as in years in the same map. my data looks like this.

    1975    1977    1979    1981    1983    1985    1987    1989
Dallas  25  28  27  29  31  33  35  37
Houston 33  38  43  48  53  58  63  68
Lubbock 28  29  31  33  35  37  39  41
Austin  22  24  26  28  30  32  34  36
San Antonio 31  32  33  34  35  36  37  38

Thank you for your help.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
bvagri
  • 31
  • 1
  • 1
  • 2
  • See also these links: http://stackoverflow.com/questions/7747991/geographical-heat-map-in-r http://stackoverflow.com/questions/8421536/a-true-heat-map-in-r http://stackoverflow.com/questions/10198228/heat-map-or-density-map-in-r http://stackoverflow.com/questions/8161014/custom-heat-map-in-r. And that is just the first few when searching for `[r] heat map` in SO. – Paul Hiemstra Apr 17 '12 at 20:17
  • @PaulHiemstra Seems like a duplicate, then - the answers are kind of sparse on those questions, but it looks like all the info is there, right? – Matt Parker Apr 17 '12 at 20:23
  • possible duplicate of [Plotting a heat map for an upper or lower triangular matrix](http://stackoverflow.com/questions/6883618/plotting-a-heat-map-for-an-upper-or-lower-triangular-matrix) – Paul Hiemstra Apr 17 '12 at 20:28

3 Answers3

6

For a nice image() type plot, you will need to add some labels to your axes. Also, a color legend will help with the interpretation of the colors. My blog http://menugget.blogspot.de/ has a function for adding this scale to an image plot. Below is an example (after loading the image.scale function from menugget:

years <- seq(1975,1989,2)
db <- data.frame(Dallas=c( 25, 28, 27, 29, 31, 33, 35, 37),
 Houston=c( 33, 38, 43, 48, 53, 58, 63, 68),
 Lubbock=c( 28, 29, 31, 33, 35, 37, 39, 41),
 Austin= c(22, 24, 26, 28, 30, 32, 34, 36),
 San_Antonio= c(31, 32, 33, 34, 35, 36, 37, 38)
)
db <- as.matrix(db)

#plot
layout(matrix(c(1,2), nrow=1, ncol=2), widths=c(4,1), heights=c(4))
layout.show(2)
par(mar=c(5,5,1,1))
image(x=years, z=db, yaxt="n")
axis(2, at=seq(0,1,,dim(db)[2]), labels=colnames(db))
#image.scale from http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html
par(mar=c(5,0,1,5))
image.scale(db, horiz=FALSE, yaxt="n", xaxt="n", xlab="", ylab="")
axis(4)
mtext("temp", side=4, line=2)
box()

enter image description here

Marc in the box
  • 11,769
  • 4
  • 47
  • 97
3

You can just use the image() function to create a heatmap. Customize by specifying the col parameter to specify the colors.

Note that if you're currently using a data.frame, you may need to convert it to a matrix:

 image(as.matrix(myVar))
Jeff Allen
  • 17,277
  • 8
  • 49
  • 70
2

Alternatively, you can make a heat map using ggplot2, just use the tile geometry (geom_tile). See this link for a very elaborate example.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149