我在网上(或Wickham的书中)找不到答案,有一个小问题:
ggplot2
中size
参数的单位是什么?例如:geom_text(size = 10)
中的10
使用何种单位?
同样的问题也适用于ggsave(height = 10, width = 10)
中的默认单位。
grid
软件包中字体大小的单位。在?unit
中,我们找到了以下定义。"points" Points. There are 72.27 points per inch.
(但请注意密切相关的“bigpts” Big Points. 72 bp = 1 in。
)
ggplot2
在内部将字体大小乘以一个神奇的数字ggplot2:::.pt
,定义为1/0.352777778。
这里是一个演示,我使用grid和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'))
补充 感谢 @baptiste
"魔数"(在 aaa-constants.r 中定义为 .pt <- 1 / 0.352777778) 实际上只是“点”和“毫米”之间的转换因子,即 1/72 * 25.4 = 0.352777778
。不幸的是,grid
对 "pts" 和 "bigpts" 进行微妙的区分,这就解释了为什么 convertUnit(unit(1, "pt"), "mm", valueOnly=TRUE)
得到略微不同的值,即 0.3514598
。
fontsize = size * ggplot2:::.pt
,它在aaa-constants.r
中定义为.pt <- 1 / 0.352777778
。 - baptiste1/72 * 25.4 = 0.352777778
,即点 -> 英寸 -> 毫米。网格将点转换为毫米略有不同,可能是因为点的定义有时被认为是设备特定的。 - baptiste与其前身'Lattice'一样,'ggplot2'包建立在'grid'包的基础之上。您可以在以下网址获取可用单元:
?grid::unit
?grid::convertX
?grid::convertY
grid::convertX(grid::unit(72.27, "points"), "inches")
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
中的par("din")
相关。
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
是以毫米为单位的,而中的height
和width
则是以英寸为单位的。
pdf()
,postscript()
等中,height
和width
以英寸为单位给出,因此假设ggsave
也使用英寸会有错吗? - MrGumbleggplot2
中并没有“单位尺寸参数”,因为有不止一个size
参数。geom_text()
函数的size
参数与element_text()
函数的size
参数完全不同,我不知道如何调和这两者之间的差异或者为什么它们会不同。 - randy