重新排序相关矩阵图的轴标签
Reorder axis labels of correlation matrix plot
我正在使用 ggcorrplot 构建相关矩阵,但输出以我不希望的方式对列重新排序。如何对列重新排序?
出于本示例的目的,我将使用在 R 中找到的 'mtcars' 数据集。生成最终输出后,我需要对列重新排序,因为它会不断重新排序为一种格式我不要。
注:代码来自网站,如下:http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization
library(ggcorrplot)
mydata <- mtcars
#correlation matrix
cormat <- round(cor(mydata),2)
library(reshape2)
melted_cormat <- melt(cormat)
head(melted_cormat)
library(ggplot2)
ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) +
geom_tile()
# Get upper triangle of the correlation matrix
get_upper_tri <- function(cormat){
cormat[lower.tri(cormat)]<- NA
return(cormat)
}
upper_tri <- get_upper_tri(cormat)
# Melt the correlation matrix
library(reshape2)
melted_cormat <- melt(upper_tri, na.rm = TRUE)
# Heatmap
library(ggplot2)
ggplot(data = melted_cormat, aes(Var2, Var1, fill = value))+
geom_tile(color = "white")+
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
这是我想要的相关矩阵,但我需要将列重新排序为与所示顺序不同的顺序。
任何帮助都会很棒。谢谢大家!
假设我们希望 x-axis
根据其名称 (labels
) 按字母顺序排序,y-axis
相同但顺序相反。以下代码有效(其余代码将相同)。
ggplot(data = melted_cormat, aes(reorder(Var2, -desc(as.character(Var2))),
reorder(Var1, desc(as.character(Var1))), fill = value))+
geom_tile(color = "white")+
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
我正在使用 ggcorrplot 构建相关矩阵,但输出以我不希望的方式对列重新排序。如何对列重新排序?
出于本示例的目的,我将使用在 R 中找到的 'mtcars' 数据集。生成最终输出后,我需要对列重新排序,因为它会不断重新排序为一种格式我不要。
注:代码来自网站,如下:http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization
library(ggcorrplot)
mydata <- mtcars
#correlation matrix
cormat <- round(cor(mydata),2)
library(reshape2)
melted_cormat <- melt(cormat)
head(melted_cormat)
library(ggplot2)
ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) +
geom_tile()
# Get upper triangle of the correlation matrix
get_upper_tri <- function(cormat){
cormat[lower.tri(cormat)]<- NA
return(cormat)
}
upper_tri <- get_upper_tri(cormat)
# Melt the correlation matrix
library(reshape2)
melted_cormat <- melt(upper_tri, na.rm = TRUE)
# Heatmap
library(ggplot2)
ggplot(data = melted_cormat, aes(Var2, Var1, fill = value))+
geom_tile(color = "white")+
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
这是我想要的相关矩阵,但我需要将列重新排序为与所示顺序不同的顺序。
任何帮助都会很棒。谢谢大家!
假设我们希望 x-axis
根据其名称 (labels
) 按字母顺序排序,y-axis
相同但顺序相反。以下代码有效(其余代码将相同)。
ggplot(data = melted_cormat, aes(reorder(Var2, -desc(as.character(Var2))),
reorder(Var1, desc(as.character(Var1))), fill = value))+
geom_tile(color = "white")+
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()