Search code examples
rgplots

How to wrap colnames without covering first row?


How can I plot a text that wraps and covers two lines in such a way that it does not cover the next row? Here's an example:

library(gplots)

a <- data.frame(a = c(1,2,3), b = c(4,5,6))
colnames(a)[1] <- "wrap\ntext"
textplot(a)

Sample image error

It can be easily seen that the word text from the title covers the number one (1).


Solution

  • I definitely had to come back and solve this problem. I think the best solution is to insert an empty row and to delete the first row name. Of course if you want it to extend a further line, add 2 empty rows and so on

    library(gplots)
    
    a <- data.frame(a = c(1,2,3), b = c(4,5,6))
    colnames(a)[1] <- "wrap\ntext"
    textplot(a)
    
    ## And here is the solution
    
    a <- rbind(c("", ""), a)  ## Insert empty row
    rownames(a)<- c("", 1:(dim(a)[1]-1)) ## delete first row name and name other indices
    textplot(a)
    

    Sample image error