tags:

views:

125

answers:

2

Hi, 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 affects ONLY the x-axis...why? and how can I adjust the y axis ?

any ideas? Thanks!!

+1  A: 

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

Gavin Simpson
+1  A: 

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

Joris Meys
Hi there,sorry for beeing unprecise (again). Actually I am running a barplot with numerical X-axis but wiht strings for y axis labels. I guess that the cex.axis in barplot cannot handle text since in the help for par it says "cex.axis = expansion factor for numeric axis labels." I guess I have to use your approach with the extra axis command to modify my y-axis. Many thanks
Jens
@Jens; If using `barplot()` separate `axis()` calls won't work because the bar labels are not an "axis". To use `axis()` you'd need to ave the bar mid points (these are returned by `barplot()`). Using a `text()` call with clipping turned off might also be a way to do a custom "axis" for a barplot.
Gavin Simpson
@ucfagls On my system, that does work actually for the Y axis. For the X axis it gives a wrong result. I adapted my answer for the barplot.
Joris Meys
@Jens Would you be as kind as to edit your question and specify this is for a barplot? thx
Joris Meys
@Joris: indeed, but whether the y-axis is an "axis" or a labelled set of bars depends on the `horiz` argument of the `barplot()`. Jens's problem was that `cex.axis` didn't work for one of the axes (in his case the x-axis so we presume he was using default `horiz = FALSE`). My comment was, not that `axis()` is wrong, just that the control over the x-axis (in Jens' case), or the bar labels, can not be managed with a separate call to `axis()` [well, easily]. Ultimately this boils down to not reading `?barplot` and not providing us here with a reproducible example showing the problem.
Gavin Simpson