RStudio中heatmap.2颜色键不显示问题求助
解决heatmap.2热力图颜色键消失问题
问题描述
在RStudio中绘制heatmap.2热力图时,颜色键(color key)突然消失,尝试切换调色板、注释边距相关代码后问题仍未解决。原代码如下:
ghe <- read.csv("greenhouse-emissions.csv") gh_df <- ghe[ghe$Pollutant == "Greenhouse gases",] gh_df <- gh_df[gh_df$VAR == "TOTAL", ] cyv_df <- gh_df[c('Country', 'Year', 'Value')] cyv_df <- cyv_df[cyv_df$Country != "OECD - Total",] new_df <- cyv_df %>% pivot_wider(names_from = Year, values_from = Value) new_df[is.na(new_df)] <- 0 #Creating matrix for heatmap.2 m <- data.matrix(new_df) m <- m[,-1] # My original color palette. However, I later decided to not use the palette because I thought the palette was the cause behind the color key disappearance. my_palette <- colorRampPalette(c("mintcream", "cadetblue", "darkolivegreen1", "yellow", "tan1", #"darkgoldenrod", "red", "red2", "red4", "black"))(99) col_breaks = c(seq(-1, 1, length=1), seq(1.001, 3428.207,length=5), seq(3428.208, 61257.344, length=15), seq(61257.345, 592587.216,length=10), #upper smaller countries seq(592587.217, 1.27e+06, length=19), #Germany seq(1270000.001, 1.33e+06, length=19),# Japan seq(1330000.001, 2.5e+06,length=11), # Russia seq(2500000.001, 6.25e+06, length=10), seq(6250000.001, 17124526.000, length=10)) Colors=rev(brewer.pal(11,"Spectral")) Colors=colorRampPalette(Colors)(99) dev.off() heatmap.2(m, col=Colors, breaks=col_breaks, trace="none", Colv=NA, # Rowv = NA, dendrogram = 'none', margins=c(5,5), density.info='histogram', denscol="black", key=TRUE, symkey=FALSE, keysize=1, key.par=list(mar=c(3.5,0,3,0)), lmat=rbind(c(5, 4, 2), c(6, 1, 3)), lwid=c(1, 10, 1), notecol = 'black')
修复方案
1. 调整布局参数
自定义的lmat和lwid布局可能挤压了颜色键的显示空间:
- 先注释掉
lmat和lwid参数,使用默认布局测试颜色键是否恢复; - 若需保留自定义布局,修改
lwid的宽度比例,比如lwid=c(2,10,1),给颜色键所在列留出足够宽度。
2. 匹配颜色与断点数量
heatmap.2要求颜色数量等于length(col_breaks)-1。当前代码中Colors有99种颜色,但col_breaks长度仅为90,导致参数不匹配引发异常。修改col_breaks的length.out参数,确保length(col_breaks)为100(对应99种颜色):
col_breaks <- c(seq(-1, 1, length.out=2), seq(1.001, 3428.207, length.out=6), seq(3428.208, 61257.344, length.out=16), seq(61257.345, 592587.216, length.out=11), seq(592587.217, 1.27e+06, length.out=20), seq(1270000.001, 1.33e+06, length.out=20), seq(1330000.001, 2.5e+06, length.out=12), seq(2500000.001, 6.25e+06, length.out=11), seq(6250000.001, 17124526.000, length.out=11))
3. 修改颜色键边距
key.par=list(mar=c(3.5,0,3,0))中左右边距设为0,可能导致颜色键被截断,调整为:
key.par=list(mar=c(3.5,1,3,1))
修改后的完整测试代码
ghe <- read.csv("greenhouse-emissions.csv") gh_df <- ghe[ghe$Pollutant == "Greenhouse gases",] gh_df <- gh_df[gh_df$VAR == "TOTAL", ] cyv_df <- gh_df[c('Country', 'Year', 'Value')] cyv_df <- cyv_df[cyv_df$Country != "OECD - Total",] new_df <- cyv_df %>% pivot_wider(names_from = Year, values_from = Value) new_df[is.na(new_df)] <- 0 #Creating matrix for heatmap.2 m <- data.matrix(new_df) m <- m[,-1] Colors=rev(brewer.pal(11,"Spectral")) Colors=colorRampPalette(Colors)(99) # 修正后的断点,匹配颜色数量 col_breaks <- c(seq(-1, 1, length.out=2), seq(1.001, 3428.207, length.out=6), seq(3428.208, 61257.344, length.out=16), seq(61257.345, 592587.216, length.out=11), seq(592587.217, 1.27e+06, length.out=20), seq(1270000.001, 1.33e+06, length.out=20), seq(1330000.001, 2.5e+06, length.out=12), seq(2500000.001, 6.25e+06, length.out=11), seq(6250000.001, 17124526.000, length.out=11)) dev.off() heatmap.2(m, col=Colors, breaks=col_breaks, trace="none", Colv=NA, dendrogram = 'none', margins=c(5,5), density.info='histogram', denscol="black", key=TRUE, symkey=FALSE, keysize=1, key.par=list(mar=c(3.5,1,3,1)), # 先注释自定义布局,测试后再按需调整 # lmat=rbind(c(5, 4, 2), c(6, 1, 3)), # lwid=c(2, 10, 1), notecol = 'black')
内容的提问来源于stack exchange,提问作者SH Yu
相关产品推荐
相关产品推荐

