ggplot2中如何不显示矩阵对角线数据?

发布于 2025-01-10 09:37:44 字数 1495 浏览 1 评论 0原文

我想绘制相关矩阵图。像这样

在此处输入图像描述

因此,我生成了一个矩阵数据并在 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()

我要解决的问题:

  1. 图形的位置在左下角;
  2. 图中不显示矩阵对角线数据。
  3. 如果我想要其他颜色(例如黑色)的背景,在绘制相关矩阵图之前是否需要先绘制图片?

I want to plot Correlation matrix plot. like this

enter image description here

so, I generated a matrix data and ploted in R.
but, I got a strange picture.

enter image description here

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:

  1. The position of the figure is in the lower left corner;
  2. Don't display the matrix diagonal data in picture.
  3. If I want the background of the other colors(such as black), do I need plot a picture before painting correlation matrix plot?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2025-01-17 09:37:44

要按照您提出的顺序回答您的问题:

  1. 您需要颠倒 y 轴变量的顺序以使下角包含您的图块。您可以通过将 Var2 设为一个因子,然后在因子 Var1 时使用其反向级别来实现此目的:
melted_cormat1$Var2 <- factor(melted_cormat1$Var2)
melted_cormat1$Var1 <- factor(melted_cormat1$Var1, rev(levels(melted_cormat1$Var2)))
  1. 要删除矩阵对角线,请删除 中的所有行>melted_cormat1 其中 Var1 == Var2
melted_cormat2 <- melted_cormat1[which(melted_cormat1$Var1 != melted_cormat1$Var2),]
  1. 要使用黑色面板背景进行绘图,请使用 theme(panel.background = element_rect(fill = "black"))
ggplot(melted_cormat2, 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() +
  theme(panel.background = element_rect(fill = "black"),
        panel.grid = element_blank())

结果是:

输入图片此处描述

To answer your questions in the order you asked them:

  1. You need to reverse the order of the y axis variables to get the lower corner to contain your tiles. You can do this by making Var2 a factor, then using its reverse levels for the levels when you factor Var1:
melted_cormat1$Var2 <- factor(melted_cormat1$Var2)
melted_cormat1$Var1 <- factor(melted_cormat1$Var1, rev(levels(melted_cormat1$Var2)))
  1. To remove the matrix diagonal, remove all the rows from melted_cormat1 where Var1 == Var2
melted_cormat2 <- melted_cormat1[which(melted_cormat1$Var1 != melted_cormat1$Var2),]
  1. To plot with a black panel background, use theme(panel.background = element_rect(fill = "black")):
ggplot(melted_cormat2, 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() +
  theme(panel.background = element_rect(fill = "black"),
        panel.grid = element_blank())

Which results in:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文