rmosaic-plot

Is it possible to change the fontsize of barplot tick mark labels with pairs_barplot in R package vcd?


With a large number of variables, the sizing of tick mark labels is too large in barplots along the diagonal of a mosaic pairs plot created with pairs_barplot() in the R vcd package. Is there any way to make them smaller? Here is a minimal working example:

library(vcd)
#> Loading required package: grid
library(vcdExtra)
#> Loading required package: gnm
pairs(table(ICU[,c(1,3:9)]), 
      diag_panel = pairs_barplot(
        gp_vartext = gpar(fontsize = 10, fontface = 2),
        gp_leveltext = gpar(fontsize = 8),
        abbreviate = 1,
        var_offset = 1.25))

Created on 2021-10-16 by the reprex package (v2.0.1)


Solution

  • Currently, you cannot set gpar() for drawing the y-axis of the bar plot explicitly. There are two general workarounds, though: (1) Not messing with the font sizes but instead plotting on a bigger device. (2) Setting an outer viewport with a different gpar(fontsize = ...) that is used as the viewports further down in the plot.

    (1) Bigger device

    For illustration I use a png() device here because the PNG graphic is what I embed on StackOverflow. But, of course, you could use the same trick on other devices including those that you do not create yourself but via chunk options in R/Markdown etc.

    I use a device size of 13 x 13 inches (as opposed to a more common setting of 6 x 6 or 7 x 7 inches). Then I can omit all of the gpar() settings because the device is large enough to accomodate the default parameters. I still set abbreviate and var_offset, though.

    png("pairs1.png", height = 13, width = 13, units = "in", res = 100)
    
    pairs(table(ICU[, c(1, 3:9)]),
      diag_panel = pairs_barplot(abbreviate = 1, var_offset = 1.25))
    
    dev.off()
    

    pairs.table on bigger device

    (2) Outer viewport

    Alternatively, I can create a new grid page myself and push a viewport with gpar(fontsize = 7) used as the default in this viewport and its children. Then I keep your gpar() settings in pairs_barplot() and just add newpage = FALSE in the pairs() call because I want to use the page I already created.

    Then all font sizes are decreased so that plotting on a 7 x 7 inches device works fine.

    png("pairs2.png", height = 7, width = 7, units = "in", res = 150)
    
    grid.newpage()
    pushViewport(viewport(gp = gpar(fontsize = 7)))
    
    pairs(table(ICU[, c(1, 3:9)]), 
      diag_panel = pairs_barplot(
        gp_vartext = gpar(fontsize = 10, fontface = 2),
        gp_leveltext = gpar(fontsize = 8),
        abbreviate = 1, var_offset = 1.25),
      newpage = FALSE)
    
    dev.off()
    

    pairs.table with outer viewport