使用 ggplot2 库的热图上的变量顺序 [重复]

Posted

技术标签:

【中文标题】使用 ggplot2 库的热图上的变量顺序 [重复]【英文标题】:Order of variables on heatmap using the ggplot2 library [duplicate] 【发布时间】:2021-11-06 06:30:12 【问题描述】:

我需要一些帮助来使用 ggplot 库对热图上的变量进行排序。

我想应用以下变量顺序,而不是现有顺序: V10 V9 V8 V7 V6 V1 V2 V3 V4 V5

提前非常感谢!!!

library(ggplot2)
library(reshape2)

dt <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep = ",")
cor.mat <- cor(dt[1:10], method = "spearman")
cor.mat.melt <- melt(cor.mat)
colnames(cor.mat.melt) <- c("x1","x2","Corelation")

ggplot(data = cor.mat.melt,
       aes(x = x1, y = x2)) +
  geom_tile(aes(fill = Corelation)) +
  scale_fill_gradientn(colours = rainbow(3)) +
  geom_text(aes(x = x1, y = x2, label = round(Corelation, 2))) +
  labs(x = "", y = "")

【问题讨论】:

将该列作为一个因素并设置您想要的级别顺序。谷歌搜索“site:***.com r ggplot 手动设置顺序”为您提供了很多选择 【参考方案1】:

我改变顺序的方法是将 x1 和 x2 变成因子并分配它们的水平。

library(ggplot2)
library(reshape2)
library(data.table)

dt <- read.table("http://archive.ics.uci.edu/ml/machine-learning- 
databases/wine/wine.data", sep = ",")
cor.mat <- cor(dt[1:10], method = "spearman")
cor.mat.melt <- melt(cor.mat)
colnames(cor.mat.melt) <- c("x1","x2","Corelation")
cor.mat.melt$x1 <- factor(cor.mat.melt$x1, levels = c("V10", "V9", "V8", "V7", "V6", 
                                                      "V1", "V2", "V3", "V4", "V5"))
cor.mat.melt$x2 <- factor(cor.mat.melt$x2, levels = c("V10", "V9", "V8", "V7", "V6", 
                                                      "V1", "V2", "V3", "V4", "V5"))



ggplot(data = cor.mat.melt,
       aes(x = x1, y = x2)) +
geom_tile(aes(fill = Corelation)) +
scale_fill_gradientn(colours = rainbow(3)) +
geom_text(aes(x = x1, y = x2, label = round(Corelation, 2))) +
labs(x = "", y = "")

【讨论】:

【参考方案2】:

您可以通过设置scale_x/y_discrete(limits = ...)来指定顺序。

library(ggplot2)
library(reshape2)
#> Warning: package 'reshape2' was built under R version 4.1.1

dt <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep = ",")
cor.mat <- cor(dt[1:10], method = "spearman")

cor.mat.melt <- melt(cor.mat)
colnames(cor.mat.melt) <- c("x1","x2","Corelation")

my_order <- paste0("V", c(10, 9, 8, 7, 6, 1, 2, 3, 4, 5))

ggplot(data = cor.mat.melt,
       aes(x = x1, y = x2)) +
  geom_tile(aes(fill = Corelation)) +
  scale_fill_gradientn(colours = rainbow(3)) +
  geom_text(aes(x = x1, y = x2, label = round(Corelation, 2))) +
  scale_x_discrete(limits = my_order) +
  scale_y_discrete(limits = my_order) +
  labs(x = "", y = "")

由reprex package (v2.0.1) 于 2021-09-09 创建

【讨论】:

【参考方案3】:

这是您可以做到的一种方法。

library(ggplot2)
library(reshape2)

dt <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep = ",")
cor.mat <- cor(dt[1:10], method = "spearman")
cor.mat.melt <- melt(cor.mat)
colnames(cor.mat.melt) <- c("x1","x2","Corelation")
order <- c('V10', 'V9', 'V8', 'V7', 'V6', 'V1', 'V2', 'V3', 'V4', 'V5')
ggplot(data = cor.mat.melt,
       aes(x =factor(x1, level = order), y =factor(x2, level = order))) +
  geom_tile(aes(fill = Corelation)) +
  scale_fill_gradientn(colours = rainbow(3)) +
  geom_text(aes(x = x1, y = x2, label = round(Corelation, 2))) +
  labs(x = "", y = "")

【讨论】:

以上是关于使用 ggplot2 库的热图上的变量顺序 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

R:使用 ggplot2 的离散热图

R:如何使用 ggplot2 创建一个半色半数的热图?

ggplot2 热图绘制

使用 ggplot2 在 r 中为热图准备数据框

使用 ggplot2 将显着性级别添加到矩阵相关热图

具有范围值颜色的 ggplot2 热图