1

In ggplot2, font size is based on a constant for converting points, inches, and mm: https://github.com/tidyverse/ggplot2/blob/main/R/geom-.r line 193, as discussed here. I apparently need to reference the ggplot2:::.pt constant in a package, where it is frowned upon to include internal functions from other package. How should I do this to minimize headaches for myself, users, and ggplot2 auhtors/maintainers? For example, I could copy the ggplot2 source code in my package (seems allowed by MIT license), ask the ggplot2 authors to export the graphical_units objects, or I could paraphrase... Thanks for your suggestions!

Michael Roswell
  • 1,300
  • 12
  • 31
  • I created a file called utils-graphical_units.R in my package and copied the ggplot2 source for the time being. – Michael Roswell Jan 03 '22 at 20:04
  • 3
    `.pt` is exported, it is not internal, so just use `ggplot2::.pt` – caldwellst Jan 03 '22 at 20:27
  • D'oh! You'd think I would have noticed when I copied the functions! Thanks! – Michael Roswell Jan 03 '22 at 20:35
  • 1
    The constants won't change, so couldn't you just hard code the ratio? 72.27 comes from the definition of one TeX point, which is 1/72.27 of an inch, and 25.4 is mm : inch conversion. So defining `.pt <- 72.27 / 25.4` will not require any ongoing maintenance... https://tex.stackexchange.com/a/200968 – Jon Spring Jan 03 '22 at 20:38

1 Answers1

2

.pt (and .stroke) are already exported graphical units from ggplot2 so can be imported into your package using the standard ggplot2::.pt or @importFrom ggplot2 .pt.

caldwellst
  • 5,719
  • 6
  • 22
  • I definitely prefer using the exported constant to hard-coding, since it releases me from fully documenting this bizarre-looking number or ratio. I'm trying to troubleshoot how ggplot is interacting with device sizes, so keeping all the `ggplot2` code together is helpful for me for now, and possibly for a downstream user. – Michael Roswell Jan 03 '22 at 20:54
  • 1
    The `.pt` constant is just a conversion constant from points to millimeters. You could use `grid::convertUnit(grid::unit(1, "mm"), "pt", valueOnly = TRUE)` to reconstruct it. – teunbrand Jan 03 '22 at 23:22