带有 p 值的 FST 距离矩阵的三角形热图

问题描述 投票:0回答:1

我正在尝试创建 FST 距离矩阵的三角形热图:

    NWAtl   CIV SEN MRT MOR PRT ESP PAL LG  JN  TUN MLT MR  AEG
NWAtl   0   0   0   0   0   0,00098 0,00586 0,01953 0,01465 0,01855 0,00391 0,05078 0,00684 0
CIV 0,58701 0   0,24805 0,0625  0,00391 0   0   0   0   0   0   0   0,00195 0
SEN 0,46143 0,00365 0   0,68555 0,11523 0   0   0   0   0   0,00195 0   0,01953 0,01562
MRT 0,40702 0,02014 0,0000  0   0,34766 0   0   0   0   0,00391 0,00293 0   0,09082 0,05078
MOR 0,34654 0,05744 0,01748 0,00000 0   0,00098 0   0   0,00293 0,02734 0,03223 0,00098 0,17285 0,08496
PRT 0,14366 0,25279 0,16592 0,13095 0,07964 0   0,76855 0,0791  0,14648 0,20605 0,33594 0,13574 0,05664 0,00098
ESP 0,13075 0,26191 0,18752 0,15328 0,10177 0,00000 0   0,08398 0,10156 0,13281 0,13672 0,20117 0,02246 0
PAL 0,10058 0,36823 0,26087 0,21078 0,14975 0,02033 0,01352 0   0,32031 0,10938 0,06738 0,37109 0,02441 0
LG  0,13747 0,29423 0,19685 0,14836 0,09261 0,01159 0,01123 0,00027 0   0,55372 0,3291  0,10156 0,13184 0
JN  0,15804 0,25805 0,15377 0,10518 0,05754 0,0118  0,01614 0,01900 0,00000 0   0,42383 0,05371 0,31348 0,03906
TUN 0,18355 0,21649 0,12779 0,08634 0,03648 0,00129 0,01209 0,02700 0,00059 0   0   0,0459  0,18848 0,01465
MLT 0,0823  0,46264 0,33675 0,28355 0,2103  0,01988 0,00747 0,00000 0,02690 0,05412 0,05474 0   0,01562 0
MR  0,24169 0,16147 0,08179 0,04301 0,01424 0,05481 0,06864 0,08095 0,02960 0,00000 0,01714 0,14447 0   0,35547
AEG 0,35912 0,0965  0,0552  0,03027 0,01964 0,11375 0,13098 0,16291 0,10019 0,05367 0,06768 0,24109 0,00000 0

对角线下方的值是 FST 值,对角线上方的值是相应的 p 值。

尝试

library(readxl)
library(ggplot2)
library(reshape2)
library(stats)
library(Hmisc)
library(RColorBrewer)
###############################################################################
heatmap<-subset(heatmap, select = -1)
rownames(heatmap)<-c("NWAtl","CIV","SEN","MRT","MOR","PRT","ESP","PAL","LG",
                       "JN","TUN","MLT","MR","AEG")
heatmap(as.matrix(heatmap))

但是,我已经尝试了本网站之前的问题或 YouTube 教程中的多行编码,我希望最终结果看起来像我在之前的帖子中看到的那样:

enter image description here

我想用浅绿色标度对 p 值进行着色,其中

0
应为白色而不是空白。

r heatmap distance-matrix fst
1个回答
0
投票

您可以执行以下操作:

library(ggplot2)
library(reshape2)


