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

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

问题描述

一个我在网上(或 Wickham 的书)上找不到答案的快速问题:

ggplot2 中大小参数的单位是什么?例如,geom_text(size = 10) -- 10 什么单位?

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

解决方案

答案是:单位就是点数.它是 grid 包中字体大小的单位.在?unit中,我们找到如下定义

"points" 点数.每英寸有 72.27 点.

(但请注意密切相关的bigpts"Big Points.72 bp = 1 in.)

在内部ggplot2会将字体大小乘以一个幻数ggplot2:::.pt,定义为1/0.352777778.>

这是一个演示,我使用 grid 和 ggplot2 创建一个相同大小的字母:

库(网格)图书馆(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?

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天全站免登陆