strwidth              package:graphics              R Documentation

_P_l_o_t_t_i_n_g _D_i_m_e_n_s_i_o_n_s _o_f _C_h_a_r_a_c_t_e_r _S_t_r_i_n_g_s _a_n_d _M_a_t_h _E_x_p_r_e_s_s_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     These functions compute the width or height, respectively, of the
     given strings or mathematical expressions 's[i]' on the current
     plotting device in _user_ coordinates, _inches_ or as fraction of
     the figure width 'par("fin")'.

_U_s_a_g_e:

     strwidth(s, units = "user", cex = NULL, font = NULL, vfont = NULL, ...)
     strheight(s, units = "user", cex = NULL, font = NULL, vfont = NULL, ...)

_A_r_g_u_m_e_n_t_s:

       s: a character or expression vector whose dimensions are to be
          determined.  Other objects are coerced by 'as.graphicsAnnot'.

   units: character indicating in which units 's' is measured; should
          be one of '"user"', '"inches"', '"figure"'; partial matching
          is performed.

     cex: numeric *c*haracter *ex*pansion factor; multiplied by
          'par("cex")' yields the final character size; the default
          'NULL' is equivalent to '1'.

font, vfont, ...: additional information about the font, possibly
          including the graphics parameter '"family"': see 'text'.

_D_e_t_a_i_l_s:

     Where an element of 's' is a multi-line string (that is, contains
     newlines '\n'), the width and height are of an enclosing rectangle
     of the string as plotted by 'text'.  The inter-line spacing is
     controlled by 'cex', 'par("lheight")' and the 'point size' (but
     not the actual font in use).

     Measurements in '"user"' units (the default) are only available
     after 'plot.new' has been called - otherwise an error is thrown.

_V_a_l_u_e:

     Numeric vector with the same length as 's', giving the width or
     height for each 's[i]'.  'NA' strings are given width and height 0
     (as they are not plotted).

_S_e_e _A_l_s_o:

     'text', 'nchar'

_E_x_a_m_p_l_e_s:

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

     sw.i <- strwidth(str.ex, "inches"); 25.4 * sw.i # width in [mm]
     unique(sw / sw.i)
     # constant factor: 1 value
     mean(sw.i / strwidth(str.ex, "fig")) / par('fin')[1] # = 1: are the same

     ## 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 ...
     mean(shL)/par("cin")[2] # around 0.6

     (swL <- strwidth(all.lett, units="inches") * 72) # 'big points'
     split(all.lett, factor(round(swL, 2)))

     sumex <- expression(sum(x[i], i=1,n), e^{i * pi} == -1)
     strwidth(sumex)
     strheight(sumex)

     par(op)#- reset to previous setting

