在完成KINSHIP矩阵(情缘关系矩阵,即G矩阵)后,可以绘制热图用于评估群体:
#安装并加载必要包
if(!require("pheatmap")) install.packages("pheatmap")
if(!require("RColorBrewer")) install.packages("RColorBrewer")
if(!require("extrafont")) install.packages("extrafont")
library(pheatmap)
library(RColorBrewer)
library(extrafont)
读取数据部分略过:矩阵变量为 kinship_matrix
# 检查Arial字体是否可用
if(!"Arial"%in% fonts()){
message("Arial字体未安装,将使用默认sans字体")
font_family <-"sans"}else{
font_family <-"Arial"}
# 生成PDF
pdf("Kinship_Heatmap_True.pdf",
width = 8,
height = 8,
family = font_family)
#因为使用G矩阵,因此数据范围为[1, 0.5]
#设置颜色及图例范围
color_breaks <- seq(0,0.5, length.out =100)
legend_breaks <- c(0.0, 0.1, 0.2,0.3, 0.4,0.5)
legend_labels <- c("0.0","0.1","0.2","0.3","0.4","0.5")
#生成热图
pheatmap(kinship_matrix,
cluster_rows =FALSE,# 关闭行聚类
cluster_cols =FALSE,# 关闭列聚类
color = colorRampPalette(rev(brewer.pal(n =7, name ="RdBu")))(100),# 颜色映射
breaks = color_breaks,
show_rownames =FALSE,# 不显示行标签(避免重叠)
show_colnames =FALSE,# 不显示列标签
legend =TRUE,
legend_breaks = legend_breaks,
legend_labels = legend_labels,
main ="Kinship Matrix Heatmap")
dev.off()