我在网上找不到答案的快速问题(或威克姆的书):
ggplot2
中大小参数的单位是什么?例如,geom_text(size = 10)
-10
在什么单位?
同样的问题也适用于
ggsave(height = 10, width = 10)
的默认单元。
EDIT3/13/2025:GEOM_TEXT的GGPLOT中的文本大小在mm中指定,而不是点。要将单元转换为点,您可以使用字体大小/.pt的快捷方式,例如10 pt字体为
size = 10/.pt
。请注意,在主题参数(使用element_text)中,大小默认为point。
答案是:单位是要点。它是grid
软件包中的字体大小单元。在
?unit
中,我们找到以下定义
"points" Points. There are 72.27 points per inch.
(但请注意密切相关的“ bigpts”Big Points. 72 bp = 1 in.
)内部
ggplot2
将字体大小乘以魔术数
ggplot2:::.pt
,定义为1/0.35277778.。 在此演示中,我使用网格和ggplot2创建一个字母,大小相同:
library(grid)
library(ggplot2)
ggplot(data=data.frame(x=1,y=1,label=c('A'))) +
geom_text(aes(x,y,label=label),size=100)
## I divide by the magic number to get the same size.
grid.text('A',gp=gpar(fontsize=100/0.352777778,col='red'))
addendum感谢 @baptiste
“魔术数”(在aaa-constants.r中定义为.pt)实际上只是“点”和“ mm”之间的转换因子,即1/72 * 25.4 = 0.352777778
<- 1 / 0.352777778。不幸的是,
grid
对“ pts”和“ bigpts”进行了微妙的区别,这解释了为什么convertUnit(unit(1, "pt"), "mm", valueOnly=TRUE)
给出了slightly的值略有不同。
'ggplot2'软件包,例如“晶格”,是在
0.3514598
软件包上构建的。您可以在以下位置获得可用单元:grid
(我使用形式主义
?grid::unit
?grid::convertX
?grid::convertY
grid::convertX(grid::unit(72.27, "points"), "inches")
,因为在大多数情况下,加载了一个名称空间,但在加载pkg::func
或`ggplot2时未连接。)
i早些时候发表了一条评论,我后来删除了说
grid
在点。我这样做后,看到文本的大小约为10毫米。 Agstudy提到的“魔术”数字实际上在以下1%之内
lattice
从size
size=10
as.numeric(grid::convertX(grid::unit(1, "points"), "mm"))
#[1] 0.3514598
0.352777778/.Last.value
#[1] 1.00375
?aes_linetype_size_shape
# Size examples
# Should be specified with a numerical value (in millimetres),
# or from a variable source
和ggsave中与
height
width
我想
par("din")
在毫米中是毫米,
?par
din
R.O.; the device dimensions, (width, height), in inches. See also dev.size,
which is updated immediately when an on-screen device windows is re-sized.
size
aes
ggsave
height