R vcd::mosaic overlapping labels

4.9k views Asked by At

I'm generating a mosaic plot with vcd::mosaic. But the text of the factors I was given are very long (cutting them is not an option, and with so many instances, introducing \n seems daunting), so there's an overlap in the texts, and I haven't been able to force the labels to go perpendicular to the axis.

This is what I'm trying:

a <- data.frame(x=sample(LETTERS[1:4],16,replace = TRUE), 
                y=rep(paste("very long label here at number", 1:4, paste=" "), 4))
mosaic(y ~ x, data= a, las= 2)

but this is what I get: enter image description here

I've also tryed par(las= 2) and par(las= 3) but none of those is able to force them into vertical alignment (las= 2 works well with mosaicplot, though. It's like vcd::mosaic overrides las either as a given parameter or as a default-set in par. I've played also with par(mar), but the labels are long enough to fool that workaround.

What can I do to get readable labels?

########## EDIT TO ADD: ##########

I've also tried this, to no avail:

mosaic(y ~ x, data= a, labeling_list= list(gp_text= gpar(las= 2)))

and

mosaic(y ~ x, data= a, labeling_list= list(rot_labels = c(0,90,0,0)))
  # Actually placed the "90" in the 4 positions

mosaic(y ~ x, data= a, labeling_list= list(rot_varnames = c(0,90,0,0)))
2

There are 2 answers

0
PavoDive On BEST ANSWER

Finally found it! Key searching docs:

?labelings
?labeling_border

In order to rotate the labels

mosaic(y ~ x, 
       data= a, 
       labeling= labeling_border(rot_labels = c(90,0,0,0), 
                                 just_labels = c("left", 
                                                 "center", 
                                                 "center", 
                                                 "center")))
2
pocuhkacumpo On

Try this:

+ theme(axis.text.x=element_text(angle=-25, hjust= .1))

it will rotate labels for 25 degrees (you can just copy and paste)