参考链接
新学到的内容
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"))
默认图例位置在右侧中间,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"))
当图例位于上下位置时,还可以设置图例标题的位置,应该是上下左右,用到的代码是
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"))
现在图例的标题是靠上巨作,如果居中的话可以继续加参数
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))
改变颜色条的宽度和高度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))
调整图例上显示的刻度
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热图可视化相关系数
代码
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()
接下来原文说对相关系数做聚类,以发现可能的关系,但是这里遇到个问题为什么原始数据要做转换呢,(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()
今天的内容就到这里
欢迎大家关注我的公众号
小明的数据分析笔记本