Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

corrplot with type = "upper" and long colname strings cuts off top labels #10

Closed
jharlap opened this issue Feb 9, 2015 · 7 comments
Closed
Assignees
Labels

Comments

@jharlap
Copy link

jharlap commented Feb 9, 2015

If you corrplot a correlation matrix where variables in the data have very long names, then the plot cuts off the top labels.

For example, the following code

set.seed(123)
rmat <- matrix(runif(100), ncol = 10)
colnames(rmat) <- c("the quick brown fox jumps over the lazy dog", "and then went to get ice cream", "A", "B", "C", "D", "E", "F", "G", "H")
M <- cor(rmat)
corrplot(M, type = "upper", tl.pos = "td",
         method = "circle", tl.cex = 0.5, tl.col = 'black',
         order = "hclust", diag = FALSE)

Produces the following plot:
image

Removing the tl.cex still has the problem, yielding the following plot:
image

Note that both plots also have a fairly excessive amount of white space to the left of the plot, but that is not the issue here.

As a workaround, I found that if I comment out line 183 of corrplot.R then the problem is reduced or resolved, although the colorlegend last value (-1) gets cut off the bottom of the plot.

@smcinerney
Copy link

The other clunky workaround is type = 'lower'

@ssouyris
Copy link

ssouyris commented Jul 18, 2015

Changing the size of the graphic device makes the trick:

png("file.png")
set.seed(123)
rmat <- matrix(runif(100), ncol = 10)
colnames(rmat) <- c("the quick brown fox jumps over the lazy dog", "and then went to get ice cream", "A", "B", "C", "D", "E", "F", "G", "H")
M <- cor(rmat)
corrplot(M, type = "upper", tl.pos = "td",
         method = "circle", tl.cex = 0.5, tl.col = 'black',
         order = "hclust", diag = FALSE)
dev.off()

file

png("file2.png", width = 480,height = 600)
set.seed(123)
rmat <- matrix(runif(100), ncol = 10)
colnames(rmat) <- c("the quick brown fox jumps over the lazy dog", "and then went to get ice cream", "A", "B", "C", "D", "E", "F", "G", "H")
M <- cor(rmat)
corrplot(M, type = "upper", tl.pos = "td",
         method = "circle", tl.cex = 0.5, tl.col = 'black',
         order = "hclust", diag = FALSE)
dev.off()

file2

@statnmap
Copy link

statnmap commented Nov 4, 2016

Hi,
First of all, thank you for package, it allows nice plots !
I also had the same problem with long label names, using type = "lower".

I went through the code of the function to find out a fix.
At some point, there is an iterative loop calculating xlim and ylim in parallel to text length, so that labels are not cut. However, I found that the calculation of xlim and ylim in this loop did not accounted for all type or tl.pos. Moreover, only the text size is used after this calculation and xlim and ylim are calculated fex lines later again.
I propose to calculate xlim and ylim only in the iterative loop, accounting for all cases, including text rotation. Please find my proposition attached. I tested with all possible tl.pos and it seems to work quite properly.

Because, I am here, I added two other propositions...

  1. Figure width. I added a new parameter to retrieve the suggested ratio between width and height of plots (because asp=1). I am working on linux and the grDevices trick does not work. Moreover, in one of my script, the figure is in a loop for many different plot having different colnames and I want the plot to be resized automatically to get nice plot without big white margins. Maybe this can help other people. At least, I added a message of suggested plot width.

  2. Bad text width calculation. For a reason I did not get, sometimes the iterative loop can not converge. Something to do with parameters of device. Default par() may have been changed somewhere and the 'oldpar' trick do not allow to retrieve a real cleaned device after running 'Corrplot' once. Thus, if reaching the maximum of the iterative loop, I added a message to suggest to clean the device and start again. This {plot.new(); dev.off()} helped me making the script working.

Hope this can help !
test_corrplot.txt

@vsimko vsimko assigned vsimko and unassigned taiyun Feb 8, 2017
@vsimko vsimko added this to the Corrplot Version 1.0 milestone Feb 8, 2017
vsimko added a commit to vsimko/corrplot that referenced this issue Feb 9, 2017
@vsimko
Copy link
Collaborator

vsimko commented Feb 9, 2017

Finally, I fixed this issue with the help of code from @statnmap . Your code is quite cryptic, but it seems to work when I added few additional tweaks.
All the plots in our vignette now seem OK. The only part I didn't check are horizontal labels. I also tested the plots only on my Linux machine.

Please test it also on Windows.
You can do this by temporarily installing my forked development version of corrplot:

devtools::install_github("vsimko/corrplot")

Then, try to generate HTML from vignettes/corrplot-intro.Rmd

vsimko added a commit that referenced this issue Feb 17, 2017
@vsimko vsimko closed this as completed Feb 17, 2017
@ghost
Copy link

ghost commented Jan 14, 2019

First, thanks Tiayun and Viliam for the wonderful tool. It is very much needed.

I have similar issue as statnmap. I am using windows. The lastest version from CRAN or GitHub won't work. However, statnmap's code work fantastically. Thank you statnmap!

@harrysouthworth
Copy link

I don't think the fix has been carried over to corrplot.mixed - latest version off github

@christopherball
Copy link

Please re-open this ticket, as this issue exists for corrplot.mixed.

@vsimko vsimko reopened this Mar 6, 2021
@taiyun taiyun closed this as completed May 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants