4

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)))
PavoDive
  • 6,322
  • 2
  • 29
  • 55
  • You could try with `rep(paste("very long label\n here at number\n", 1:4, paste=" "), 4)`, if it fits your requirement. –  Feb 15 '16 at 01:18
  • Pascal, thanks for your comment, but as I stated in the question the data is a lot more (and a lot more complex) than the reproducible example, so inputting `\n` will be very cumbersome. I'd like to know how to rotate the labels :'( – PavoDive Feb 15 '16 at 01:53
  • I don't think rotating the labels is a good solution. –  Feb 15 '16 at 02:01
  • @Pascal some of the labels in the mosaic plot have very small areas, which mean they're quite cramped to the sides. It doesn't matter if I split each string at 5 characters, they'll be one on top of the other. I understand you don't like rotating the labels, but could you show me how to do it? Thanks! – PavoDive Feb 15 '16 at 02:12
  • I am sorry, but you misinterpret what I am saying. –  Feb 15 '16 at 02:13

2 Answers2

13

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")))
PavoDive
  • 6,322
  • 2
  • 29
  • 55
-2

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)

pocuhkacumpo
  • 1
  • 1
  • 1