Automatic adjustment of margins in horizontal bar chart Automatic adjustment of margins in horizontal bar chart r r

Automatic adjustment of margins in horizontal bar chart


I think your first idea is probably the most appropriate. Something like this seems to work okay and requires not much stuffing about.

ylabels <-  c(  "1oooooooooooo",            "2",            "3",            "4")test <- matrix(c(55,65,30, 40,70,55,75,6,49,45,34,20),                   nrow =3 ,                ncol=4,                byrow=TRUE,               dimnames = list(c("Subgroup 1", "Subgroup 2", "Subgroup 3"),                               ylabels))# adjust to the maximum of either the default # or a figure based on the maximum lengthpar(mar=c(5.1, max(4.1,max(nchar(ylabels))/1.8) ,4.1 ,2.1))barplot(test,        las=2,       beside = TRUE,       legend=T,       horiz=T)

After inspecting dotchart, a more generalisable solution may also be to use:

linch <-  max(strwidth(ylabels, "inch")+0.4, na.rm = TRUE)par(mai=c(1.02,linch,0.82,0.42))