How to plot a heatmap in R?

r
datavisualization
heatmap

#1

Hello,

I was trying to plot a heatmap with my data in R. I used the following code while following a tutorial on a website.
->

library(gplots)
library(RColorBrewer)
mat_data <- data.matrix(train[c(“temp”,“atemp”,“humidity”)])
head(mat_data)
temp atemp humidity
[1,] 9.84 14.395 81
[2,] 9.02 13.635 80
[3,] 9.02 13.635 80
[4,] 9.84 14.395 75
[5,] 9.84 14.395 75
[6,] 9.84 12.880 75

rownames(mat_data)=train$weather
my_palette <- colorRampPalette(c(“red”, “yellow”, “green”))(n = 299)
col_breaks = c(seq(-1,0,length=100), # for red
seq(0,0.8,length=100), # for yellow
seq(0.8,1,length=100)) # for green
png(“heatmaps_in_r.png”, # create PNG for the heat map
width = 5300, # 5 x 300 pixels
height = 5
300,
res = 300, # 300 pixels per inch
pointsize = 8)
heatmap.2(mat_data,
cellnote = mat_data, # same data set for cell labels
main = “Correlation”, # heat map title
notecol=“black”, # change font color of cell labels to black
density.info=“none”, # turns off density plot inside color legend
trace=“none”, # turns off trace lines inside the heat map
margins =c(12,9), # widens margins around plot
col=my_palette, # use on color palette defined earlier
breaks=col_breaks, # enable color transition at specified limits
dendrogram=“row”, # only draw a row dendrogram
Colv=“NA”)

But I am getting an error saying->

Error in seq.default(min.raw, max.raw, by = min(diff(breaks)/4)) :
invalid (to - from)/by in seq(.)

What does this mean and how to get rid of this error and get a heatmap?

thanks!


#2

look at these two lines in your code

and modify them for by value

seq(0,0.8,length=100), # for yellow
seq(0.8,1,length=100)) # for green