【问题标题】:R - change size of axis labels for corrplotR - 更改 corrplot 轴标签的大小
【发布时间】:2011-07-18 14:14:25
【问题描述】:

我将以下内容与corrplot 一起使用:

require("corrplot") ## needs the corrplot package
corrplot(cor(lpp_axis1, lpp_axis2), method=c("number"), bg = "grey10",
         addgrid.col = "gray50", tl.offset = 2, tl.cex=2,
         tl.col = "black", 
         col = colorRampPalette(c("yellow","green","navyblue"))(100))

这是使用可用的 csv 文件创建的 here

图表很好,我可以随意调整 cl 标签。我尝试调整 x 和 y 轴上的标签而没有影响。我看着改变mar - 但我还没有找到办法。我尝试使用 cex.label 更改大小没有成功。

问题 - 如何使 corrplot(不是 cl,不在网格中)出现的文本更大?

两个轴是以下数据框:

lpp_axis1 <- data.frame("Compile Source Code" = Q3A.1, "View Source Code" = Q3A.2, "Change Source Code" = Q3A.3, "Write Documentation" = Q3A.8, "File Bug Reports"= Q3B.3, "s" = Q3B.5, "Provide Answers" = Q3B.6, "Overall Participation" = Q3a3bConsolidated)

lpp_axis2 <- data.frame("Identification" = Q1,"Overall Learning" = Q6Consolidated,  "Learning Programming" = Q6.1,  "Learning about Computers" = Q6.2, "Learning Teamwork" =  Q6.3)

输出

str(lpp_axis1)

> str(lpp_axis1)
'data.frame':   4603 obs. of  8 variables:
 $ Compile.Source.Code  : int  4 2 3 2 2 2 3 2 2 0 ...
 $ View.Source.Code     : int  4 2 1 1 2 2 3 1 1 0 ...
 $ Change.Source.Code   : int  4 1 0 1 2 1 2 1 1 0 ...
 $ Write.Documentation  : int  4 1 2 2 3 0 3 0 1 0 ...
 $ File.Bug.Reports     : int  4 4 1 2 2 0 2 0 0 0 ...
 $ Ask.Questions        : int  4 4 2 4 2 1 2 1 3 0 ...
 $ Provide.Answers      : int  2 4 1 4 4 0 3 1 3 0 ...
 $ Overall.Participation: int  49 26 14 32 31 8 27 10 15 0 ...

来自

的输出
packageDescription("corrplot")

表示:

Package: corrplot
Type: Package
Title: visualization of a correlation matrix
Version: 0.30
Date: 2010-05-30
Author: Taiyun Wei
Suggests: seriation, cairoDevice, Cairo,
Maintainer: Taiyun Wei <weitaiyun@gmail.com>
Description: The corrplot package is a graphical display of a
        correlation matrix, confidence interval. It also contains some
        algorithms to do matrix reordering.
License: GPL-2 | GPL-3
LazyLoad: yes
URL: http://corrplot.r-forge.r-project.org
Repository: CRAN
Repository/R-Forge/Project: corrplot
Repository/R-Forge/Revision: 45
Date/Publication: 2010-05-31 07:44:14
Packaged: 2010-05-30 20:39:16 UTC; rforge
Built: R 2.11.1; ; 2011-03-19 00:22:49 UTC; unix

-- File: /home/user/R/x86_64-pc-linux-gnu-library/2.11/corrplot/Meta/package.rds 
> 

corrplot 维护者回复了一个备用 corrplot.r 可用here

使用这个 corrplot 和下面的示例代码,文本大小是可以接受的。但是,尝试增加它也会产生相同的效果。

source("http://misterdavis.org/R_info/corrplot.r")
corrplot(cor(lpp_axis1, lpp_axis2), addn=T,
         addgrid.col = "gray50", tl.cex=2, assign.col="min2max",
         tl.col = "black", cl.ratio=0.4, addcolor="no",
         col = colorRampPalette(c("yellow","green","blue"))(100))

使用here 可用的早期版本的相关圈,可以根据自己的内心愿望调整文本。 (虽然该图缺少后来更完善的 corrplot 包的一些功能。) cex 可用于 cex。如果时间允许,我可能会尝试调整这两者以找到一个快乐的媒介。

