1

I recently switched a workstation from windows to linux, and for interactive use, the font sizes as chosen by geom_text are dramatically different. I'm trying to find a way to set the base size for geom_text so that the use of geom_text(aes(size=..)) can be used interchangeably between windows and linux with similar-enough results.

Notably, without changing the actual code that produces the plot.

Code:

R.version[c("system","version.string")]
#                _                                
# system         x86_64, mingw32                  
# version.string R version 4.2.3 (2023-03-15 ucrt)

plot(mpg ~ disp, mtcars, type="n")
with(mtcars, text(disp, mpg, cyl))

library(ggplot2)
packageVersion("ggplot2")
# [1] ‘3.4.2’
ggplot(mtcars, aes(disp, mpg, label = cyl)) + geom_text()

windows base graphics

windows ggplot2

The font size for base text and ggplot2 geom_text are approximately the same.

Same code on Ubuntu 22.04:

R.version[c("system","version.string")]
#                _                           
# system         x86_64, linux-gnu           
# version.string R version 4.2.3 (2023-03-15)

plot(mpg ~ disp, mtcars, type="n")
with(mtcars, text(disp, mpg, cyl))

library(ggplot2)
packageVersion("ggplot2")
# [1] ‘3.4.2’
ggplot(mtcars, aes(disp, mpg, label = cyl)) + geom_text()

linux base graphics

linux ggplot2

The text sizes are markedly different.

Since there is much legacy code that needs to run similarly (I recognize some differences will sneak through), I would like to be able to set something (perhaps in .Rprofile) that will render similar font sizes for both geom_text and axis labels and titles, etc.

  • Something like theme_set(theme_gray(base_size=20)) does not affect geom_text sizes.
  • This is not a symptom of "high DPI monitor", this has been tested on both high and low DPI displays (on the same linux system).
  • Running extrafont::loadfonts() (with fully-imported fonts) before loading ggplot2 does not fix it.

Is it possible to set geom_text size via theme? is very very similar: to be clear, if there is a way to set it to a known/common size basis, regardless of the ability to set it to arbitrary values, I'm good with that.

r2evans
  • 141,215
  • 6
  • 77
  • 149

1 Answers1

1

Great question. The easiest solution I can think of is a bit hacky, but effective, requiring only a single line of code to be added to a script after ggplot2 is loaded. It is to overwrite ggplot::.pt, which is the multiplier used to convert the size parameter in ggplot layers to font sizes.

library(ggplot2)

.pt
#> [1] 2.845276

ggplot(mtcars, aes(disp, mpg, label = cyl)) +
  geom_text()

If we overwrite .pt like this:

assignInNamespace(".pt", 5, ns = "ggplot2")

Then we can see the font size increases appropriately.

ggplot(mtcars, aes(disp, mpg, label = cyl)) +
  geom_text()

Notice that the theme elements are left alone since these do not use .pt.

You will need to tinker with the value to get the appropriate multiplier that makes your two systems match.

Created on 2023-05-10 with reprex v2.0.2

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87