如何复制下半部分灰度系数和上半部分圆圈的相关图?

How to replicate correlation plot with greyscale coefficients in the lower half and circles in upper half?

我正在寻找复制 this correlation plot,或者至少尽可能接近它。

具体来说,我想要:

我已经比较接近了,但还没有足够精确地复制。我在下面用可重现的代码描述了我最接近的尝试。 corrplot 包裹让我离我最近。

    # general preparation
    library(car)
    correlations = cor(mtcars)
  1. corrplot 包
    library(corrplot)
    
    corrplot.mixed(correlations, 
                   upper = "number", #upper.col = ???
                   lower = "circle", #lower.col = ???
                   tl.pos = "lt", tl.col = "black", tl.cex = 0.5)

备注:

  1. cormat 包
    source("http://www.sthda.com/upload/rquery_cormat.r")

    rquery.cormat(mtcar)
  1. ggcorrplot
    library("ggcorrplot")
    
    # circles separate
    ggcorrplot(correlations, # correlation matrix
               method = "circle", # circles instead of squares
               type = "upper", # show only upped triangle
               show.diag = F, # don't show diagonal values (1)
               lab = F, # don't show cor coeffs
               outline.col = "white", # no outline of circles
               ggtheme = theme_bw, # theme
               colors = c("#440154FF","#238A8DFF","#FDE725FF"))
    
    # coefs separate
    ggcorrplot(correlations, # correlation matrix
               method = "circle", # circles instead of squares
               type = "upper", # show only upped triangle
               show.diag = F, # don't show diagonal values (1)
               lab = T, # don't show cor coeffs
               outline.col = NA, # don't show circles
               ggtheme = theme_bw, # theme
               colors = c("#440154FF","#238A8DFF","#FDE725FF"))
    
    # can't combine both plots?
  1. corrgram 包
    library(corrgram)
    
    corrgram(correlations,
             labels = indices_all,
             lower.panel = "panel.fill",
             upper.panel = "panel.cor")

一些其他注意事项:

Out-of-the-box 选项又快又好。但是,当涉及到自定义时,恕我直言,使用 ggplot2 从头开始​​构建绘图可能是值得的。作为第一步,这涉及一些数据争论以使相关矩阵成为正确的形状。同样在这一步中,我将类别转换为 factors 和一个数字 ID。基于 id,我将数据拆分为上对角线值和下对角线值,然后可以使用 geom_pointgeom_text 分别绘制这些值。除此之外,重要的是将 drop=FALSE 添加到 x 和 y 尺度以保持所有因子水平和正确的顺序。我还使用一些函数来获取所需的轴标签:

编辑:按照@AllanCameron 的建议,我添加了一个coord_equal 作为“最终”触摸,以获得漂亮的方形矩阵外观。感谢@RichtieSacramento,代码现在将绝对值映射到 size aes.

library(dplyr)
library(tidyr)
library(ggplot2)

correlations = cor(mtcars)

levels <- colnames(mtcars)

corr_long <- correlations %>%
  data.frame() %>% 
  mutate(row = factor(rownames(.), levels = levels), 
         rowid = as.numeric(row)) %>%
  pivot_longer(-c(row, rowid), names_to = "col") %>%
  mutate(col = factor(col, levels = levels),
         colid = as.numeric(col))

ggplot(corr_long, aes(col, row)) +
  geom_point(aes(size = abs(value), fill = value), 
             data = ~filter(.x, rowid > colid), shape = 21) +
  geom_text(aes(label = scales::number(value, accuracy = .01), color = abs(value)), 
            data = ~filter(.x, rowid < colid), size = 8 / .pt) +
  scale_x_discrete(labels = ~ attr(.x, "pos"), drop = FALSE) +
  scale_y_discrete(labels = ~ paste0(.x, " (", attr(.x, "pos"), ")"), drop = FALSE) +
  scale_fill_viridis_c(limits = c(-1, 1)) +
  scale_color_gradient(low = grey(.8), high = grey(.2)) +
  coord_equal() +
  guides(size = "none", color = "none") +
  theme(legend.position = "bottom", 
        panel.grid = element_blank(), 
        axis.ticks = element_blank()) +
  labs(x = NULL, y = NULL, fill = NULL)