Examples for 'graphics::strwidth'


Plotting Dimensions of Character Strings and Math Expressions

Aliases: strwidth strheight

Keywords: dplot character

### ** Examples

str.ex <- c("W","w","I",".","WwI.")
op <- par(pty = "s"); plot(1:100, 1:100, type = "n")
plot of chunk example-graphics-strwidth-1
sw <- strwidth(str.ex); sw
[1] 3.453488 2.805959 1.079215 1.079215 8.417878
all.equal(sum(sw[1:4]), sw[5])
[1] TRUE
#- since the last string contains the others

sw.i <- strwidth(str.ex, "inches"); 25.4 * sw.i  # width in [mm]
[1]  4.233333  3.439583  1.322917  1.322917 10.318750
unique(sw / sw.i)
[1] 20.72093 20.72093
# constant factor: 1 value
mean(sw.i / strwidth(str.ex, "fig")) / par('fin')[1]  # = 1: are the same
[1] 1
## See how letters fall in classes
##  -- depending on graphics device and font!
all.lett <- c(letters, LETTERS)
shL <- strheight(all.lett, units = "inches") * 72  # 'big points'
table(shL)  # all have same heights ...
shL
 9 
52 
mean(shL)/par("cin")[2] # around 0.6
[1] 45
(swL <- strwidth(all.lett, units = "inches") * 72)  # 'big points'
 [1]  7.50  7.50  6.75  7.50  7.50  4.50  7.50  7.50  3.00  3.00  6.75  3.00
[13] 12.00  7.50  7.50  7.50  7.50  5.25  6.00  4.50  7.50  6.75  9.75  6.75
[25]  6.75  6.00  8.25  8.25  8.25  9.00  7.50  6.75  9.00  9.00  3.75  3.75
[37]  8.25  6.75 10.50  9.00  9.75  7.50  9.75  8.25  7.50  7.50  9.00  8.25
[49] 12.00  8.25  7.50  8.25
split(all.lett, factor(round(swL, 2)))
$`3`
[1] "i" "j" "l"

$`3.75`
[1] "I" "J"

$`4.5`
[1] "f" "t"

$`5.25`
[1] "r"

$`6`
[1] "s" "z"

$`6.75`
[1] "c" "k" "v" "x" "y" "F" "L"

$`7.5`
 [1] "a" "b" "d" "e" "g" "h" "n" "o" "p" "q" "u" "E" "P" "S" "T" "Y"

$`8.25`
[1] "A" "B" "C" "K" "R" "V" "X" "Z"

$`9`
[1] "D" "G" "H" "N" "U"

$`9.75`
[1] "w" "O" "Q"

$`10.5`
[1] "M"

$`12`
[1] "m" "W"
sumex <- expression(sum(x[i], i=1,n), e^{i * pi} == -1)
strwidth(sumex)
[1]  5.899709 14.269622
strheight(sumex)
[1] 8.461047 3.572202
par(op)  #- reset to previous setting

[Package graphics version 4.2.3 Index]