fst_matrix <- as.matrix(read.table(text = "
        NWAtl   CIV SEN MRT MOR PRT ESP PAL LG  JN  TUN MLT MR  AEG
NWAtl   0   0   0   0   0   0.00098 0.00586 0.01953 0.01465 0.01855 0.00391 0.05078 0.00684 0
CIV 0.58701 0   0.24805 0.0625  0.00391 0   0   0   0   0   0   0   0.00195 0
SEN 0.46143 0.00365 0   0.68555 0.11523 0   0   0   0   0   0.00195 0   0.01953 0.01562
MRT 0.40702 0.02014 0.0000  0   0.34766 0   0   0   0   0.00391 0.00293 0   0.09082 0.05078
MOR 0.34654 0.05744 0.01748 0.00000 0   0.00098 0   0   0.00293 0.02734 0.03223 0.00098 0.17285 0.08496
PRT 0.14366 0.25279 0.16592 0.13095 0.07964 0   0.76855 0.0791  0.14648 0.20605 0.33594 0.13574 0.05664 0.00098
ESP 0.13075 0.26191 0.18752 0.15328 0.10177 0.00000 0   0.08398 0.10156 0.13281 0.13672 0.20117 0.02246 0
PAL 0.10058 0.36823 0.26087 0.21078 0.14975 0.02033 0.01352 0   0.32031 0.10938 0.06738 0.37109 0.02441 0
LG  0.13747 0.29423 0.19685 0.14836 0.09261 0.01159 0.01123 0.00027 0   0.55372 0.3291  0.10156 0.13184 0
JN  0.15804 0.25805 0.15377 0.10518 0.05754 0.0118  0.01614 0.01900 0.00000 0   0.42383 0.05371 0.31348 0.03906
TUN 0.18355 0.21649 0.12779 0.08634 0.03648 0.00129 0.01209 0.02700 0.00059 0   0   0.0459  0.18848 0.01465
MLT 0.0823  0.46264 0.33675 0.28355 0.2103  0.01988 0.00747 0.00000 0.02690 0.05412 0.05474 0   0.01562 0
MR  0.24169 0.16147 0.08179 0.04301 0.01424 0.05481 0.06864 0.08095 0.02960 0.00000 0.01714 0.14447 0   0.35547
AEG 0.35912 0.0965  0.0552  0.03027 0.01964 0.11375 0.13098 0.16291 0.10019 0.05367 0.06768 0.24109 0.00000 0
", header = TRUE, check.names = FALSE))

fst_data <- melt(fst_matrix)
colnames(fst_data) <- c("Row", "Column", "Value")

ggplot(fst_data, aes(x = Row, y = Column, fill = Value)) +
  geom_tile() +
  scale_fill_gradient(low = "lightblue", high = "blue") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_fixed()

返回者

enter image description here

但是,如果您绝对需要像图像示例这样的数字,您可以将代码更改为:

library(ggplot2)
library(reshape2)


fst_matrix <- as.matrix(read.table(text = "
        NWAtl   CIV SEN MRT MOR PRT ESP PAL LG  JN  TUN MLT MR  AEG
NWAtl   0   0   0   0   0   0.00098 0.00586 0.01953 0.01465 0.01855 0.00391 0.05078 0.00684 0
CIV 0.58701 0   0.24805 0.0625  0.00391 0   0   0   0   0   0   0   0.00195 0
SEN 0.46143 0.00365 0   0.68555 0.11523 0   0   0   0   0   0.00195 0   0.01953 0.01562
MRT 0.40702 0.02014 0.0000  0   0.34766 0   0   0   0   0.00391 0.00293 0   0.09082 0.05078
MOR 0.34654 0.05744 0.01748 0.00000 0   0.00098 0   0   0.00293 0.02734 0.03223 0.00098 0.17285 0.08496
PRT 0.14366 0.25279 0.16592 0.13095 0.07964 0   0.76855 0.0791  0.14648 0.20605 0.33594 0.13574 0.05664 0.00098
ESP 0.13075 0.26191 0.18752 0.15328 0.10177 0.00000 0   0.08398 0.10156 0.13281 0.13672 0.20117 0.02246 0
PAL 0.10058 0.36823 0.26087 0.21078 0.14975 0.02033 0.01352 0   0.32031 0.10938 0.06738 0.37109 0.02441 0
LG  0.13747 0.29423 0.19685 0.14836 0.09261 0.01159 0.01123 0.00027 0   0.55372 0.3291  0.10156 0.13184 0
JN  0.15804 0.25805 0.15377 0.10518 0.05754 0.0118  0.01614 0.01900 0.00000 0   0.42383 0.05371 0.31348 0.03906
TUN 0.18355 0.21649 0.12779 0.08634 0.03648 0.00129 0.01209 0.02700 0.00059 0   0   0.0459  0.18848 0.01465
MLT 0.0823  0.46264 0.33675 0.28355 0.2103  0.01988 0.00747 0.00000 0.02690 0.05412 0.05474 0   0.01562 0
MR  0.24169 0.16147 0.08179 0.04301 0.01424 0.05481 0.06864 0.08095 0.02960 0.00000 0.01714 0.14447 0   0.35547
AEG 0.35912 0.0965  0.0552  0.03027 0.01964 0.11375 0.13098 0.16291 0.10019 0.05367 0.06768 0.24109 0.00000 0
", header = TRUE, check.names = FALSE))

fst_data <- melt(fst_matrix)
colnames(fst_data) <- c("Row", "Column", "Value")

ggplot(fst_data, aes(x = Row, y = Column, fill = Value)) +
  geom_tile() +
  geom_text(aes(label = sprintf("%.4f", Value)), size = 3, color = "black") +
  scale_fill_gradient(low = "lightblue", high = "blue") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_fixed()

这会给你

enter image description here

但我不建议你这样做。这在审美上是令人不快的。

© www.soinside.com 2019 - 2024. All rights reserved.