【问题标题】:Title in R corrplot too not centred and too highR corrplot中的标题太不居中且太高
【发布时间】:2017-01-01 08:24:44
【问题描述】:

我正在使用 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,我至少可以让标题出现在情节中,但无法弄清楚如何使标题更接近情节并以情节为中心而不是占用的空间按标签。

上面是这样的:

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

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

【问题讨论】:

  • 根据corrplot 绘制图形的方式,您没有简单的编程方法可以根据函数输入或输出知道框边界在哪里。我认为您真正最好的选择是采用 source of corrplor 并对其进行修改,以便您可以 (a) 知道框边界在哪里,或者 (b) 自己找到一种方法将标题居中。
  • @dww 感谢您修复我的问题中的图像 - 我如何控制图像的大小?我想避免将来有人需要编辑我的问题。

标签: r r-corrplot


【解决方案1】:

您可以使用 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")

【讨论】:

    【解决方案2】:

    您可以改用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查看更多选项

    【讨论】:

    • 你需要side = 3吗?
    • @r2evans,side = 3 是默认值
    • 嗯,好电话,对不起。 (我一直更喜欢明确表达,所以我从来没有检查过。)
    • 我尝试在我的 corrplot 代码之后运行这一行,但遇到错误:“mtext 中的错误(“数值变量的相关表”,at = 2.5,line = -0.5,:plot .new 尚未被调用)。有什么想法可以解决这个问题吗?谢谢
    猜你喜欢
    • 2017-06-26
    • 2020-06-08
    • 2011-02-13
    • 2013-05-21
    • 1970-01-01
    • 2011-08-27
    • 2013-05-23
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多