R:所绘制字符的确切大小和位置

时间:2018-07-19 03:47:14

标签: r plot text

我正在尝试使用text(..., cex = ...)来写一个字符逐渐变小的单词。但是,由于每个字符占用的空间比前一个字符少,因此我在努力使每个字符的位置和对齐正确。使用srt旋转文本时,定位更加困难。

也许有一种方法可以问R所放置字符的确切位置和大小,以便我知道下一个字符的放置位置?谢谢!

例如,不使用srt

x.range <- c(0, 1)
y.range <- c(0, 1)
plot(NA, xlim = x.range, ylim = y.range, asp = 1)

w <- "incrementally smaller text"
n <- nchar(w)
shrink <- 0.025 # How much the text shrinks from one character to the next

for(i in 1:n){
  current <- substr(w, i, i)
  o <- shrink * diff(x.range) # some offset from the previous char
  text(x = x.range[1] + o * (i - 1), y = y.range[1], current,
       cex = (1 - shrink) ^ i)
}

会产生以下结果(我在图像编辑器中为文本加上下划线以突出显示该问题):

ugly alignment

或对角文字:

x.range <- c(0, 1)
y.range <- c(0, 1)
plot(NA, xlim = x.range, ylim = y.range, asp = 1)

w <- "incrementally smaller text"
n <- nchar(w)
shrink <- 0.025 # How much the text shrinks from one character to the next

for(i in 1:n){
  current <- substr(w, i, i)
  x.o <- shrink * diff(x.range) # some horizontal offset from the previous char
  y.o <- shrink * diff(y.range) # some vertical offset from the previous char
  text(x = x.range[1] + x.o * (i - 1), y = y.range[2] - y.o * (i - 1)/3, current,
       cex = (1 - shrink) ^ i, srt = -15)
}

哪个会产生:

enter image description here

有人可以向我解释这里发生了什么吗?对齐似乎遍布整个地方。

0 个答案:

没有答案