R - change size of axis labels for corrplot
Asked Answered
I

4

14

I am using the following with 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))

This is created with a csv file available here.

The graph is fine and I can adjust the cl labels all I want. I've tried adjusting the labels on x and y axis with no impact. I looked at changing mar - yet I haven't found a way to. I was unsuccessful with trying to use cex.label to change size.

The question - how can I make the text appearing for corrplot (not the cl, and not in the grid) larger?

The two axes are the following data frames:

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, "Ask Questions" = 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)

The output from

str(lpp_axis1)

is

> 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 ...

The output from

packageDescription("corrplot")

indicates:

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 <[email protected]>
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 
> 

The corrplot maintainer wrote back with an alternate corrplot.r available here

Using the this corrplot and the example code below, the text size is acceptable. However, attempts to increase it also produce the same effects.

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))

Using an earlier version of the correlation circles available here, it is possible to adjust the text to one's heart desire. (Though the graph lacks some of the functionality of the later, more refined corrplot package.) cex can be used for cex. I may try to tweak the two to come up with a happy medium as time permits.

Using the older correlation circles script, the following code produces sufficiently large X and Y axis labels:

circle.corr(cor(lpp_axis1, lpp_axis2), bg = "gray50",   col = colorRampPalette(c("navyblue","white", "red"))(100), cex=1.5)
Incitement answered 19/3, 2011 at 2:26 Comment(1)
can you explain which x/y axis labels you are trying to change? A reproducible example helps. Here is one: data(mtcars); corr <- cor(mtcars); corrplot(corr, method = "number") --- produce that plot and then say which labels you want to increase.Malenamalet
M
19

Update2

Actually a real reproducible example is now, thanks to code and data being provided:

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,
                                 "Ask Questions" = 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))

The dev.new() allows you to have both on screen at once to compare, without splitting the plotting region into two panels.

The tl.offset seems to cause more problems than it is worth, so I have left it out. I include the two figures below:

With tl.cex = 1

corrplot with tl.cex = 1

With tl.cex = 2

corrplot with tl.cex = 2

As you can see, I can't reproduce the problem you are seeing; tl.cex is only changing the size of the size of the axis labels. Note this is without using tl.offset but the rest of the plotting code is the same as yours.

This is what I get from 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 <[email protected]>
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

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

Compare it with the one on your system and try the example above so we are running exactly the same code for comparison.


Original Example Here is a reproducible example:

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

Update

Ok, I see the problem now. With tl.offset, you push the labels away from the correlation graphic further out into the margins. This seems either a bug on infelicity in corrplot() as if you don't set tl.offset it scales the correlation graphic to accommodate the labels. The only solution I can see is to not set tl.offset at all, or set it to a smaller value Here is an extreme example:

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)

corrplot 4

You can improve things, by altering the relative dimensions of the plot device - if on screen, increase the width or height (or both) of the plot device window until all the labels are visible. If this is another device (pdf() or png() say), then you'll need to alter the dimensions of the device when you create it.


Original Which [The reproducible example] gives:

mtcars correlation plot

You aren't clear what the problem with the x and y axis labels, but corrplot() alters the plot margins to accommodate the labels. You have already stated the relative size of these x and y axis labels by setting argument tl.cex = 2. If you want the labels bigger, increase this value:

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

mtcars correlation plot 2

and if you want smaller labels, set tl.cex to a smaller value:

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

mtcars correlation plot 3

Given these are the only x and y labels on the plot, does this help? If not, which labels need altering?

