我想用pairwise.wilcox.test的结果创建p值的热图。因此,在进行测试后,我重塑了结果:
test <- pairwise.wilcox.test(world$mean, world$con, p.adjust.method ="bonferroni",conf.level = 0.95)
test.result <- melt (test[[3]],na.rm=T)
结果如下:
X1 X2 value
1 europe africa 7.216273e-20
2 namerica africa 2.694228e-23
3 samerica africa 1.001953e-01
4 asia africa 3.515077e-66
5 europe europe NA
6 namerica europe 6.551144e-02
7 samerica europe 2.615654e-05
8 asia europe 2.148064e-09
9 europe namerica NA
10 namerica namerica NA
11 samerica namerica 4.894171e-10
12 asia namerica 3.642124e-02
13 europe samerica NA
14 namerica samerica NA
15 samerica samerica NA
16 asia samerica 5.999172e-25
然后我运行ggplot2脚本来获取热图:
test.result$X1 <- factor(test.result$X1, levels = c("europe", "namerica", "samerica", "asia"))
test.result$X2 <- factor(test.result$X2, levels = c("europe", "namerica", "samerica","asia"))
test.result$value<-cut(test.result$value, breaks=c(-Inf,0.001,0.05,1),right=F)
ggplot(data = test.result, aes(X1, X2, fill = value)) +
geom_tile(aes(fill=test.result$value),color="white") +
scale_fill_brewer(palette="Blues",name="p-Val")
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1)) +
coord_fixed()
正如你所看到的那样,图形没有在对角线上排序,有点草率......我不知道如何正确排列图形以获得对角线中的所有p值。谢谢你的帮助
答案 0 :(得分:1)
我认为这就是你想要的?:
调用您的数据tr
:
tr = structure(list(X1 = structure(c(2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L), .Label = c("asia", "europe",
"namerica", "samerica"), class = "factor"), X2 = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("africa",
"europe", "namerica", "samerica"), class = "factor"), value = c(7.216273e-20,
2.694228e-23, 0.1001953, 3.515077e-66, NA, 0.06551144, 2.615654e-05,
2.148064e-09, NA, NA, 4.894171e-10, 0.03642124, NA, NA, NA, 5.999172e-25
)), .Names = c("X1", "X2", "value"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16"))
Swarch的评论是正确的,因为我们需要具有相同级别/相同顺序的因子。评论没有奏效,因为africa
被省略了。修复:
lev = c("europe", "namerica", "samerica", "asia", "africa")
tr$X1 <- factor(tr$X1, levels = lev)
trX2 <- factor(tr$X2, levels = lev)
我们现在可以制作情节了。这里有一些更正
data$column
内使用aes()
- 使用不带引号的列名称。fill = value
次来电中指定ggplot()
,则无需为geom_tile()
图层重复此内容。value
似乎是连续的。 scale_fill_brewer
表示离散比例,因此不能在此处使用。没有,似乎很好,但您也可以尝试scale_fill_distiller
。+
。此代码有效:
ggplot(data = tr, aes(X1, X2, fill = value)) +
geom_tile(color = "white") +
theme_minimal() +
theme(axis.text.x = element_text(
angle = 45,
vjust = 1,
size = 12,
hjust = 1
)) +
coord_fixed()
另请注意,此处缺少1的确切对角线(与您的mtcars
示例不同),因为数据中缺少它。也就是说,africa
中完全没有X1
,asia
完全没有X2
。如果要绘制这些图块,则需要使用这些行来扩充数据。