Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R vcd::mosaic overlapping labels

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)))
like image 285
PavoDive Avatar asked Dec 18 '22 19:12

PavoDive


1 Answers

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")))
like image 183
PavoDive Avatar answered Jan 05 '23 17:01

PavoDive