如何安排草率的热图 r
How to arange a sloppy heatmap r
我想创建一个 p 值热图,结果为 pairwise.wilcox.test。因此,在执行测试后,我重塑了结果:
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 值。感谢您的帮助
我要找的图是这样的:
我想这就是你想要的?:
调用你的数据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 的评论是正确的,因为我们需要因子具有相同的 levels/same 顺序。注释没有完全起作用,因为 africa
被省略了。修复:
lev = c("europe", "namerica", "samerica", "asia", "africa")
tr$X1 <- factor(tr$X1, levels = lev)
trX2 <- factor(tr$X2, levels = lev)
我们现在可以画图了。此处进行一些更正
- 从不 在
aes()
中使用 data$column
- 使用不带引号的列名。
- 如果您在顶部
ggplot()
调用中指定 fill = value
,则无需为 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
示例不同),因为您的数据中缺少它。即 X1
中完全没有 africa
,X2
中完全没有 asia
。如果要绘制这些图块,则需要使用这些行来扩充数据。
我想创建一个 p 值热图,结果为 pairwise.wilcox.test。因此,在执行测试后,我重塑了结果:
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 值。感谢您的帮助
我要找的图是这样的:
我想这就是你想要的?:
调用你的数据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 的评论是正确的,因为我们需要因子具有相同的 levels/same 顺序。注释没有完全起作用,因为 africa
被省略了。修复:
lev = c("europe", "namerica", "samerica", "asia", "africa")
tr$X1 <- factor(tr$X1, levels = lev)
trX2 <- factor(tr$X2, levels = lev)
我们现在可以画图了。此处进行一些更正
- 从不 在
aes()
中使用data$column
- 使用不带引号的列名。 - 如果您在顶部
ggplot()
调用中指定fill = value
,则无需为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
示例不同),因为您的数据中缺少它。即 X1
中完全没有 africa
,X2
中完全没有 asia
。如果要绘制这些图块,则需要使用这些行来扩充数据。