[R] font size relative to graphic

Mike Miller mbmiller+l at gmail.com
Fri Apr 24 22:10:55 CEST 2009


On Fri, 24 Apr 2009, David Winsemius wrote:

> On Apr 24, 2009, at 3:14 PM, Mike Miller wrote:
>
>> I am printing numbers onto horizontal bars in a bar plots, but I am 
>> finding it difficult to make the number always the right size.  If 
>> there are more bars, the bars get narrower and the font is too big.  I 
>> also find that if I change the size of the graph, the bars will get 
>> wider or narrower and the font stays about the same size.  The result 
>> depends on whether I view the graph in X11 or in postscript.
>> 
>> Is there a way to specify font size relative to bar width, or at least 
>> specify font size relative to graph coordinates?
>> 
>> Here's a bit of what I've been doing:
>> 
>> x.bar.plot <- barplot(as.matrix(x), horiz=TRUE, beside=TRUE, xlim=c(0,100), xlab="Score", las=1)
>> text(as.matrix(x), x.bar.plot-.07, as.character(as.matrix(round(x))), pos=2, col="black")
>> 
>> The "-.07" is something I added to make the numbers align well with the 
>> bars under one condition, but it doesn't always help.
>> 
>> Any tips would be greatly appreciated.  Thanks in advance.
>
> The values returned from the barplot function are the locations of the 
> bars. Why not call barplot, collect the values, and then use the length 
> of the result to determine your sizes and widths?

That is exactly the kind of thing I want to do, but I don't know how the 
font size can be specified so that it fits.  My bars are horizontal and 
yours (from code below) are vertical.  Your barplot output was called "mp" 
and mine was called "x.bar.plot" and here is what they looked like:

> mp
      [,1] [,2] [,3] [,4] [,5]
[1,]  1.5  6.5 11.5 16.5 21.5
[2,]  2.5  7.5 12.5 17.5 22.5
[3,]  3.5  8.5 13.5 18.5 23.5
[4,]  4.5  9.5 14.5 19.5 24.5

> x.bar.plot
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]  1.5  4.5  7.5 10.5 13.5 16.5 19.5 22.5 25.5  28.5  31.5  34.5
[2,]  2.5  5.5  8.5 11.5 14.5 17.5 20.5 23.5 26.5  29.5  32.5  35.5

They are pretty similar numbers.  It seems that [1,1] element is always 
1.5 and consecutive elements in the same column differ by 1.0, but in the 
rows we see something different: mine are 3.0 apart and yours are 5.0 
apart.  So what does this tell me?  I think fonts are specified in points 
which correspond to 1/72 inches, while bars are specified in coordinates 
that are independent of inches.  So I need to know how coordinates 
correspond to inches before I can get this fixed.

Mike

> hh <- t(VADeaths)[, 5:1]
> mybarcol <- "gray20"
> mp <- barplot(hh, beside = TRUE,
>       col = c("lightblue", "mistyrose",
>               "lightcyan", "lavender"),
>       legend = colnames(VADeaths), ylim= c(0,100),
>       main = "Death Rates in Virginia", font.main = 4,
>       sub = "Faked upper 2*sigma error bars", col.sub = mybarcol,
>       cex.names = 1.5)
>
> length(mp)  #  [1] 20




More information about the R-help mailing list