R:打印字符的精确大小和位置
我试图使用R:打印字符的精确大小和位置,r,plot,text,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 <
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)
}
x.range我不知道你想做什么。你能添加可复制代码和匹配图吗adj
允许您指定text
内相对于水平和垂直参考点的位置调整。默认情况下,文本是水平居中的。我添加了一个最小的工作示例和一个我试图完成的图形。我的问题是如何正确计算偏移量,这样对齐就不会上下移动,字符之间的距离也就合适了。
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)
}