尝试使用 heatmap.2 进行绘图时出现错误(与颜色有关)

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

我正在尝试使用 heatmap.2 创建绘图,但我一直收到错误消息 must have one more break than colour

如果有兴趣,我正在改编这个小伙子的代码:https://sebastianraschka.com/Articles/heatmaps_in_r.html

这是有问题的代码部分:

# 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

现在真正让我困惑的是这有效:

# 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

因为我似乎已经正确地修改了一次原始代码,所以我很困惑为什么我再次没有这样做。

解决方法是:

# 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

唯一相关的变化是 n=399,因为您定义 col_breaks 的长度为 400。