1
votes

The ggplot() function and anything built on top of it ignores the global point size. Functions like plot() and text(), however, do not. The former functions expect font sizes to be specified in absolute terms, through a size parameter, while the latter work with cex, which does relative scaling.

It's not always possible to avoid mixing those mechanisms. Here's an example: You want to plot a series of polygons and place labels inside them, typically for a map. Especially for highly nonconvex polygons, you might want to use rgeos::polygonsLabel() (rather than, say, coordinates()) to determine appropriate label positions. This function is built on top of text() and thus, again, only allows you to pass relative font sizes. But maybe you later want to place the labels with geom_text() from the ggplot2 package; for optimal utility of the rgeos::polygonsLabel() output, font sizes need to match here.

1

1 Answers

1
votes

I've found the following example to work as expected and would like to share it since it took me a while to get there. Please correct me if I'm doing something that I shouldn't be, e.g. with the point-to-mm conversion. I'll create a PNG image file for this compatibility with this site but, e.g., SVG and PDF work just as well.

pointSize <- 20 # or whatever you want

# Setting point size here affects the native plotting methods
# like text()
png('myfigure.png', pointsize=pointSize) # apparent default: 12

library(ggplot2)
plot.new()

pointToMM = function(x) 0.352778*x

# plot a few 'o's
p <- ggplot(mtcars, aes(wt, mpg, label = 'o')) +
  geom_text(size = pointToMM(pointSize)) # apparent default: pointToMM(11)

# label the axes in the same
p <- p + labs(x = 'xo xo xo xo xo xo', y = 'xo xo xo xo xo xo') +
  theme_bw(pointSize) # apparent default: 12

print(p)

# Add 'xo' in two places. Notice how the sizes match up.
# The x and y coordinates were chosen ad-hoc for this example
text(0.35,0.13, 'xo')
text(0.5, 0.0, 'xo')

dev.off()

sample plot