如何在ggplot2中定义线宽(大小)?

Mik*_*kko 5 r ggplot2

线宽(size)美观ggplot2似乎打印出大约2.13磅的宽线到pdf(实验是在带有Mac的Adobe Illustrator中完成的):

library(ggplot2)

dt <- data.frame(id = rep(letters[1:5], each = 3), x = rep(seq(1:3), 5), y = rep(seq(1:5), each = 3), s = rep(c(0.05, 0.1, 0.5, 1, 72.27/96*0.5), each = 3))

lns <- split(dt, dt$id)

ggplot() + geom_line(data = lns[[1]], aes(x = x, y = y), size = unique(lns[[1]]$s)) + 
  geom_text(data = lns[[1]], y = unique(lns[[1]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[1]]$s))) + 
  geom_line(data = lns[[2]], aes(x = x, y = y), size = unique(lns[[2]]$s)) + 
  geom_text(data = lns[[2]], y = unique(lns[[2]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[2]]$s))) + 
  geom_line(data = lns[[3]], aes(x = x, y = y), size = unique(lns[[3]]$s)) + 
  geom_text(data = lns[[3]], y = unique(lns[[3]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[3]]$s))) + 
  geom_line(data = lns[[4]], aes(x = x, y = y), size = unique(lns[[4]]$s)) + 
  geom_text(data = lns[[4]], y = unique(lns[[4]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[4]]$s))) + 
  geom_line(data = lns[[5]], aes(x = x, y = y), size = unique(lns[[5]]$s)) + 
  geom_text(data = lns[[5]], y = unique(lns[[5]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[5]]$s))) + 
  xlim(1,4) + theme_void()

ggsave("linetest.pdf", width = 8, height = 2)

# Device size does not affect line width:
ggsave("linetest2.pdf", width = 10, height = 6)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我读到应该将线宽乘以72.27/96以获得pt中的线宽,但是当我试图获得0.5 pt时,上面的实验给出了0.8 pt的线宽.

正如@Pascal指出的那样,线宽似乎不符合pt到mm的转换,它适用于字体,并且是由@hadley在其中一条注释中定义的.即线宽似乎不是由"幻数"1/0.352777778定义的.

ggplot2的线宽背后的公式是什么?

Cla*_*lke 8

你的帖子里已经有了所有的部分。首先,ggplot2 将size设置乘以ggplot2::.pt,定义为 72.27/25.4 = 2.845276(geom- .r 中的第 165 行):

> ggplot2::.pt
[1] 2.845276
Run Code Online (Sandbox Code Playgroud)

然后,正如您所说,您需要将结果值乘以 72.27/96 才能从 R 像素转换为点。因此转换系数为:

> ggplot2::.pt*72.27/96
[1] 2.141959
Run Code Online (Sandbox Code Playgroud)

如您所见,ggplot2 size = 1 对应于大约 2.14pt,类似地,0.8 pt 对应于 ggplot2 大小单位中的 0.8/2.141959 = 0.3734899。