You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot2相关性热力图标签格式错误问题求助

相关性热力图y轴标签异常与警告问题解决建议

问题说明

使用corr.data结合ggplot2绘制相关性热力图时,通过fct_relevel指定了包含pH、L*、a*等的标签顺序,运行后出现以下问题:

  • x轴列标签显示正常,但y轴行标签中的L*、a*、b*、c*、h*被替换为L.、a.、b.、c.、h.
  • 控制台输出4条警告:5 unknown levels in 'f': L*, a*, b*, c*, and h*
  • 已确认数据集原标签名称与代码中指定的完全一致

原因分析

corr.data函数在处理行变量(rowvars)时,会自动将变量名中的特殊字符(如*)替换为.,但列变量(colvars)保留了原名称格式。这导致fct_relevel中指定的L*等层级在rowvars中不存在,触发警告的同时,y轴显示被替换后的名称。

解决步骤

1. 验证变量名格式

先查看corr.data输出结果中rowvars的实际值,确认特殊字符是否被替换:

temp_corr <- corr.data(final[c(14:24, 52:55)])
unique(temp_corr$rowvars)

输出会显示L.、a.等格式的名称,验证上述原因。

方案一:直接使用替换后的名称重新排序

修改ggplot的y轴映射,将fct_relevel中的L*等替换为L.:

tiff("C:/Users/grego/OneDrive/Documentos/Meat_grades/Brazil_Beef_Quality/Bianca/lab_corr_geral.tiff", width =10 , height = 6, units = 'in', res = 200)
corr.data(final[c(14:24, 52:55)]) %>% 
  ggplot(aes(
    fct_relevel(colvars,'pH','PL','DL','EL','CL','L*','a*','b*','c*','h*','WBSF','TE','JU','FL','OA'), 
    fct_relevel(rowvars,'pH','PL','DL','EL','CL','L.','a.','b.','c.','h.','WBSF','TE','JU','FL','OA')
  )) +
  geom_tile(colour="grey20", aes(fill=corr), size=0.5) +
  geom_text(aes(label=sprintf("%1.2f", corr)), position=position_nudge(y=0.2), 
            size=2.5, colour="black") +
  geom_text(aes(label=paste0("(",sprintf("%1.2f", p.value),")")), position=position_nudge(y=-0.2), 
            size=2, colour="black") +
  scale_fill_gradient2(low="red", mid="yellow2", high="blue", midpoint=0, limits=c(-1,1)) +
  scale_size_continuous(range=c(8,12)) +
  labs(x="",y="") +
  theme (axis.text = element_text(size = 10), legend.position = "none") +
  coord_fixed()
dev.off()

方案二:修复rowvars名称后再统一排序

先将rowvars中的.替换回*,再指定排序规则,这样y轴会显示原标签格式:

library(stringr) # 需先加载stringr包

tiff("C:/Users/grego/OneDrive/Documentos/Meat_grades/Brazil_Beef_Quality/Bianca/lab_corr_geral.tiff", width =10 , height = 6, units = 'in', res = 200)
corr.data(final[c(14:24, 52:55)]) %>% 
  mutate(rowvars = str_replace_all(rowvars, "\\.", "\\*")) %>%  # 将.替换为*
  ggplot(aes(
    fct_relevel(colvars,'pH','PL','DL','EL','CL','L*','a*','b*','c*','h*','WBSF','TE','JU','FL','OA'), 
    fct_relevel(rowvars,'pH','PL','DL','EL','CL','L*','a*','b*','c*','h*','WBSF','TE','JU','FL','OA')
  )) +
  geom_tile(colour="grey20", aes(fill=corr), size=0.5) +
  geom_text(aes(label=sprintf("%1.2f", corr)), position=position_nudge(y=0.2), 
            size=2.5, colour="black") +
  geom_text(aes(label=paste0("(",sprintf("%1.2f", p.value),")")), position=position_nudge(y=-0.2), 
            size=2, colour="black") +
  scale_fill_gradient2(low="red", mid="yellow2", high="blue", midpoint=0, limits=c(-1,1)) +
  scale_size_continuous(range=c(8,12)) +
  labs(x="",y="") +
  theme (axis.text = element_text(size = 10), legend.position = "none") +
  coord_fixed()
dev.off()

内容的提问来源于stack exchange,提问作者Greg Rov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 19:57:39