使用较旧的相关圆脚本,以下代码会生成足够大的 X 和 Y 轴标签:

circle.corr(cor(lpp_axis1, lpp_axis2), bg = "gray50",   col = colorRampPalette(c("navyblue","white", "red"))(100), cex=1.5)

【问题讨论】:

  • 您能解释一下您要更改哪些 x/y 轴标签吗?一个可重现的例子会有所帮助。这是一个:data(mtcars); corr &lt;- cor(mtcars); corrplot(corr, method = "number") --- 制作该图,然后说出您要增加哪些标签。

标签: r plot font-size correlation r-corrplot


【解决方案1】:

您可以尝试使用 par 更改文本的磅值。我认为默认大小是 12。
corrplot 之前添加像par( ps=14) 这样的行应该会使文本变大。

【讨论】:

  • 这将影响所有文本,@Donnied 想更改一些标签。
  • 嗯.. 在这一点上,如果我可以更改我要更改的文本,我可能会对此感到满意。当我更改 tl.cex - 除了我想要更改的标签之外,它正在更改所有内容,par 也是如此。
  • par (ps = 12) + corrplot(cor(lpp_axis1,lpp_axis2), method=c("number"), tl.cex = 2) 创建 [URL=imgur.com/ABtdF][IMG]http://i.imgur.com/ABtdF.png[/IMG][/URL]
  • 此 par(ps=14) 适用于旧脚本;但是,它会覆盖图表。
【解决方案2】:

更新2

由于提供了代码和数据,现在实际上是一个真正可重现的示例:

d1 <- read.csv(url("http://misterdavis.org/r_wiki/r_results_1231_2010"))
lpp_axis1 <- with(d1, data.frame("Compile Source Code" = Q3A.1,
                                 "View Source Code" = Q3A.2,
                                 "Change Source Code" = Q3A.3, 
                                 "Write Documentation" = Q3A.8, 
                                 "File Bug Reports"= Q3B.3,
                                 "s" = Q3B.5,
                                 "Provide Answers" = Q3B.6,
                                 "Overall Participation" = Q3a3bConsolidated))

lpp_axis2 <- with(d1, data.frame("Identification" = Q1,
                                 "Overall Learning" = Q6Consolidated,  
                                 "Learning Programming" = Q6.1,
                                 "Learning about Computers" = Q6.2, 
                                 "Learning Teamwork" =  Q6.3))

corrplot(cor(lpp_axis1, lpp_axis2), method=c("number"), bg = "grey10",
         addgrid.col = "gray50", tl.cex=1,
         tl.col = "black", 
         col = colorRampPalette(c("yellow","green","navyblue"))(100))
dev.new()
corrplot(cor(lpp_axis1, lpp_axis2), method=c("number"), bg = "grey10",
         addgrid.col = "gray50", tl.cex=2,
         tl.col = "black", 
         col = colorRampPalette(c("yellow","green","navyblue"))(100))

dev.new() 允许您同时在屏幕上显示两者以进行比较,而无需将绘图区域分成两个面板。

tl.offset 似乎造成的问题多于其价值,因此我将其忽略了。我包括以下两个数字:

tl.cex = 1

tl.cex = 2

如您所见,我无法重现您所看到的问题; tl.cex 只是改变轴标签大小的大小。请注意,这没有使用tl.offset,但其余的绘图代码与您的相同。

这是我从packageDescription()得到的:

R> packageDescription("corrplot")
Package: corrplot
Type: Package
Title: visualization of a correlation matrix
Version: 0.30
Date: 2010-05-30
Author: Taiyun Wei
Suggests: seriation, cairoDevice, Cairo,
Maintainer: Taiyun Wei <weitaiyun@gmail.com>
Description: The corrplot package is a graphical display of a
        correlation matrix, confidence interval. It also contains some
        algorithms to do matrix reordering.
