如何只在R中调整y轴标签的尺寸?

时间:2023-02-05 14:57:49

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

如何只调整R中的y轴标签的大小?

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?

我知道cex。axis改变了轴标签的大小,但它只影响x轴。为什么,我如何调整y轴?

3 个解决方案

#1


53  

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

ucfagls是正确的,提供您使用plot()命令。如果没有,请给我们更多的细节。

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 :

在任何情况下,您都可以使用axis()命令和plot()中的xaxt/yaxt选项来分别控制每个轴。利用ucfagls的数据,这成为:

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 :

选项yaxt="n"是必要的,以避免plot命令在不更改的情况下绘制y轴。对于x轴,它的工作原理完全相同:

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

See also the help files ?par and ?axis

还可以看到帮助文件?par和?axis


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

编辑:因为这是一个酒吧,看看选项cex。轴和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)

如何只在R中调整y轴标签的尺寸?

#2


13  

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:

正如书名所暗示的,我们要调整大小的标签而不是刻度线我想我可能会添加一些问题,您需要使用多行文字()如果您想要指定一个标签的尺寸,或者你可以用par(cex.lab = 2)作为一个简单的选择。这里有一个更高级的mtext()示例:

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)

如何只在R中调整y轴标签的尺寸?

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.

您可能需要调整line=选项来获得文本的最佳位置,但除此之外,它非常容易使用。

#3


7  

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:

不知道你在做什么(这有助于展示你尝试过的那些没用的东西),但是你声称cex。轴只影响x轴不成立:

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.

同时,cex。轴影响标记的标记。cex。lab用于控制所谓的axis标签。

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

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

但即使是x轴和y轴也是一样的。


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

追踪Jens关于使用barplot()的评论。查看barplot()的cex.names参数,它允许您控制bar标签:

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

dat <- rpois(10,3) names(dat) <- LETTERS[1:10] barplot(dat, cex.names = 3, cex)。轴= 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()

正如你提到的cex。axis只影响了x轴,我想你在barplot()调用中也使用了horiz = TRUE ?由于条形标签不是用axis()调用绘制的,因此使用单个axis()调用应用Joris'(否则非常有用)答案在使用barplot()的情况下不会有帮助。

HTH

HTH

#1


53  

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

ucfagls是正确的,提供您使用plot()命令。如果没有,请给我们更多的细节。

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 :

在任何情况下,您都可以使用axis()命令和plot()中的xaxt/yaxt选项来分别控制每个轴。利用ucfagls的数据,这成为:

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 :

选项yaxt="n"是必要的,以避免plot命令在不更改的情况下绘制y轴。对于x轴,它的工作原理完全相同:

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

See also the help files ?par and ?axis

还可以看到帮助文件?par和?axis


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

编辑:因为这是一个酒吧,看看选项cex。轴和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)

如何只在R中调整y轴标签的尺寸?

#2


13  

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:

正如书名所暗示的,我们要调整大小的标签而不是刻度线我想我可能会添加一些问题,您需要使用多行文字()如果您想要指定一个标签的尺寸,或者你可以用par(cex.lab = 2)作为一个简单的选择。这里有一个更高级的mtext()示例:

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)

如何只在R中调整y轴标签的尺寸?

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.

您可能需要调整line=选项来获得文本的最佳位置,但除此之外,它非常容易使用。

#3


7  

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:

不知道你在做什么(这有助于展示你尝试过的那些没用的东西),但是你声称cex。轴只影响x轴不成立:

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.

同时,cex。轴影响标记的标记。cex。lab用于控制所谓的axis标签。

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

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

但即使是x轴和y轴也是一样的。


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

追踪Jens关于使用barplot()的评论。查看barplot()的cex.names参数,它允许您控制bar标签:

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

dat <- rpois(10,3) names(dat) <- LETTERS[1:10] barplot(dat, cex.names = 3, cex)。轴= 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()

正如你提到的cex。axis只影响了x轴,我想你在barplot()调用中也使用了horiz = TRUE ?由于条形标签不是用axis()调用绘制的,因此使用单个axis()调用应用Joris'(否则非常有用)答案在使用barplot()的情况下不会有帮助。

HTH

HTH