R corrplot 中的标题太不居中且太高

Title in R corrplot too not centred and too high

我正在使用 corrplot 来可视化相关性,但是标题在图上方相当高,我想让它更接近。我该怎么做?

示例数据框:

"VADeaths" <-
  structure(c(11.7, 18.1, 26.9, 41, 66, 8.7, 11.7, 20.3, 30.9, 54.3, 15.4, 
  24.3, 37, 54.6, 71.1, 8.4, 13.6, 19.3, 35.1, 50), .Dim = c(5, 4),
  .Dimnames = list(c("50-54", "55-59", "60-64", "65-69", "70-74"),
  c("Rural Male", "Rural Female", "Urban Male", "Urban Female")))

计算相关性并可视化

library(corrplot)
cors = cor(VADeaths)

corrplot(cors,tl.col="black",title="Example Plot",mar=c(0,0,5,0),tl.offset = 1)

通过将边距扩展到情节上方的 5,我至少可以让标题出现在情节中,但无法弄清楚如何使标题更接近情节并居中于情节而不是 space 被标签占据。

上面看起来像这样:

我想要更像这样的东西(忽略字体)

我的实际地块的标签要小得多,所以标签和标题之间有大约 3-4 厘米的间隙。我没有发现增加 mar 的值可以解决问题。

您可以使用 mtext 来添加标题

corrplot(cors,tl.col="black", mar=c(0,0,5,0), tl.offset = 1)
mtext("Example Plot", at=2.5, line=-0.5, cex=2)

at 控制水平位置。 line 控制高度。 cex 大小。 ?mtext 查看更多选项

您可以使用 ggplot2 绘制相关图。

首先将相关数据转换为数据框。

library(reshape2)
cors <- cor(VADeaths)
cor_data <- reshape2::melt(
  cors, 
  varnames = paste0("demographic", 1:2), 
  value.name = "correlation"
)

然后画图

library(ggplot2)
ggplot(cor_data, aes(demographic1, demographic2, fill = correlation)) + 
  geom_tile() + 
  ggtitle("Correlation across demographics for VA deaths")