Malenamalet answered 19/3, 2011 at 14:3 Comment(19)
@Gavin Simpson - That is the way it should work. However, changing tl.cex changes everything except the labels I'm trying to change. Using corrplot(cor(lpp_axis1,lpp_axis2), method=c("number"), tl.cex = 4) the other text increases, yet the text on the x and y axes stays tiny.Incitement
@Donnied are you running the latest version from CRAN? That is 0.30. What does packageDescription("corrplot")$version say?Malenamalet
@Donnied Apologies it should have been $Version with a capital V. Try: packageDescription("corrplot")$Version. The point is however, that I am running the latest version of corrplot and it does work for me and as documented, so you/we need to figure out why that is.Malenamalet
It says 0.30. I'm running it on Debian, but that shouldn't change things.Incitement
@Gavin - could it be in any way related to the dataframes I'm using as Axes?Incitement
What are lpp_axis1, and lpp_axis2? Are they both data frames? Does the example I provide with data(cars) work for you? Or do you get the odd behaviour you report for your example. Can you provide output of str(lpp_axis1) and the other object in your Question - just edit it and insert the output from R. Alternatively, if you can put the data somewhere temporarily that I can get, I'll see if I can reproduce on my machine.Malenamalet
That data(cars) example works exactly as displayed. The output of str(lpp_axis1) is: 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 ...Incitement
The csv file is here: misterdavis.org/r_wiki/r_results_1231_2010 The data frames in question are: 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, "Ask Questions" = 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)Incitement
@Gavin - "just edit it and insert the output from R" I'm sorry; what do you mean?Incitement
@Donnied you can edit your Q to include extra info. Chunks of code aren't easily handled well in comments. Look at the two comments you just posted - nigh on incomprehensible.Malenamalet
@Gavin - apologies. Hopefully, it is a bit more decipherable now.Incitement
@Donnied can you run the update 2 code from my answer (in now) and see what happens. Problem persists or not? If it persists, check the output of packageDescription() that I have also included in my updated answer.Malenamalet
@Gavin - here's a copy of what I get running your code tl.cex 1,2,and 6. The text gets progressively smaller. i.imgur.com/oZpsg.pngIncitement
Are you sure the text is getting smaller? To me, it looks like it is getting bigger certainly between plot 1 and 2, but you have such stupidly long labels that the function reserves so much space for the the labels that the change in plot size is obfuscating everything. There is an issue when you get too large with tl.cex - the function basically ignores you. At this point, you need to speak to the developers of the package because all reasonable attempts to make this work have failed. Either we don't know what the heck we are doing or there are bugs in their function that need solving.Malenamalet
@Gavin- Even when I make the plots very large the text size remains tiny in the corner. Thanks, I'll drop him an email and see what happens.Incitement
@Gavin - I dropped him an email. I'll post his response if there is one. There is some room to tweak the Axis labels but not much. I'll play around with that and a few other things. Thanks for your time.Incitement
@Donnied - you're welcome. Please do post back with comments or your own answer (that you can accept) so we can close off your R.Malenamalet
@Gavin - well the creator wrote back right away. He sent me an alternate corrplot.r that accepted alternate parameters. Given the code he provides, the text looks great. However, the same problem occurs when trying to increase the size of the labels beyond 2. I'll post the code and a link to the corrplot.r above.Incitement
Is there any better way to put numeric axis labels in corrplot?Mudskipper
U
4

You could try changing the point size of text with par. I think the default size is 12.
Adding a line like par( ps=14) before corrplot should make the text larger.

Unrestraint answered 19/3, 2011 at 12:52 Comment(5)
That will affect all text and @Donnied wants to alter only some labels.Malenamalet
Well.. At this point I might be happy with that if I could change the text I'm trying to change. When I change tl.cex - it's changing everything but the labels I want to change, the same is true for par.Incitement
par (ps = 12) + corrplot(cor(lpp_axis1,lpp_axis2), method=c("number"), tl.cex = 2) creates [URL=imgur.com/ABtdF][IMG]http://i.imgur.com/ABtdF.png[/IMG][/URL]Incitement
This par(ps=14) works with the older script; however, it writes over the graph.Incitement
a smaller ps = bigger cor plot and smaller text sizePember
U
3

Given the examples you added, you might have to increase the dimensions of the plot and set the outer margins to accommodate the length of your labels.

The current plot dimension can be accessed with par()$pin and the outer margins with par()$omi .

You can change the plot dimension and the outer margins by adapting the following example:

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)

enter image description here


Unrestraint answered 20/3, 2011 at 18:3 Comment(2)
but that is only because of the tl.offset in @Donnied's call. @Donnied claims, and shows in images in the comments to my answer that tl.cex is increasing the size of everything but the variable labels.Malenamalet
@Gavin - thanks for pointing that out. I had hopes that @David's answer would improve the situation; however, the text remains tiny.Incitement
D
0

cl.cex argument in corr() function should alter the size of the required labels.

Deadening answered 8/8 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.