0
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
Apartment_no <- c("1-SV","1-SV","1-SV","1-SH","1-SH","1-SH","1-1V","1-1V","1-1V","1-1H","1-1H","1-1H","3-SV","3-SV","3-SV","3-1V","3-1V","3-1V","3-1H","3-1H","3-1H")
month <- c("September","October","November","September","October","November","September","October","November","September","October","November","September","October","November","September","October","November","September","October","November")
Days <- c(NA,19,28,2,19,28,2,19,NA,2,19,28,25,31,28,12,NA,24,8,26,19)
Heat_clean <- data.frame(Apartment_no,month,Days)

This is just a sample dataset. In actual, I have around 163 apartment_no and all the months of data. I wish to create an interactive heatmap for it since the data is quite big. I wish to use a special color code i.e whenever the Days==NA, color=Red,1<=Days<=5,color=Blue,6<=Days<=15,color=Orange,16<=Days<=25,color=Pink, 25<=Days<=31,color=Green. I have used the following code but it is not working for me & infact giving me an error message as "`x' must be a numeric matrix". My code is as follows:

> mypallete <- colorRampPalette(Days=na.color,col="Red", 1<=Days<=5,col="Blue", 6<=Days<=15,col="Orange", 16<=Days<=25,col="Pink", 25<=Days<=31,color=Green)

> heatmap.2(as.matrix(Heat_clean),Rowv = F,Colv = F,main = "Heatmeters data",col = mypallete,dendrogram = "none",density.info = "none",trace = "none")

I first defined my own pallete & then implemented it in my code. I wish to get something that can be seen in figure, though with my personalised color coding. Maybe later I can insert the code in plot_ly to get it interactive. enter image description here

camille
  • 16,432
  • 18
  • 38
  • 60

1 Answers1

0

Your ranges are not exclusive (Days <= 25 and 25 <= Days) but that is easily fixed...

I don't think the structure of your data matches what heatmap.2 is expecting, and that is what is giving you the error, not the color map.

Here is one brute-force way to generate the color palette... (note I changed the spelling of mypalette)

mypalette=rep("Green",length(Days))
mypalette[Days <= 25]  = "Pink" 
mypalette[Days <= 15]  = "Orange" 
mypalette[Days <= 5]   = "Blue" 
mypalette[is.na(Days)] = "Red" 
beroe
  • 11,784
  • 5
  • 34
  • 79
  • What sort of structure do I need for this? – Kartik Kaushal Nov 21 '19 at 10:23
  • I have tried the following code now, >p <- ggplot(Heat_clean,aes(month,Apartment_no,fill=as.factor(Days)))+geom_tile()+scale_fill_manual(values = mypalette) I am getting the following error now "Insufficient values in manual scale. 31 needed but only 21 provided" – Kartik Kaushal Nov 21 '19 at 13:55
  • Try starting with `mypalette=rep("Green",31)` or `mypalette=rep(NA,31); mypalette[Days > 25] = "Green" ` ` – beroe Nov 26 '19 at 07:31