线型间距和尺寸的确切尺寸

时间:2020-07-25 12:58:38

标签: r ggplot2 graphics r-grid

这主要是关于previous one的后续问题。

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

有两件事我不太了解。

  1. 如何定义行大小?如果我要画一条垂直直线并用矩形代替,那么矩形的宽度应等于线的大小?特别是,我传递给lwd = 1 / lwd = 10的{​​{1}}或par()与绝对尺寸(像素,毫米,英寸,点)有什么关系?

gpar()文档是指gpar()文档,其中指出:

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

这很公平,但是我真的找不到常见设备所需的设备特定文档。

  1. 我想我可能会假定不同线型的间距与它们的大小成正比,但是精确定义虚线长度与间距长度的“点划线”,“虚线”,“点划线”等比例是多少? / li>

在下面的图中,如何提前预测或计算破折号/间距的长度?

par()

我认为这主要是一个文档问题,因此,如果您能将我指向正确的页面,我将非常高兴。否则,回答以上两个问题或进行演示也将很不错。

编辑:ggplot有一个名为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)) 的变量,他们经常使用该变量与行大小相乘。这可能意味着在网格中,线的大小为.pt,但以什么单位呢?

1 个答案:

答案 0 :(得分:3)

另一个有趣的问题Teunbrand。我在这里有一个部分答案,似乎可以给出有效的结果,但是有点不准确。

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

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毫米

我们可以通过在页面顶部附近的绿线上绘制我们计算宽度的红色矩形,然后在页面底部附近的同一红色矩形上绘制相同的绿线来进行测试。当且仅当它们的宽度完全相同时,我们的页面上才会有一条绿线和一条红线:

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

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

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