creating a heatplot with ggplot column wise

382 views Asked by At

I have the following (small) melted data. Its a R RDS file, best way to transfer R datasets! You need data.table library.

> urlf = 'http://emboss.math.yorku.ca/results/ove_m_rds'
> ove.m = readRDS(gzcon(url(urlf))) ##download the data.table

Basically, I have the following code to make a heat plot of the data:

  gg = ggplot(ove.m, aes(variable, state)) 
  gg = gg +  geom_tile(aes(fill = value), colour = "white")
  gg = gg +  scale_fill_gradient(low = "white", high = "#1f78b4")

But the problem with this is the colors are adjusted to ALL the values in the matrix. I want the colors to represent the column only. So the resulting heat map looks like the following for the above data:

enter image description here

2

There are 2 answers

1
Hack-R On BEST ANSWER
urlf = 'http://emboss.math.yorku.ca/results/ove_m_rds'
ove.m = readRDS(gzcon(url(urlf))) ##download the data.table

a           <- data.frame(matrix(nrow = 9, ncol = 7))
names(a)    <- unique(levels(ove.m$variable))
ove.m$state <- as.numeric(ove.m$state)
for(i in 1:9){
  a$genomecoverage[i] <- as.numeric(ove.m$value[ove.m$variable == "genomecoverage" & ove.m$state == i ])
  a$cpgisland[i]      <- ove.m$value[ove.m$variable == "cpgisland" & ove.m$state == i ]
  a$exon[i]           <- ove.m$value[ove.m$variable == "exon" & ove.m$state == i ]
  a$gene[i]           <- ove.m$value[ove.m$variable == "gene" & ove.m$state == i ]
  a$tes[i]            <- ove.m$value[ove.m$variable == "tes" & ove.m$state == i ]
  a$tss[i]            <- ove.m$value[ove.m$variable == "tss" & ove.m$state == i ]
  a$tss2kb[i]         <- ove.m$value[ove.m$variable == "tss2kb" & ove.m$state == i ]
}

b       <- as.data.frame(apply(a, 2, scale))
b$state <- 1:9
c       <- melt(b, id.vars = "state")

gg = ggplot(ove.m, aes(variable, state)) 
gg = gg +  geom_tile(aes(fill = value), colour = "white")
gg = gg +  scale_fill_gradient(low = "white", high = "#1f78b4")
gg
0
masfenix On

I could not find an answer to this question. I was looking for a solution in which ggplot2 could identify the column groups and create a color scale on the groupings.

The alternative solution was to scale the values accordingly. For each entry of the column x, the formula is

x - min(column) divided by max(column)