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

时间:2019-04-05 06:23:37

标签: r ggplot2 plot colors heatmap

我正在尝试使用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

在我对为何再次失败的原因感到困惑之前,我似乎已经正确地修改了原始代码一次。

1 个答案:

答案 0 :(得分:1)

解决方案是:

# 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

,由于您定义了长度为400的col_breaks,因此唯一相关的更改是n=399