Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heatmap for one column of data in R

Tags:

r

Now I have a column of data, which is over 500 rows.

#example
df <- data.frame(
  City = c("New York", "LA", "DC", "Boston", "Chicago"),
  Data = c(780, 982, 111, 893, 989)

I want to build a heatmap but i only have one variable, which is City. Can I split this column into multiple columns and create the heatmap.

I am new to R and I can't find any answers online.

Thanks in advance!

like image 968
Christina Avatar asked Jan 30 '23 03:01

Christina


1 Answers

The trick is to duplicate the single column with cbind and plot...

library(gplots)
df <- data.frame(
  City = c("New York", "LA", "DC", "Boston", "Chicago"),
  Data = c(780, 982, 111, 893, 989)
)

heatmap.2(cbind(df$Data, df$Data), trace="n", Colv = NA, 
          dendrogram = "row", labCol = "", labRow = df$City, cexRow = 0.75)

enter image description here

An exmample of wrapping the data to 5x2 (or 100)

Note that I turned of the dendrograms completely as any ordering would be meaningless.

heatmap.2(matrix(df$Data, ncol = 2), trace="n", Colv = FALSE, Rowv=FALSE,
      cellnote = matrix(df$City, ncol = 2), notecol = 1, scale = "none",
      dendrogram = "none", labCol = "", labRow = "", cexRow = 0.75)

enter image description here

labRow: sets row labels for (use "" for no label)
cellnote: sets the cell label (delete this parameter if you do want cell labels)
col: sets the color palette. My suggestion is to use a pre-defined palette from colorspace, RColorBrewer or colorRamps. Some suggestions:

col=col=colorspace::sequential_hcl(15)
col=col=RColorBrewer::brewer.pal(9, "BuGn"
col=colorRamps::ygobb(25)

See this link for more on color palettes in R

like image 192
emilliman5 Avatar answered Feb 03 '23 03:02

emilliman5