How to adjust the size of y axis labels only in R?

后端 未结 3 1419
一个人的身影
一个人的身影 2021-02-03 18:19

How can I adjust only the size of Y-axis labels in R?

I know that cex.axis alters the size of the axis labels but it only affects the x-axis. Why, and how can I adjust t

相关标签:
3条回答
  • 2021-02-03 18:33

    ucfagls is right, providing you use the plot() command. If not, please give us more detail.

    In any case, you can control every axis seperately by using the axis() command and the xaxt/yaxt options in plot(). Using the data of ucfagls, this becomes :

    plot(Y ~ X, data=foo,yaxt="n")
    axis(2,cex.axis=2)
    

    the option yaxt="n" is necessary to avoid that the plot command plots the y-axis without changing. For the x-axis, this works exactly the same :

    plot(Y ~ X, data=foo,xaxt="n")
    axis(1,cex.axis=2)
    

    See also the help files ?par and ?axis


    Edit : as it is for a barplot, look at the options cex.axis and cex.names :

    tN <- table(sample(letters[1:5],100,replace=T,p=c(0.2,0.1,0.3,0.2,0.2)))
    
    op <- par(mfrow=c(1,2))
    barplot(tN, col=rainbow(5),cex.axis=0.5) # for the Y-axis
    barplot(tN, col=rainbow(5),cex.names=0.5) # for the X-axis
    par(op)
    

    alt text

    0 讨论(0)
  • 2021-02-03 18:50

    As the title suggests that we want to adjust the size of the labels and not the tick marks I figured that I actually might add something to the question, you need to use the mtext() if you want to specify one of the label sizes, or you can just use par(cex.lab=2) as a simple alternative. Here's a more advanced mtext() example:

    set.seed(123)
    foo <- data.frame(X = rnorm(10), Y = rnorm(10))
    plot(Y ~ X, data=foo,
         yaxt="n", ylab="", 
         xlab="Regular boring x", 
         pch=16,
         col="darkblue")
    axis(2,cex.axis=1.2)
    mtext("Awesome Y variable", side=2, line=2.2, cex=2)
    

    enter image description here

    You may need to adjust the line= option to get the optimal positioning of the text but apart from that it's really easy to use.

    0 讨论(0)
  • 2021-02-03 18:53

    Don't know what you are doing (helpful to show what you tried that didn't work), but your claim that cex.axis only affects the x-axis is not true:

    set.seed(123)
    foo <- data.frame(X = rnorm(10), Y = rnorm(10))
    plot(Y ~ X, data = foo, cex.axis = 3)
    

    at least for me with:

    > sessionInfo()
    R version 2.11.1 Patched (2010-08-17 r52767)
    Platform: x86_64-unknown-linux-gnu (64-bit)
    
    locale:
     [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
     [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
     [5] LC_MONETARY=C              LC_MESSAGES=en_GB.UTF-8   
     [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
     [9] LC_ADDRESS=C               LC_TELEPHONE=C            
    [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
    
    attached base packages:
    [1] grid      stats     graphics  grDevices utils     datasets  methods  
    [8] base     
    
    other attached packages:
    [1] ggplot2_0.8.8 proto_0.3-8   reshape_0.8.3 plyr_1.2.1   
    
    loaded via a namespace (and not attached):
    [1] digest_0.4.2 tools_2.11.1
    

    Also, cex.axis affects the labelling of tick marks. cex.lab is used to control what R call the axis labels.

    plot(Y ~ X, data = foo, cex.lab = 3)
    

    but even that works for both the x- and y-axis.


    Following up Jens' comment about using barplot(). Check out the cex.names argument to barplot(), which allows you to control the bar labels:

    dat <- rpois(10, 3) names(dat) <- LETTERS[1:10] barplot(dat, cex.names = 3, cex.axis = 2)

    As you mention that cex.axis was only affecting the x-axis I presume you had horiz = TRUE in your barplot() call as well? As the bar labels are not drawn with an axis() call, applying Joris' (otherwise very useful) answer with individual axis() calls won't help in this situation with you using barplot()

    HTH

    0 讨论(0)
提交回复
热议问题