1

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)

jtr13
  • 1,225
  • 11
  • 25
  • Have you tried using the ```cex``` parameter? According to the ```VCD``` documentation that should scale text/symbols relative to the default. So ```cex=0.5``` should scale it down by 50%. – Fabian B Oct 17 '21 at 02:47
  • This is obviously a bug, `gp_vartext = gpar(fontsize = 10, fontface = 2, cex.names=.5, cex.axis=.5)` should do (see [there](https://stackoverflow.com/questions/3778084/how-to-adjust-the-size-of-y-axis-labels-only-in-r)) but doesn't, you should address the author. – jay.sf Oct 17 '21 at 04:19
  • I tried putting `cex.names=.5` `cex=.5` and `cex.axis=.5` in `gp_bars()` rather than `gp_vartext()` and that didn't work either. – jtr13 Oct 17 '21 at 14:45

1 Answers1

1

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

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49