简体   繁体   中英

Error (relating to colours) when trying to plot using heatmap.2

I'm trying to create a plot using heatmap.2 but I keep receiving the error must have one more break than colour .

In case it's of interest I'm adapting this chap's code: https://sebastianraschka.com/Articles/heatmaps_in_r.html

Here is the portion of the code in question:

# creates a own color palette 
my_palette <- colorRampPalette(c("snow", "yellow", "darkorange", "red"))(n = 299)

# (optional) defines the color breaks manually for a "skewed" color transition
col_breaks = c(seq(0,0.15,length=100), #white
               seq(0.16,0.29,length=100), # for yellow
               seq(0.3,0.5,length=100), # for orange
               seq(0.51,1,length=100))    # for red

Now something that is really confusing me is that this works:

# creates a own color palette 
my_palette <- colorRampPalette(c("snow", "yellow", "red"))(n = 299)


# (optional) defines the color breaks manually for a "skewed" color transition
col_breaks = c(seq(0,0.29,length=100),  #white
                  seq(0.3,0.5,length=100), # for yellow
                  seq(0.51,1,length=100))  # for red

As I seem to have modified the original code correctly once before I'm quite confused as to why I've failed to do so again.

The solution is:

# creates a own color palette 
my_palette <- colorRampPalette(c("snow", "yellow", "darkorange", "red"))(n = 399)

# (optional) defines the color breaks manually for a "skewed" color transition
col_breaks = c(seq(0,0.15,length=100), #white
               seq(0.16,0.29,length=100), # for yellow
               seq(0.3,0.5,length=100), # for orange
               seq(0.51,1,length=100))    # for red

with the only relevant change being n=399 , since you define col_breaks of length 400.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM