ggplot2画热图展示相关系数的简单小例子

参考链接

http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization

新学到的内容

upper.tri()
lower.tri()
可以分别获取矩阵数据的上三角和下三角

通过cormat[upper.tri(cormat)] <- NA能够将矩阵上三角数据赋值为NA

对图例的一些操作,包括:
调整右侧图例的上下位置
theme()函数中的legend.justification参数
比如简单的柱形图
首先是构造数据集

df<-data.frame(A=LETTERS[1:10],B=1:10)

画图


p1<-ggplot(df,aes(x=A,y=B))+
  geom_col(aes(fill=B))+
  scale_fill_gradient2(low="red",high = "blue",
                       mid="green",midpoint = 5)+
  theme(legend.justification = c(0,1))

p2<-ggplot(df,aes(x=A,y=B))+
  geom_col(aes(fill=B))+
  scale_fill_gradient2(low="red",high = "blue",
                       mid="green",midpoint = 5)+
  theme(legend.justification = c(0,0))
library(cowplot)
plot_grid(p1,p2,ncol=1,nrow = 2,labels = c("p1","p2"))
ggplot2画热图展示相关系数的简单小例子_第1张图片
image.png

默认图例位置在右侧中间,legend.justification=c(0,0) c(0,0)前一个0是x后一个0是y,如果图例在左右侧,只需要设置y,范围是0到1,1在顶部0在底部。如果图例在上下位置对应只需要设置x

p3<-ggplot(df,aes(x=A,y=B))+
  geom_col(aes(fill=B))+
  scale_fill_gradient2(low="red",high = "blue",
                       mid="green",midpoint = 5)+
  theme(legend.position = "top",
        legend.justification = c(0,0))

p4<-ggplot(df,aes(x=A,y=B))+
  geom_col(aes(fill=B))+
  scale_fill_gradient2(low="red",high = "blue",
                       mid="green",midpoint = 5)+
  theme(legend.position = "top",
        legend.justification = c(1,0))
plot_grid(p3,p4,ncol=1,nrow = 2,labels = c("p3","p4"))
ggplot2画热图展示相关系数的简单小例子_第2张图片
image.png

当图例位于上下位置时,还可以设置图例标题的位置,应该是上下左右,用到的代码是
guides(fill=guide_colorbar(title.position = "top"))

ggplot(df,aes(x=A,y=B))+
  geom_col(aes(fill=B))+
  scale_fill_gradient2(low="red",high = "blue",
                       mid="green",midpoint = 5)+
  theme(legend.position = "top",
        legend.justification = c(0,0))+
  guides(fill=guide_colorbar(title.position = "top"))

ggplot2画热图展示相关系数的简单小例子_第3张图片
image.png

现在图例的标题是靠上巨作,如果居中的话可以继续加参数 title.hjust = 0.5

ggplot(df,aes(x=A,y=B))+
  geom_col(aes(fill=B))+
  scale_fill_gradient2(low="red",high = "blue",
                       mid="green",midpoint = 5)+
  theme(legend.position = "top",
        legend.justification = c(0,0))+
  guides(fill=guide_colorbar(title.position = "top",title.hjust = 0.5))
ggplot2画热图展示相关系数的简单小例子_第4张图片
image.png

改变颜色条的宽度和高度barwidth = 5,barheight = 5

ggplot(df,aes(x=A,y=B))+
  geom_col(aes(fill=B))+
  scale_fill_gradient2(low="red",high = "blue",
                       mid="green",midpoint = 5,
                       limit=c(0,10))+
  theme(legend.position = "top",
        legend.justification = c(0,0))+
  guides(fill=guide_colorbar(title.position = "top",
                             title.hjust = 0.5,
                             barwidth = 5,barheight = 5))
ggplot2画热图展示相关系数的简单小例子_第5张图片
image.png

调整图例上显示的刻度

ggplot(df,aes(x=A,y=B))+
  geom_col(aes(fill=B))+
  scale_fill_gradient2(low="red",high = "blue",
                       mid="green",midpoint = 5,
                       limit=c(0,10),breaks=c(0,5,10),
                       label=c("A","B","C"))+
  theme(legend.position = "top",
        legend.justification = c(0,0))+
  guides(fill=guide_colorbar(title.position = "top",
                             title.hjust = 0.5,
                             barwidth = 5,barheight = 5,
                             ticks = T,
                             label = T))
ggplot2画热图展示相关系数的简单小例子_第6张图片
image.png
下面进入正题,ggplot2热图可视化相关系数

代码

library(reshape2)
library(ggplot2)
mydata <- mtcars[, c(1,3,4,5,6,7)]
head(mydata)
cormat <- round(cor(mydata),2)
get_upper_tri <- function(cormat){
  cormat[lower.tri(cormat)]<- NA
  return(cormat)
}
upper_tri <- get_upper_tri(cormat)
melted_cormat <- melt(upper_tri, na.rm = TRUE)
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()
ggplot2画热图展示相关系数的简单小例子_第7张图片
image.png

接下来原文说对相关系数做聚类,以发现可能的关系,但是这里遇到个问题为什么原始数据要做转换呢(1-cormat)/2这个代码有什么作用呢?

reorder_cormat <- function(cormat){
  # Use correlation between variables as distance
  dd <- as.dist((1-cormat)/2)
  hc <- hclust(dd)
  cormat <-cormat[hc$order, hc$order]
}

cormat <- reorder_cormat(cormat)
upper_tri <- get_upper_tri(cormat)

melted_cormat <- melt(upper_tri, na.rm = TRUE)
ggplot(melted_cormat, aes(Var2, Var1, fill = value))+
  geom_tile(color = "white")+
  geom_text(aes(Var2, Var1, label = value), color = "black", size = 4)+
  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()
ggplot2画热图展示相关系数的简单小例子_第8张图片
image.png

今天的内容就到这里

欢迎大家关注我的公众号
小明的数据分析笔记本

你可能感兴趣的:(ggplot2画热图展示相关系数的简单小例子)