Question

I want to create a heatmap using R.

Here is how my dataset looks like:

sortCC
                     Genus     Location  Number    propn
    86         Flavobacterium       CC    580 0.3081827843
    130          Algoriphagus       CC    569 0.3023379384
    88              Joostella       CC    175 0.0929861849
    215            Paracoccus       CC    122 0.0648246546
    31              Leifsonia       CC     48 0.0255047821

sortN

                  Genus   Location  Number  propn
119           Niastella        N    316 0.08205661
206         Aminobacter        N    252 0.06543755
51         Nocardioides        N    222 0.05764736
121            Niabella        N    205 0.05323293
257 Pseudorhodoferax???        N    193 0.05011685
149          Pedobacter        N    175 0.04544274

Here is the code I have so far:

row.names(sortCC) <- sortCC$Genus
sortCC_matrix <- data.matrix(sortCC)
sortCC_heatmap <- heatmap(sortCC_matrix, Rowv=NA, Colv=NA, col = cm.colors(256), scale="column", margins=c(5,10))

I was going to generate 2 separate heatmap, but when I used the code above it looked wrong while using R.

Questions: 1)Is it possible to combine the two data set since they have the same genus, but the difference is the location and number & proportion. 2) If it is not possible to combine the two then how do I exclude the location column from the heatmap.

Any suggestions will be much appreciated! Thanks!

Was it helpful?

Solution

Since you have the same columns, you cand bind your data.frames and use some facets to differentiate it. Here a solution based on ggplot2:

dat <- rbind(sortCC,sortN)
library(ggplot2)
ggplot(dat, aes(y = factor(Number),x = factor(Genus))) +
  geom_tile(aes(fill = propn)) +
  theme_bw() + 
  theme(axis.text.x=element_text(angle=90)) +
  facet_grid(Location~.)

enter image description here

To remove extra column , You can use subset:

subset(dat,select=-c(Location))

If you still want to merge data's by Genius, you can use do this for example:

sortCC <- subset(sortCC,select=-c(Location))
sortN <- subset(sortN,select=-c(Location))
merge(sortCC,sortN,by='Genus')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top