Search code examples
rmatrixheatmapread.tablegplots

R: read.table for heatplot


I have the following matrix:

row genome1 genome2 genome3
genome1 100 99.99 98.88
genome2 99.99 100 NA
genome3 98.88 NA 100

which I want to plot as a heatmap.

With the code posted below, I have problems reading the file - the rownames and header are not kept. Plus, I would like that the values are displayed with a % on the heatmap or the NA.

ani <-read.table(file="matrix", header=T, sep="\t", dec=".", row.names=1)
anim<-as.matrix(as.data.frame(lapply(ani, as.numeric)))
library(gplots)
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)
heatmap.2(anim, notecol="black", density.info="none", trace="none", col=my_palette, cellnote=anim)

Solution

  • Try this:

    ani <- read.table(text='
    row genome1 genome2 genome3
    genome1 100 99.99 98.88
    genome2 99.99 100 NA
    genome3 98.88 NA 100
    ', header=T, row.names=1, quote="")
    
    anim <- as.matrix(ani)
    library(gplots)
    my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)
    heatmap.2(anim, notecol="black", density.info="none", trace="none", 
              col=my_palette, cellnote=anim, margins=c(10,10))
    

    enter image description here