Fisseha Berhane Fisseha Berhane - 3 months ago 11
R Question

heatmap in R with cell area proportional to an observation

I want to produce heatmap in R where the cell area and color are governed by two columns. For example, in the data below, I want the area of every cell to be proportional to the "count" column and the color to be controlled by the "measure" column and put the "rank" label on each cell. How can I do this in R?

Event measure rank Count
A 20 1 2000
B 15 2 1870
C 10 3 1540
D 5 4 1200

Answer

The type of plot you're looking for is called a treemap.

library(treemap)

mydat <- structure(list(Event = structure(1:4, .Label = c("A", "B", "C", 
    "D"), class = "factor"), measure = c(20L, 15L, 10L, 5L), rank = 1:4, 
    Count = c(2000L, 1870L, 1540L, 1200L)), .Names = c("Event", 
"measure", "rank", "Count"), class = "data.frame", row.names = c(NA, 
 -4L))

treemap(mydat, index = "rank", vSize = "Count", vColor = "measure", type = "value",
  position.legend = "none")

produces

Treemap

Comments