56

A quick question that I can't find answer on the web (or Wickham's book):

What is the unit of the size argument in ggplot2? For example, geom_text(size = 10) -- 10 in what units?

The same question applies to default unit in ggsave(height = 10, width = 10).

Henrik
  • 65,555
  • 14
  • 143
  • 159
Heisenberg
  • 8,386
  • 12
  • 53
  • 102
  • Until now, I believed the text size was in points (as in font sizes). Is this still true, albeit a simplified explanation? In `pdf()`, `postscript()` etc., the `height` and `width` are given in inches, so would it be wrong to assume that `ggsave` also uses inches? – MrGumble Jun 26 '13 at 07:31
  • Unfortunately, there is not a “unit of the size argument in `ggplot2`” because there is more than one `size` argument. The `size` argument to `geom_text()` does something very different from the `size` argument to `element_text()`, and I have no idea how to reconcile the two or why they would be different. – randy May 12 '21 at 03:25

3 Answers3

59

The answer is : The unit is the points. It is the unit of fontsize in the grid package. In ?unit, we find the following definition

"points" Points. There are 72.27 points per inch.

(but note the closely related "bigpts" Big Points. 72 bp = 1 in.)

Internally ggplot2 will multiply the font size by a magic number ggplot2:::.pt, defined as 1/0.352777778.

Here a demonstration, I create a letter using grid and ggplot2 with same size:

library(grid)
library(ggplot2)
ggplot(data=data.frame(x=1,y=1,label=c('A'))) +
  geom_text(aes(x,y,label=label),size=100)
## I divide by the magic number to get the same size.
grid.text('A',gp=gpar(fontsize=100/0.352777778,col='red'))

enter image description here

Addendum Thanks to @baptiste

The "magic number"(defined in aaa-constants.r as .pt <- 1 / 0.352777778) is really just the conversion factor between "points" and "mm", that is 1/72 * 25.4 = 0.352777778. Unfortunately, grid makes the subtle distinction between "pts" and "bigpts", which explains why convertUnit(unit(1, "pt"), "mm", valueOnly=TRUE) gives the slightly different value of 0.3514598.

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Nice answer. It is likely my ignorance of why this was done, but isn't this a bit hackish? – thelatemail Jun 26 '13 at 06:06
  • So what is a good practice while specifying the size? Too often I find myself wondering which size is appropriate, which is the original motivation of my question. Thanks again for the nice demo! – Heisenberg Jun 26 '13 at 06:11
  • 1
    @thelatemail thanks. hackish do you talk about the division of the magic number? it looks like a dirty implementation but I don't think so it is a compromise.Note that :1-) There isn't a lot of constants in ggplot2, 2-)when you deal with text you are obliged to do something like this , (see also strheight() and strWidth() in basic plot)... But I think we will get better explanations from ggplot2 proficients... – agstudy Jun 26 '13 at 06:17
  • @Anh Personally I would try to test.Maybe You can do a dichotomous to choose faster... – agstudy Jun 26 '13 at 06:19
  • Hmm I was hoping that there is something more than trial and error because this consumes quite a bit of my time as I wrap up my plots. Thanks anyhow! – Heisenberg Jun 26 '13 at 06:25
  • 2
    The magic number used throughout ggplot2 is `fontsize = size * ggplot2:::.pt`, which is defined in `aaa-constants.r` as `.pt <- 1 / 0.352777778` – baptiste Jun 26 '13 at 12:16
  • 2
    and for the sake of clarity, one may note that `1/72 * 25.4 = 0.352777778`, that is point -> inch -> mm. Grid converts points to mm slightly differently, presumably because the definition of points is sometimes argued to be device-specific. – baptiste Jun 26 '13 at 12:35
  • @baptiste feel free to add this to the answer. I can't formulate it better. – agstudy Jun 26 '13 at 12:41
  • 1
    I've edited, but it's a bit messy. In fact, one could argue that ggplot2 is using a slightly incorrect factor. – baptiste Jun 26 '13 at 12:56
  • isn't the unit in ggplot2 aes() mm? see user1609452's answer below. the actual font size in points in your answer should be 100/0.35277=283.465 – olala Jul 21 '14 at 18:38
  • 12
    A rewrite of this answer would be appreciated. The unit of size in ggplot2 is mm, not pt, and I've just redefined .pt as 72 / 25.4 to hopefully make that more clear in the code – hadley Aug 06 '15 at 11:51
  • 8
    And I've now changed it to 72.27 / 25.4 since as pointed out in the comments, grid uses printers' pointers, not Adobe/big points. – hadley Aug 06 '15 at 11:56
  • 1
    Is this 72 defined by dpi? If I want to save the plot as a png with different resolutions, will I get different results? Or, do I need to adjust all the size = ***? It seems to be the case. – Feng Jiang May 20 '17 at 13:05
  • 1
    Follow-up question: Where can I vent my frustration due to ggplot2 and LaTeX not using the same definition for "pt"? 10pt in ggplot2 and 10pt in LaTeX side-by-side is noticeable. – user1202136 Oct 17 '17 at 14:32
  • “The unit is the points. It is the unit of fontsize in the grid package.” These two sentences are contradictory. Are points the units for `size` in a `geom`, or are points the unit for `fontsize` in `grid`? If points are the units for `size` in a `geom` and if `ggplot2` “will multiply the font size by a magic number” before passing it to `grid`, then points are NOT the unit of `fontsize` in `grid`. – randy May 12 '21 at 03:11
8

The 'ggplot2' package, like 'lattice' before it, is built on the grid package. You can get the available units at:

?grid::unit
?grid::convertX
?grid::convertY

grid::convertX(grid::unit(72.27, "points"), "inches")

(I use the formalism pkg::func because in most cases grid is loaded a a NAMESPACE but not attached when either lattice or `ggplot2 are loaded.)

I earlier posted a comment that I later deleted saying that size was in points. I did so after seeing that the size of the text with size=10 was roughly 10 mm. The "magic" number mentioned by agstudy is in fact within 1% of:

as.numeric(grid::convertX(grid::unit(1, "points"), "mm"))
#[1] 0.3514598
0.352777778/.Last.value
#[1] 1.00375
jbaums
  • 27,115
  • 5
  • 79
  • 119
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I had thought units of size were in mm http://groups.google.com/group/ggplot2-dev/tree/browse_frm/thread/37ba1aaaf8e22b38/a2c142da58418570?hide_quotes=no&pli=1 I wonder why the magic number is not 0.3514598. – user1609452 Jun 26 '13 at 07:40
  • Perhaps it's that size ratio on hadley's device. I'm working on a six year-old Mac. – IRTFM Jun 26 '13 at 07:48
  • I'd repeat my question for the poster above: so given this information, do you recommend any good practice regarding specifying size in points? I often find myself wasting time with trial and error. – Heisenberg Jun 26 '13 at 07:57
  • I guess if you want to specify points multiply the points you want by the magic number. – user1609452 Jun 26 '13 at 08:01
4

From ?aes_linetype_size_shape

# Size examples
# Should be specified with a numerical value (in millimetres),
# or from a variable source

height and width in ggsave relate to par("din") from ?par

din

  R.O.; the device dimensions, (width, height), in inches. See also dev.size,
  which is updated immediately when an on-screen device windows is re-sized.

So I guess size in aes is in millimetres and ggsave height and width are in inches.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
user1609452
  • 4,406
  • 1
  • 15
  • 20