## Synthetic figure for the auto-margin feature.
##
## Before the gtable Stage-1 wrap, the "very_long_pid_label_XX" labels on
## the right (drawn by WColorBarV with label.use.data=TRUE) clip at the
## figure edge unless the user passes WCustomize(mar.right=...). After,
## the same figure with a bare WCustomize() renders cleanly.
##
## Reproduce:
##   Rscript-4.6.0 -e 'devtools::load_all("~/repo/wheatmap"); source("~/repo/wheatmap/inst/examples/auto_margin.R")'
## Output: ~/gallery/wheatmap_auto_margin.pdf

library(wheatmap)

set.seed(1)
m = matrix(rnorm(200), nrow = 20)
rownames(m) = sprintf("very_long_row_label_%02d", 1:20)
grp = rep(c("groupA","groupB","groupC","groupD"), each = 5)

## Edge-clipping cases auto-margin must protect against:
##   * east  -- long row names extending right of the main heatmap
##   * north -- WColorBarH in-situ labels: placement='north' on TopOf()
##              defaults xticklabel.side='t' so labels point UP (away
##              from the heatmap), not down into it.
g = WHeatmap(m, name = "main",
             yticklabels = TRUE, yticklabel.side = 'r',
             yticklabel.fontsize = 7) +
    WColorBarH(grp, TopOf("main"), name = "grp",
               label.use.data = TRUE, label.fontsize = 8) +
    WCustomize()

out = path.expand("~/gallery/wheatmap_auto_margin.pdf")
dir.create(dirname(out), showWarnings = FALSE, recursive = TRUE)
pdf(out, width = 5, height = 5)
print(g)
dev.off()
message("wrote ", out)
