ggplot2 - 大小的单位 [英] ggplot2 - The unit of size

查看:610
本文介绍了ggplot2 - 大小的单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在网上找到答案的一个简单问题(或Wickham的书):

ggplot2中size参数的单位是多少?例如,geom_text(size = 10) - 10以什么单位表示?



编辑:同样的问题适用于ggsave中的默认单位(height = 10,width = 10 )

解决方案

答案是:单位是分数。它是网格包中字体大小的单位。在?unit 中,我们找到以下定义

 pointsPoints 。每英寸有72.27点。 

(但请注意密切相关的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)
##我除以幻数得到相同的大小。
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


A quick question that I can't find answer on the web (or Wickham's book):

What is the unit of the size argument in ggplot2? For example, geom_text(size=10) -- 10 in what units?

EDIT: The same question applies to default unit in ggsave(height=10, width=10)

解决方案

The answer is : The unit is the points. It is the unit of fontsize in the grid package. In ?unit, we find the following definition

"points" Points. There are 72.27 points per inch.

(but note the closely related "bigpts" Big Points. 72 bp = 1 in.)

Internally ggplot2 will multiply the font size by a magic number ggplot2:::.pt, defined as 1/0.352777778.

Here a demonstration, I create a letter using grid and ggplot2 with same size:

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 Thanks to @baptiste

The "magic number"(defined in aaa-constants.r as .pt <- 1 / 0.352777778) is really just the conversion factor between "points" and "mm", that is 1/72 * 25.4 = 0.352777778. Unfortunately, grid makes the subtle distinction between "pts" and "bigpts", which explains why convertUnit(unit(1, "pt"), "mm", valueOnly=TRUE) gives the slightly different value of 0.3514598.

这篇关于ggplot2 - 大小的单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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