3

I'm trying to understand how ggplot/grid determines the point size to render. This answer (with comment from Hadley) describes the role of .pt magic number constant, but I can't see how the numbers add up. This demonstrates:

# empty ggplot obj with no margins outside panel
p0 = ggplot() + scale_size_identity() +
  scale_y_continuous(expand = c(0,0)) + scale_x_continuous(expand = c(0,0)) +
  theme(axis.text = element_blank(), panel.grid = element_blank(),
        panel.border = element_rect(colour='black', fill='transparent'),
        panel.spacing = unit(0, 'mm'), axis.ticks.length = unit(0, "mm"),
        plot.margin=unit(c(0,0,0,0), "mm")) + labs(x=NULL, y=NULL)

# 2 data points plus 2 corner points to define bbox ('limits' args seem to force extra margins)
d = data.frame(x = 1:4, y = c(100,150,250,300), sz = c(0,76.15,76.15,0))

p = p0 + geom_point(data=d, aes(x, y, size=sz), fill='red', stroke=0, shape=22, alpha=.8)

# output to pdf and open
fn='test.pdf'; ggsave(p, filename = fn, w=6, h=4, units = "in"); browseURL(fn)

enter image description here

I got to the size 76.15 by trial and error. This size gets the two square points just touching, but I don't understand why. The plot is 6 inches wide = 152.4mm. To meet, the points must be 2 inches wide = 50.8mm. But I can't see how size 76.15 maps to 50.8mm via the .pt multiplier (2.845276).

Any suggestions much appreciated. I should add that when using shape=15 instead of 22 the point size for the same result is 67.15, but I'm not sure that gets us any closer to the answer.

geotheory
  • 22,624
  • 29
  • 119
  • 196

1 Answers1

3

You might find this old thread useful: http://r.789695.n4.nabble.com/Fwd-R-size-of-point-symbols-td923507.html

Essentially: the only available reference to figure out the size of point shapes is the source code (src/main/engine.c)

case  22: /* squares */
    xc = toDeviceWidth(RADIUS * SQRC * GSTR_0, GE_INCHES, dd);
    yc = toDeviceHeight(RADIUS * SQRC * GSTR_0, GE_INCHES, dd);
    GERect(x-xc, y-yc, x+xc, y+yc, gc, dd);
    break;

Shapes 0,1,4,7,8,10,12,13,14,15,16,18,19,21 seem to be constrained by a square scaled down by a factor 0.75,

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Meow Professor Ripley! Well I likewise struggle to make much sense of the above, other than RADIUS is 0.375 and SQRC is sqrt(pi/4). `toDeviceWidth` line (`result = (result / dd->dev->ipr[0]) / fabs(dd->dev->right - dd->dev->left);`) is beyond me. Did your ggplot2 point idea come to anything? – geotheory Jun 24 '17 at 18:45
  • IIRC I did make a geom_ngon with regular polygons (in what was called the ggExtra package, now retired), but it wasn't super useful. – baptiste Jun 24 '17 at 20:40
  • Cheers dude. I guess for my current purposes I'll assume a point-to-inches multiplier of 38.075, and see how that goes. – geotheory Jun 24 '17 at 21:18