Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: understanding point size

Tags:

r

ggplot2

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.

like image 559
geotheory Avatar asked Feb 28 '26 19:02

geotheory


1 Answers

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

like image 154
baptiste Avatar answered Mar 03 '26 09:03

baptiste



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!