Skip to content
Prev 178963 / 398506 Next

font size relative to graphic

In my particular situation, the trick was to figure out what worked for 
one set of bars, then determine how bar widths changed when various 
graphing parameters changed.  Then I used that information to decide on a 
"cex" (character expansion) multiplier that worked perfectly for every 
example I produced (this was called "bar_cex" in my R code, which is shown 
below).

There are a couple of neat things about the horizontal bar plot that 
everyone can use in choosing their cex.  First, when there are n sets of m 
bars per set, then the vertical distance from the lower side of the bottom 
bar to the top side of the top bar is divided into n*(m+1)-1 equal-height 
regions and every bar width (vertical distance for horizontal bars, used 
to determine character height) is 1/(n*(m+1)-1) times that total vertical 
distance.  So just find the right cex for one set of bars, say n=3, m=2, 
so that n*(m+1)-1 = 7, then use bar_cex = cex*7/(n*(m+1)-1) for other 
values of n and m.

In my situation, using pdf with letter paper, height=11, width=8.5, I also 
used a variable number of inches empty on the top of the page.  I called 
that number of inches "top_inches".  I then figured out the correct cex 
when the plot height was at a max of .78 and used (.78-top_inches/11) to 
figure cex for other values of top_inches.  After a little arithmetic I 
came up with the equation appended below for bar_cex.  It works for me, 
but it won't work for other people.  This probably will work for other 
situations:

bar_cex <- C/(n*(m+1)-1)

Where an appropriate value of C that works for one collection of 
horizontal bars (n sets of m bars per set) will work for other sets of 
bars.

Mike


bar_cex <- (51/(n*(m+1)-1))*(.78-top_inches/11)


# generate the horizontal bar plot and store values for later use
x.bar.plot <- barplot(as.matrix(x), col=rep(rev(bar.colors[1:m]),n), horiz=TRUE,
                       beside=TRUE, xlim=c(0,100), main=bar.main.title,
                       xlab=bar.x.label, las=1, cex.names=min( c(1, 16/L) ),
                       legend=labels.legend)
# add numbers to bars
text(as.matrix(x), x.bar.plot-.1, as.character(as.matrix(round(x))), pos=2,
      offset=0.1, col="black", cex=bar_cex)

If I don't subtract .1 from x.bar.plot, the text is positioned too high 
(vertically) on the horizontal bar.
On Fri, 24 Apr 2009, David Winsemius wrote: