Exact dimensions of linetype spacing and size
Asked Answered
U

1

4

This is mostly a follow-up question on a previous one.

Given that in ggplot2 and grid there are different linetypes and spacings vary between line sizes, what is their relationship?

There are two things I do not quite understand.

  1. How is the line size defined? If I were to draw a straight vertical line and substitute it by a rectangle, what should be the width of the rectangle to get the equivalent of the line's size? Especially, how does the lwd = 1 or lwd = 10 I pass to par()/gpar() relate to absolute dimensions (pixels, mm, inches, points)?

The gpar() documentation refers to the par() documentation which states the following:

The line width, a positive number, defaulting to 1. The interpretation is device-specific, and some devices do not implement line widths less than one.

Which is fair enough but I couldn't really find the necessary device specific documentation for common devices.

  1. I think I might assume that the spacings of different linetypes are proportional to their size, but how exactly are the 'dotdash', 'dashed', 'dotted' etc. proportions of dash-length to spacing-length defined?

In the plot below, how can I predict or calculate the dash/spacing lengths in advance?

library(ggplot2)

df <- data.frame(
  x = rep(c(0, 1), 4),
  y = rep(1:4, each = 2),
  size = rep(c(2, 10), each = 4),
  linetype = rep(c(2,2,3,3), 2)
)

# The `I()` function automatically assigns identity scales
ggplot(df, aes(x, y, size = I(size), linetype = I(linetype))) +
  geom_line(aes(group = y))

I think this is mostly a documentation question, so I'd be happy if you could point me to the correct pages. Otherwise, an answer to my two questions above or a demonstration thereof would also be nice.

EDIT: ggplot has a variable called .pt which they use often to multiply a line size with. That probably means that in grid the linesize is something / .pt, but in what units?

Uniformize answered 25/7, 2020 at 12:58 Comment(2)
I think this is what you want, which includes "The lengths of on/off stretches of line. This is done with a string containing 2, 4, 6, or 8 hexadecimal digits which give the lengths of consecutive lengths. For example, the string "33" specifies three units on followed by three off and "3313" specifies three units on followed by three off followed by one on and finally three off" and "The size of a line is its width in mm".Wyatan
Thank you for this, I was aware of this alternative notation, but I missed this important bit from the documentation: 'The five standard dash-dot line types described above correspond to 44, 13, 1343, 73, and 2262.' Now I would just need to figure out what 'units' mean exactly.Uniformize
T
3

Another great question Teunbrand. I have a partial answer here which seems to give valid results but feels a bit imprecise.

The obvious way to get conversion between lwd and length units is to measure them programatically. For example, to check the lwd of the X11 device, you can do this:

library(grid)

x11()
grid.newpage()

# draw a thick black line that goes right across the page
grid.draw(linesGrob(x = unit(c(-0.1, 1.1), "npc"), 
                    y = unit(c(0.5, 0.5), "npc"),
                    gp = gpar(lwd = 10)))

# Capture as a bitmap
bmp_line    <- dev.capture()

# Work out the thickness of the line in pixels as proportion of page height
lwd_10_prop <- sum(bmp_line != "white")/length(bmp_line)

# Now draw a black rectGrob of known height with lwd of 0 and transparent for completeness
grid.newpage()
grid.draw(rectGrob(width  = unit(1.1, "npc"),
                   height = unit(10, "mm"),
                   gp     = gpar(lwd = 0, col = "#00000000", fill = "black")))

# Capture as a bitmap and measure the width as proportion of device pixels
bmp_rect    <- dev.capture()
mm_10_prop  <- sum(bmp_rect != "white")/length(bmp_rect)

# Get the ratio of lwd to mm
lwd_as_mm <- lwd_10_prop / mm_10_prop
dev.off()

lwd_as_mm
#> [1] 0.2702296

Which tells us that an lwd of 1 is 0.2702296 mm on this device

We can test this by plotting a red rectangle of our calculated width over a green line near the top of our page, then plotting the same green line over the same red rectangle near the bottom of the page. If and only if they are exactly the same width will we have a completely green line and a completely red line on our page:

grid.newpage()

grid.draw(linesGrob(x = unit(c(-0.1, 1.1), "npc"), 
                    y = unit(c(0.75, 0.75), "npc"),
                    gp = gpar(lwd = 5, col = "green")))

grid.draw(rectGrob(y = unit(0.75, "npc"),
                   width  = unit(1.1, "npc"),
                   height = unit(5 * lwd_as_mm, "mm"),
                   gp     = gpar(lwd = 0,  col = "#00000000", fill = "red")))

grid.draw(rectGrob(y = unit(0.25, "npc"),
                   width  = unit(1.1, "npc"),
                   height = unit(5 * lwd_as_mm, "mm"),
                   gp     = gpar(lwd = 0,  col = "#00000000", fill = "red")))

grid.draw(linesGrob(x = unit(c(-0.1, 1.1), "npc"), 
                    y = unit(c(0.25, 0.25), "npc"),
                    gp = gpar(lwd = 5, col = "green")))

enter image description here

Of course, we can improve precision by increasing the thickness of our lines when measuring how wide they are in pixels.

Although the result is supposed to be device-independent, it's worth noting that in the above example I took the results from the X11 device but plotted them in the rstudio device, so the equivalence seems to hold for both devices.

Tessietessier answered 25/7, 2020 at 15:5 Comment(1)
Hi Allan, great answer as usual. I get very similar numbers on my machine so it seems a great empirical approach. I've tried all kinds of convertUnit(unit(0.2702296, "mm"), "pt") and vary the latter unit to guess what the original input must have been, but so far it doesn't make a lot of sense to me. It works for me though despite not knowing the exact details :)Uniformize

© 2022 - 2024 — McMap. All rights reserved.