How to increase font size in a plot in R?

前端 未结 7 1360
小鲜肉
小鲜肉 2020-11-28 01:21

I am confused. What is the right way to increase font size of text in the title, labels and other places of a plot?

For example

x <- rnorm(100)
h         


        
相关标签:
7条回答
  • 2020-11-28 01:55

    You want something like the cex=1.5 argument to scale fonts 150 percent. But do see help(par) as there are also cex.lab, cex.axis, ...

    0 讨论(0)
  • 2020-11-28 01:55

    Thus, to summarise the existing discussion, adding

    cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5

    to your plot, where 1.5 could be 2, 3, etc. and a value of 1 is the default will increase the font size.

    x <- rnorm(100)
    

    cex doesn't change things

    hist(x, xlim=range(x),
         xlab= "Variable Lable", ylab="density", main="Title of plot", prob=TRUE)
    
    hist(x, xlim=range(x),
         xlab= "Variable Lable", ylab="density", main="Title of plot", prob=TRUE, 
         cex=1.5)
    

    enter image description here

    Add cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5

    hist(x, xlim=range(x),
         xlab= "Variable Lable", ylab="density", main="Title of plot", prob=TRUE, 
         cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5)
    

    enter image description here

    0 讨论(0)
  • 2020-11-28 01:56

    For completeness, scaling text by 150% with cex = 1.5, here is a full solution:

    cex <- 1.5
    par(cex.lab=cex, cex.axis=cex, cex.main=cex)
    plot(...)
    par(cex.lab=1, cex.axis=1, cex.main=1)
    

    I recommend wrapping things like this to reduce boilerplate, e.g.:

    plot_cex <- function(x, y, cex=1.5, ...) {
      par(cex.lab=cex, cex.axis=cex, cex.main=cex)
      plot(x, y, ...)
      par(cex.lab=1, cex.axis=1, cex.main=1)
      invisible(0)
    }
    

    which you can then use like this:

    plot_cex(x=1:5, y=rnorm(5), cex=1.3)
    

    The ... are known as ellipses in R and are used to pass additional parameters on to functions. Hence, they are commonly used for plotting. So, the following works as expected:

    plot_cex(x=1:5, y=rnorm(5), cex=1.5, ylim=c(-0.5,0.5))
    
    0 讨论(0)
  • 2020-11-28 01:58

    By trial and error, I've determined the following is required to set font size:

    1. cex doesn't work in hist(). Use cex.axis for the numbers on the axes, cex.lab for the labels.
    2. cex doesn't work in axis() either. Use cex.axis for the numbers on the axes.
    3. In place of setting labels using hist(), you can set them using mtext(). You can set the font size using cex, but using a value of 1 actually sets the font to 1.5 times the default!!! You need to use cex=2/3 to get the default font size. At the very least, this is the case under R 3.0.2 for Mac OS X, using PDF output.
    4. You can change the default font size for PDF output using pointsize in pdf().

    I suppose it would be far too logical to expect R to (a) actually do what its documentation says it should do, (b) behave in an expected fashion.

    0 讨论(0)
  • 2020-11-28 01:58

    I came across this when I wanted to make the axis labels smaller, but leave everything else the same size. The command that worked for me, was to put:

    par(cex.axis=0.5)
    

    Before the plot command. Just remember to put:

    par(cex.axis=1.0)
    

    After the plot to make sure that the fonts go back to the default size.

    0 讨论(0)
  • 2020-11-28 02:06

    In case you want to increase the font of the labels of the histogram when setting labels=TRUE

    bp=hist(values, labels = FALSE, 
     main='Histogram',
     xlab='xlab',ylab='ylab',  cex.main=2, cex.lab=2,cex.axis=2)
    
    text(x=bp$mids, y=bp$counts, labels=bp$counts ,cex=2,pos=3)
    
    0 讨论(0)
提交回复
热议问题