如何复制下半部分灰度系数和上半部分圆圈的相关图?
How to replicate correlation plot with greyscale coefficients in the lower half and circles in upper half?
我正在寻找复制 this correlation plot,或者至少尽可能接近它。
具体来说,我想要:
- 下半部分的相关值,根据绝对值在灰度上变化
- 上半部分的圆圈,具有不同的直径和色阶。
- 我希望能够编辑轴刻度标签,以便完整描述位于 y 轴上,数字参考位于 x 轴上
我已经比较接近了,但还没有足够精确地复制。我在下面用可重现的代码描述了我最接近的尝试。 corrplot
包裹让我离我最近。
# general preparation
library(car)
correlations = cor(mtcars)
- corrplot 包
library(corrplot)
corrplot.mixed(correlations,
upper = "number", #upper.col = ???
lower = "circle", #lower.col = ???
tl.pos = "lt", tl.col = "black", tl.cex = 0.5)
备注:
- 有一种方法可以制作灰度系数,但我不明白:https://rdrr.io/cran/corrplot/man/COL1.html
- 出于某些奇怪的原因,当我使用自己的数据(而不是
mtcar
)时,the coefficient colours don't match with the actual correlation values。我不能在这里给出可重现的代码示例,因为它可以很好地处理 mtcar
数据。
- cormat 包
source("http://www.sthda.com/upload/rquery_cormat.r")
rquery.cormat(mtcar)
- 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?
- corrgram 包
library(corrgram)
corrgram(correlations,
labels = indices_all,
lower.panel = "panel.fill",
upper.panel = "panel.cor")
一些其他注意事项:
- 似乎图的一半倾向于 运行 通过与示例图相反的对角线,但我想这不是一个大问题。
Out-of-the-box 选项又快又好。但是,当涉及到自定义时,恕我直言,使用 ggplot2
从头开始构建绘图可能是值得的。作为第一步,这涉及一些数据争论以使相关矩阵成为正确的形状。同样在这一步中,我将类别转换为 factor
s 和一个数字 ID。基于 id,我将数据拆分为上对角线值和下对角线值,然后可以使用 geom_point
和 geom_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)
我正在寻找复制 this correlation plot,或者至少尽可能接近它。
具体来说,我想要:
- 下半部分的相关值,根据绝对值在灰度上变化
- 上半部分的圆圈,具有不同的直径和色阶。
- 我希望能够编辑轴刻度标签,以便完整描述位于 y 轴上,数字参考位于 x 轴上
我已经比较接近了,但还没有足够精确地复制。我在下面用可重现的代码描述了我最接近的尝试。 corrplot
包裹让我离我最近。
# general preparation
library(car)
correlations = cor(mtcars)
- corrplot 包
library(corrplot)
corrplot.mixed(correlations,
upper = "number", #upper.col = ???
lower = "circle", #lower.col = ???
tl.pos = "lt", tl.col = "black", tl.cex = 0.5)
备注:
- 有一种方法可以制作灰度系数,但我不明白:https://rdrr.io/cran/corrplot/man/COL1.html
- 出于某些奇怪的原因,当我使用自己的数据(而不是
mtcar
)时,the coefficient colours don't match with the actual correlation values。我不能在这里给出可重现的代码示例,因为它可以很好地处理mtcar
数据。
- cormat 包
source("http://www.sthda.com/upload/rquery_cormat.r")
rquery.cormat(mtcar)
- 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?
- corrgram 包
library(corrgram)
corrgram(correlations,
labels = indices_all,
lower.panel = "panel.fill",
upper.panel = "panel.cor")
一些其他注意事项:
- 似乎图的一半倾向于 运行 通过与示例图相反的对角线,但我想这不是一个大问题。
Out-of-the-box 选项又快又好。但是,当涉及到自定义时,恕我直言,使用 ggplot2
从头开始构建绘图可能是值得的。作为第一步,这涉及一些数据争论以使相关矩阵成为正确的形状。同样在这一步中,我将类别转换为 factor
s 和一个数字 ID。基于 id,我将数据拆分为上对角线值和下对角线值,然后可以使用 geom_point
和 geom_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)