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)
     strheight(s, units = "user", cex = NULL)

_A_r_g_u_m_e_n_t_s:

       s: character vector or 'expression's whose string widths in
          plotting units are to be determined.  An attempt is made to
          coerce other vectors to character, and other language objects
          (names and calls) to expressions.

   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'.

_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