License: GPL-2 | GPL-3
LazyLoad: yes
URL: http://corrplot.r-forge.r-project.org
Repository: CRAN
Repository/R-Forge/Project: corrplot
Repository/R-Forge/Revision: 45
Date/Publication: 2010-05-31 07:44:14
Packaged: 2010-05-30 20:39:16 UTC; rforge
Built: R 2.13.0; ; 2011-04-01 12:33:21 UTC; unix

-- 文件:/home/gavin/R/libs/corrplot/Meta/package.rds

将它与您系统上的进行比较并尝试上面的示例,这样我们运行的代码完全相同。


原始示例 这是一个可重现的示例:

require(corrplot)
data(mtcars)
corr <- cor(mtcars)
corrplot(corr, method = "number", tl.cex = 2)

更新

好的,我现在看到了问题。使用tl.offset,您可以将标签从相关图形推到更远的边缘。这似乎是corrplot() 中的错误错误,好像您没有设置tl.offset 它会缩放相关图形以适应标签。我能看到的唯一解决方案是根本不设置tl.offset,或者将其设置为更小的值。这是一个极端的例子:

layout(matrix(1:2, ncol = 2))
corrplot(corr, method = "number", tl.cex = 2, tl.offset = 3)
corrplot(corr, method = "number", tl.cex = 2)
layout(1)

可以通过更改绘图设备的相对尺寸来改进事情 - 如果在屏幕上,增加绘图设备窗口的宽度或高度(或两者),直到所有标签都可见。如果这是另一台设备(pdf()png() 说),那么您需要在创建设备时更改设备的尺寸。


原创 [The reproducible example] 给出的:

您不清楚 x 和 y 轴标签有什么问题,但corrplot() 会更改绘图边距以适应标签。您已经通过设置参数tl.cex = 2 说明了这些 x 和 y 轴标签的相对大小。如果您希望标签更大,请增加此值:

corrplot(corr, method = "number", tl.cex = 4)

如果您想要更小的标签,请将tl.cex 设置为更小的值:

corrplot(corr, method = "number", tl.cex = 0.8)

鉴于这些是情节上仅有的 x y 标签,这有帮助吗?如果没有,哪些标签需要更改?

【讨论】:

  • @Gavin Simpson - 这就是它应该工作的方式。但是,更改 tl.cex 会更改除我要更改的标签之外的所有内容。使用 corrplot(cor(lpp_axis1,lpp_axis2), method=c("number"), tl.cex = 4) 其他文本增加,但 x 和 y 轴上的文本保持很小。
  • @Donnied 您运行的是 CRAN 的最新版本吗?那是0.30。 packageDescription("corrplot")$version 说什么?
  • @Donnied 抱歉,它应该是$Version,大写为V。试试:packageDescription("corrplot")$Version。然而,重点是,我正在运行最新版本的corrplot并且确实对我有用并且如文档所述,所以你/我们需要弄清楚为什么是。
  • 它说 0.30。我在 Debian 上运行它,但这不会改变任何事情。
  • @Gavin - 它可能与我用作轴的数据帧有任何关系吗?
【解决方案3】:

鉴于您添加的示例,您可能需要增加绘图的尺寸并设置外边距以适应标签的长度。

可以使用par()$pin 访问当前绘图尺寸,使用par()$omi 访问外边距。

您可以通过修改以下示例来更改绘图尺寸和外边距:

require("corrplot")         ## needs the corrplot package  
data(mtcars)  
  corr <- cor(mtcars)    
par(pin=c(7,7))              ##  (width, height) in inches    
par(omi=c(0,1,1,0.5))        ## (bottom, left, top, right)  in inches  
corrplot(corr, method="number", tl.cex = 2)


【讨论】:

  • 但这只是因为@Donnied 的电话中的tl.offset。 @Donnied 声称,并在我的回答的 cmets 中的图像中显示 tl.cex 正在增加除变量标签之外的所有内容的大小。
  • @Gavin - 感谢您指出这一点。我曾希望@David 的回答能改善这种情况;但是,文本仍然很小。
猜你喜欢
  • 2016-06-28
  • 1970-01-01
  • 2017-05-31
  • 2015-10-11
  • 1970-01-01
  • 2022-09-28
  • 2016-04-25
  • 2018-07-20
  • 2013-02-03
相关资源
最近更新 更多