What does the `fontsize` represent in `grid` graphics?

Viewed 47

The fontsize is used to determine the size of text or points in R graphics. But what does it actually represent? For example,

grid::grid.newpage()
grid::grid.points(default.units = "npc", 
                  gp = gpar(fontsize = 100))

enter image description here

Here, the point fontsize is 100. So, I have two questions:

  1. What does the value mean, the radius, the diameter or the area of a point? Can I find any reference?
  2. What is the unit of 100? In specific, does it mean 100 pointsize (1 fontsize = 1pt)? Otherwise, is it determined by device-specification? If so, 1 fontsize = ?pt.

Also, I have another question that, in language R, is there any handy functions to convert pt to pixel (px)? For example, we can convert a pt unit to a mm unit as in

convertUnit(unit(1, "pt"), "mm", valueOnly = TRUE)

So, is there any function to query the dimension of the screen and then do the transformation from pt to px?

Thanks!

1 Answers

The size of the symbols drawn by grid.points() depend on several factors, primarily pch, which determines the shape of the symbol, and size. By default, the latter is unit(1, "char"), which means the size further depends on the current font size (fontsize and cex).

Deep in the graphics engine source code (https://github.com/r-devel/r-svn/blob/master/src/main/engine.c#L2110) are some constants that further modify the nominal symbol size based on the symbol shape. For example, pch=1 multiplies size by .375 to get the circle radius.

Yes, fontsize=100 means 100pt.

The following code demonstrates the calculations by drawing a symbol and a rectangle with the same width:

library(grid)
grid.newpage()
pushViewport(viewport())
grid.points(.5, .5)
grid.rect(width=2*.375*unit(1, "char"))
grid.points(.5, .5, gp=gpar(fontsize=100))
grid.rect(width=2*.375*unit(100, "pt"))
Related