ggplot2中如何不显示矩阵对角线数据?
我想绘制相关矩阵图。像这样
因此,我生成了一个矩阵数据并在 R 中绘制。 但是,我得到了一张奇怪的照片。
这是我的代码。
aa1=data.frame(eada=c(1.1,5,0,0,0,0),
goke=c(2.2,5,0,0,0,0),
adet=c(1.0,5,0,0,0,0),
feag=c(2.3,5,0,0,0,0),
edep=c(2.5,5,0,0,0,0),
jate=c(1.5,7,0,0,0,0),
pafe=c(2.6,8,0,0,0,0),
bink=c(3.3,6,0,0,0,0),
culp=c(2.1,6,0,0,0,0),
soit=c(1.3,6,0,0,0,0),
yosp=c(2.1,5,0,0,0,0),
wiso=c(2.3,8,0,0,0,0))
as.data.frame(lapply(aa1,as.numeric))
cormat1=round(cor(aa1),2)
# Get lower triangle of the correlation matrix
get_lower_tri<-function(cormat1){
cormat1[upper.tri(cormat1)] <- NA
return(cormat1)}
lower_tri <- get_lower_tri(cormat1)
t(lower_tri)
melted_cormat1 <- melt(lower_tri, na.rm = TRUE)
ggplot(melted_cormat1, aes(Var2, Var1, fill = value))+
geom_tile(color = "white")+
scale_x_discrete(position = "top") +
scale_fill_gradient2(low = "#8134af", high = "#C00000", mid = "white") +
theme_bw()
我要解决的问题:
- 图形的位置在左下角;
- 图中不显示矩阵对角线数据。
- 如果我想要其他颜色(例如黑色)的背景,在绘制相关矩阵图之前是否需要先绘制图片?
I want to plot Correlation matrix plot. like this
so, I generated a matrix data and ploted in R.
but, I got a strange picture.
this is my code.
aa1=data.frame(eada=c(1.1,5,0,0,0,0),
goke=c(2.2,5,0,0,0,0),
adet=c(1.0,5,0,0,0,0),
feag=c(2.3,5,0,0,0,0),
edep=c(2.5,5,0,0,0,0),
jate=c(1.5,7,0,0,0,0),
pafe=c(2.6,8,0,0,0,0),
bink=c(3.3,6,0,0,0,0),
culp=c(2.1,6,0,0,0,0),
soit=c(1.3,6,0,0,0,0),
yosp=c(2.1,5,0,0,0,0),
wiso=c(2.3,8,0,0,0,0))
as.data.frame(lapply(aa1,as.numeric))
cormat1=round(cor(aa1),2)
# Get lower triangle of the correlation matrix
get_lower_tri<-function(cormat1){
cormat1[upper.tri(cormat1)] <- NA
return(cormat1)}
lower_tri <- get_lower_tri(cormat1)
t(lower_tri)
melted_cormat1 <- melt(lower_tri, na.rm = TRUE)
ggplot(melted_cormat1, aes(Var2, Var1, fill = value))+
geom_tile(color = "white")+
scale_x_discrete(position = "top") +
scale_fill_gradient2(low = "#8134af", high = "#C00000", mid = "white") +
theme_bw()
I want to solve the questions:
- The position of the figure is in the lower left corner;
- Don't display the matrix diagonal data in picture.
- If I want the background of the other colors(such as black), do I need plot a picture before painting correlation matrix plot?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要按照您提出的顺序回答您的问题:
Var2
设为一个因子,然后在因子Var1
时使用其反向级别来实现此目的:中的所有行>melted_cormat1
其中Var1 == Var2
theme(panel.background = element_rect(fill = "black"))
:结果是:
To answer your questions in the order you asked them:
Var2
a factor, then using its reverse levels for the levels when you factorVar1
:melted_cormat1
whereVar1 == Var2
theme(panel.background = element_rect(fill = "black"))
:Which results in: