1

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

double-beep
  • 5,031
  • 17
  • 33
  • 41
Wilmer E. Henao
  • 4,094
  • 2
  • 31
  • 39

2 Answers2

2

play around with rmar and cmar

?textplot : rmar, cmar: Space between rows or columns, in fractions of the size of the letter 'M'.

library(gplots)

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

enter image description here

rawr
  • 20,481
  • 4
  • 44
  • 78
  • I really like this approach. The only problem is that it also expands all other rows in the matrix. A way to expand only the title? – Wilmer E. Henao Mar 21 '14 at 20:18
  • are you using rstudio? maybe you can adjust the aspect ratio to get closer to what you want. I've played around with an uglier method which is just a slight hack to the textplot matrix method. Currently, it calls a new plot each time, so basically what I did was suppress that so that you can plot one on top of another (I tried passing `add = TRUE` at first, but that didn't work). Then it is just a matter of making the colors transparent in the first plot with just column names and doing the opposite for the second plot. Might try the aspect ratio first. – rawr Mar 21 '14 at 21:04
0

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

Wilmer E. Henao
  • 4,094
  • 2
  • 31
  • 39