线型间距和尺寸的确切尺寸 [英] Exact dimensions of linetype spacing and size

查看:161
本文介绍了线型间距和尺寸的确切尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这主要是关于>上一个问题的后续问题一个.

鉴于ggplot2和grid中存在不同的线型,并且线大小之间的间距不同,它们之间的关系是什么?

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

有两件事我不太了解.

  1. 如何定义行大小?如果我要画一条垂直直线并用矩形代替,那么矩形的宽度应该是多少才能等于该线的大小?特别是,传递给par()/gpar()lwd = 1lwd = 10与绝对尺寸(像素,毫米,英寸,点)有什么关系?
  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)?

gpar()文档是指par()文档,其中指出以下内容:

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

线宽,一个正数,默认为1.解释是特定于设备的,某些设备的线宽不小于一.

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. 我想我可能会假设不同线型的间距与它们的大小成正比,但是精确定义虚线长度与间距长度的点划线",虚线",点划线"等比例是多少? /li>
  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.

ggplot有一个名为.pt的变量,他们经常使用该变量与行大小相乘.这可能意味着在网格中,线宽为something / .pt,但以什么单位?

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?

推荐答案

Teunbrand.我在这里有一个部分答案,似乎可以给出有效的结果,但感觉有点不准确.

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

lwd和长度单位之间进行转换的一种明显方法是以编程方式对其进行测量.例如,要检查X11设备的lwd,您可以执行以下操作:

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

告诉我们,此设备上的lwd为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")))

当然,当测量线条的宽度(以像素为单位)时,我们可以通过增加线条的粗细来提高精度.

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

尽管结果应该与设备无关,但值得注意的是,在上面的示例中,我从X11设备获取了结果,然后将它们绘制在rstudio设备中,因此这两个设备的等效性似乎都成立了.

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.

这篇关于线型间距和尺寸的确切尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