首页 > 解决方案 > R Graph - 图形字符串测量

问题描述

我想以图形方式测量字符串的宽度。(参见附图中的示例。我使用的是 Times New Roman,点大小为 11 和大小为 16)

每个字符将具有不同的宽度,例如相同字体和字体大小的“a”与“A”。我正在尝试寻找以图形方式绘制文本并以编程方式测量图形长度(以“mm”或“inch”表示)的解决方案,我尝试了 strwidth() 函数,但它无法正常工作。例如,“a”与“A”的值相同

R - 使用 strwidth() 测量字符串的宽度

在此处输入图像描述

标签: rggplot2

解决方案


看起来您需要par()ps参数一起使用。

 strwidth("This is a test.", units = "inches", family = "Arial", ps = par(ps = 11))
[1] 0.875

> strwidth("This is a test.", units = "inches", family = "Arial", ps = par(ps = 16))
[1] 1.333333

> strwidth("This is a test.", units = "inches", family = "Times New Roman", ps = par(ps = 16))
[1] 1.145833

> strwidth("This is a test.", units = "inches", family = "Times New Roman", ps = par(ps = 11))
[1] 0.8333333

> strwidth("This is a test.", units = "inches", family = "Times New Roman", ps = par(ps = 24))
[1] 1.75

谷歌文档中的快速测量,它看起来很正确。


推荐阅读