将“大"字样水平对齐.在文本中使用adj参数时,labels偏移 [英] Horizontal alignment of "large" labels is offset when using adj argument in text

查看:111
本文介绍了将“大"字样水平对齐.在文本中使用adj参数时,labels偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

text中,参数adj允许相对于xy调整labels.例如,adj值(0,1)表示左上对齐,即标签的左上角位于给定的xy坐标上.

In text, the argument adj allows adjustment of the labels with respect to x and y. For example, adj values of (0, 1) means left-top alignment, i.e. the left, top corner of the label is placed at the given x, y coordinate.

这可以在默认字符扩展cex = 1下正常工作.但是,当我想要一个更大的标签(通过增加cex创建)时,标签的位置相对于给定的坐标和adj ustment水平偏移.

This works fine with the default character expansion cex = 1. But when I want a larger label, created by increasing cex, the position of label is offset horizontally from the given coordinate and adjustment.

这是一个演示此情况的小例子:

Here is a small example which demonstrates this:

# a basic plot
plot(1:10, asp = 1, cex = 1)

# a red reference mark at 5, 5
points(x = 5, y = 5, pch = 3, cex = 3, col = 'red')

# a label of default size (cex = 1), left top adjusted
text(x = 5, y = 5, labels = 'H', cex = 1, adj = c(0, 1))

# a large label (cex = 8) with same position and adjustment as above becomes offset horizontally
text(x = 5, y = 5, labels = 'H', cex = 8, adj = c(0, 1), col = rgb(0.1, 0.9, 0.1, 0.5))

水平偏移会发生在左/右底部/顶部对齐的所有组合中

The horizontal offset occurs for all combinations of left/right bottom/top alignments:

plot(1:10, cex = 1)
points(x = 5, y = 5, pch = 3, lwd = 4, cex = 4, col = 'red')

text(x = 5, y = 5, labels = "H", cex = 1, adj = c(0, 0))
text(x = 5, y = 5, labels = "H", cex = 1, adj = c(0, 1))
text(x = 5, y = 5, labels = "H", cex = 1, adj = c(1, 0))
text(x = 5, y = 5, labels = "H", cex = 1, adj = c(1, 1))

text(x = 5, y = 5, labels = "H", cex = 8, adj = c(0, 0), col = "green")
text(x = 5, y = 5, labels = "H", cex = 8, adj = c(0, 1), col = "green")
text(x = 5, y = 5, labels = "H", cex = 8, adj = c(1, 0), col = "green")
text(x = 5, y = 5, labels = "H", cex = 8, adj = c(1, 1), col = "green")

cex> 1时如何避免标签的水平偏移?

How to avoid the horizontal offset of labels when cex > 1?

推荐答案

该问题可能很难解决.首先尝试解释原因,然后提出潜在的解决方案.

The problem may be a bit tricky to solve. First an attempt to explain why, and then a potential solution.

如R董事会成员Brian Ripley在R帮助邮件列表上所写

As written by R board member Brian Ripley on the R help mailing list here:

" R图形中的文本字符串直接以指定的字体绘制,而不是作为单个字母".

任何字体的字母(或数字,标点和形状)均以字形表示.每个字形在两侧都具有水平空间,即所谓的左右方向轴承.参见例如 此处 此处(字形指标") 此处 .

Letters (or numbers, punctuations and shapes) in any font are represented by glyphs. Each glyph has horizontal spaces on either side, the so-called left and right side bearings. See e.g. here, here ('glyph metrics'), and here.

虽然使用cex = 1时非常小,但它是导致轴承偏移的侧面轴承.当您增加绘图中字形"的大小时(使用cex),不仅字符本身的大小会增加,而且大小方位的绝对宽度也会增加.

It is the side bearings which cause the offset in your plot, albeit very small when using cex = 1. When you increase the size of the 'glyph' in your plot (using cex), not only the character itself is increasing in size, but also the absolute width of the size bearings.

里普利由此得出结论:

And Ripley thus concludes:
"so there is nothing you can do about letter spacing in R."

> 此问题与解答 显示了一种减少 个字母之间的空格的技巧.不过,卸下左侧的前轴承可能会比较棘手.

This Q&A shows a hack to reduce space between letters. To remove the leading left side bearing might be more tricky though.

可能的解决方法是使用systemfonts::shape_string抓住左轴承,然后相应地调整x位置.

A potential solution may be to use systemfonts::shape_string to grab the left bearing and then adjust x positions accordingly.

这是一些带有坐标的字符串的示例.使用原始x值(浅灰色)和x值减去方位(深灰色)来绘制字符串(带有大号cex).

Here's an example of some strings with coordinates. The strings are plotted (with a "large" cex), using the original x values (light grey), and the x values minus the bearing (dark grey).

d <- data.frame(x = 1:3, y = 1:3, labs = c("Where", "is", "Here"))

# set pointsize, cex and resolution
ps <- 12
cex <- 8
res <- 72

# calculate left bearing in pixels
left_bear_px <- shape_string(d$labs, size = ps * cex)$metrics$left_bearing

# open device
png("pp.png", width = 10, height = 5, units = "in", res = res)

# plot with "cross hair"
plot(x = d$x, y = d$y, pch = 3, cex = 3, col = "red", xlim = c(0, 5), ylim = c(0, 3))

# convert unit of bearing from pixel to xy: multiply by xy / pixel ratio
left_bear_xy <- left_bear_px * ((par("cxy") / par("cra"))[1])

# add text at original positions (light grey)
text(x = d$x, y = d$y, labels = d$labs,
     cex = cex, adj = c(0, 1), col = grey(0.6, alpha = 0.5))

# x values with left bearing removed (dark grey)
text(x = d$x - left_bear_xy, y = d$y, labels = d$labs,
     cex = cex, adj = c(0, 1), col = grey(0.1, alpha = 0.5))    

dev.off()

这篇关于将“大"字样水平对齐.在文本中使用adj参数时,labels偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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