5

I am using ggpairs and while plotting the matrix, I receive a matrix as follows

enter image description here

As you can see, some of the text length is large and hence the text is not seen completely. Is there anyway that I can wrap the text so that it is visible completely.

Code

ggpairs(df) 

I want the text to wrap so that it can be seen something like this

enter image description here

Hardik Gupta
  • 4,700
  • 9
  • 41
  • 83

1 Answers1

11

You can use the labeller argument of ggpairs to pass a function to be applied to the facet strip text.

ggplot does have a nice ready function label_wrap_gen() that wrap the long labels.

By default ggpairs use the column names as labels, and those can't contain spaces. label_wrap_gen() need spaces to split the labels on multiple rows.

This is a solution:

library(ggplot2)
library(GGally)
df <- iris

colnames(df) <- make.names(c('Long colname', 
                  'Quite long colname', 
                  'Longer tha usual colname',
                  'I\'m not even sure this should be a colname',
                  'The ever longest colname that one should be allowed to use'))

ggpairs(df, 
        columnLabels = gsub('.', ' ', colnames(df), fixed = T), 
        labeller = label_wrap_gen(10))

GGamba
  • 13,140
  • 3
  • 38
  • 47