Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

plot

r

labels

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 the y axis?

like image 350
Jens Avatar asked Sep 23 '10 11:09

Jens


People also ask

How do I change the size of axis labels in R?

Go to the menu in RStudio and click on Tools and then Global Options. Select the Appearance tab on the left. Again buried in the middle of things is the font size. Change this to 14 or 16 to start with and see what it looks like.

How do I increase the size of Y axis in R?

To increase the length of Y-axis for ggplot2 graph in R, we can use scale_y_continuous function with limits argument.

How do you change the Y axis labels in R?

The easiest way to change the Y-axis title in base R plot is by using the ylab argument where we can simply type in the title. But the use of ylab does not help us to make changes in the axis title hence it is better to use mtext function, using which we can change the font size, position etc.

How do I increase axis label size?

To increase the X-axis labels font size using ggplot2, we can use axis. text. x argument of theme function where we can define the text size for axis element.


2 Answers

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

like image 71
Joris Meys Avatar answered Sep 20 '22 10:09

Joris Meys


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.

like image 45
Max Gordon Avatar answered Sep 21 '22 10:09

Max Gordon