Search code examples
rplotfont-sizefont-style

Font size and font styles in R plots (with R base plotting)


I am struggling to find a way to change font style and font size of figures in R. I want all text in the figure in Arial 12. I frequently use cex command for increasing and decreasing font but for now I want 12 font size specifically. I am not using any specialized plotting package and just using R base plotting functions, like plot().

I tried installing and using the package extrafont, following this post: Changing Fonts for Graphs in R but it is giving me the following error. I don’t understand why it is saying “could not find function "theme". I did install and load the package and followed instructions like given in the post.

> plot(hfit) +
+ theme(text=element_text(family="Times New Roman", face="bold", 
size=12))
Error in theme(text = element_text(family = "Times New Roman", face 
= "bold",  : 
could not find function "theme"

From this post: specifying font size in R figures I thought I may use par(ps=12) for font size 12, but I couldn’t figure out how to exactly use this function. I tried this “plot(hfit, par(ps=12))” but I think this code is wrong. If someone can please tell me any simple way to get all fonts of figure in Arial 12.

Thank you


Solution

  • csv vs ps

    I wonder if OP is directly interested in cex. From ?par:

    cex A numerical value giving the amount by which plotting text and symbols should be magnified relative to the default. This starts as 1 when a device is opened, and is reset when the layout is changed, e.g. by setting mfrow.

    It sounds like they would like to set the font size in points which sounds more like ps. From ?par:

    ps integer; the point size of text (but not symbols).

    Of course, OP does then become interested in cex when needing to reset the magnification of the plot title or any of the other plot elements. On my machine it seems a cex of 1 is 12 points, and therefore, cex = 1.5 will be 18 points.

    font family

    OP would also like to set the font family to Arial which may not be the same syntax depending on OS and settings.

    On Windows one can check default font mapping with windowsFonts():

    Windows

    windowsFonts()
    #> $serif
    #> [1] "TT Times New Roman"
    #> 
    #> $sans
    #> [1] "TT Arial"
    #> 
    #> $mono
    #> [1] "TT Courier New"
    

    Created on 2024-05-06 with reprex v2.1.0

    Which on my Windows machine suggests Arial is already mapped to sans.

    Macintosh/Linux

    On Macintosh/Linux one might use X11Fonts() for this purpose:

    X11Fonts()
    #> $serif
    #> [1] "-*-times-%s-%s-*-*-%d-*-*-*-*-*-*-*"
    #> 
    #> $sans
    #> [1] "-*-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"
    #> 
    #> $mono
    #> [1] "-*-courier-%s-%s-*-*-%d-*-*-*-*-*-*-*"
    #> 
    #> $Times
    #> [1] "-adobe-times-%s-%s-*-*-%d-*-*-*-*-*-*-*"
    #> 
    #> $Helvetica
    #> [1] "-adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"
    #> 
    #> $CyrTimes
    #> [1] "-cronyx-times-%s-%s-*-*-%d-*-*-*-*-*-*-*"
    #> 
    #> $CyrHelvetica
    #> [1] "-cronyx-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"
    #> 
    #> $Arial
    #> [1] "-monotype-arial-%s-%s-*-*-%d-*-*-*-*-*-*-*"
    #> 
    #> $Mincho
    #> [1] "-*-mincho-%s-%s-*-*-%d-*-*-*-*-*-*-*"
    

    Created on 2024-05-06 with reprex v2.1.0

    Which suggests Arial is mapped to Arial.

    Of course, font mapping can get more complicated if the desired font is not already mapped. Luckily Arial seems to be a pretty standard font.

    Example

    I was thinking OP may be more interested in seeing something like this.

    Note, it seems the defaults for cex.axis and cex.lab might already be set at 1 given all the text seems to be the same size. I also rendered this to a word document to check font size, and at least when output to word, the plot font is 12 pt as desired.

    par(ps = 12)
    
    plot(
      pressure, 
      main = "pressure by temperature", 
      family = "sans", # as per my window machine font mappings
      cex.main = 1, # re-set title magnification from 1.5? default
      font.main = 1 # re-set title format to plain from bold default
    )
    

    Created on 2024-05-06 with reprex v2.1.0