如何在R中增加字体大小?

时间:2021-11-22 23:15:24

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)
hist(x, xlim=range(x), xlab= "Variable Label", 
     ylab="density", main="Title of plot", prob=TRUE, ps=30)

The ps argument does not change font size (but it says in R Help for ?par that it is for "the point size of text (but not symbols)".

ps参数不改变字体大小(但它在R Help中表示,它是“文本的点大小(而不是符号)”。

Also is it possible to separate changing the font size from the plotting function such as hist?

是否可以将更改字体大小与图像功能(如hist)分开?

6 个解决方案

#1


104  

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, ...

你需要像cex=1.5这样的参数来缩放字体150%但是,确实看到了帮助(par),因为也有cex。实验室,cex。轴,…

#2


96  

Thus, to summarise the existing discussion, adding

因此,总结现有的讨论,并补充。

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

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.

在你的图中,1.5可以是2 3等等,而1的值是默认值会增加字体大小。

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)

如何在R中增加字体大小?

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)

如何在R中增加字体大小?

#3


20  

Notice that "cex" does change things when the plot is made with text. For example, the plot of an agglomerative hierarchical clustering:

请注意,“cex”在使用文本的时候确实会改变一些事情。例如,凝聚层次聚类的情节:

library(cluster)
data(votes.repub)
agn1 <- agnes(votes.repub, metric = "manhattan", stand = TRUE)
plot(agn1, which.plots=2)

will produce a plot with normal sized text:

将产生一个与正常大小的文本:

如何在R中增加字体大小?

and plot(agn1, which.plots=2, cex=0.5) will produce this one:

和情节(agn1。图=2,cex=0.5)将产生这个:

如何在R中增加字体大小?

#4


16  

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在hist()中不起作用。使用cex。轴上的数字轴,cex。实验室的标签。
  3. cex doesn't work in axis() either. Use cex.axis for the numbers on the axes.
  4. cex也不能在axis()中工作。使用cex。轴上的数字轴。
  5. 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.
  6. 在使用hist()设置标签时,可以使用mtext()设置它们。您可以使用cex设置字体大小,但是使用1的值实际上将字体设置为默认的1.5倍!!!您需要使用cex=2/3来获得默认字体大小。至少,在使用PDF输出的Mac OS X的R 3.0.2中是这样的情况。
  7. You can change the default font size for PDF output using pointsize in pdf().
  8. 您可以使用PDF()中的pointsize更改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.

我认为,期望R (a)实际上按照它的文档说明应该做的事情(b)按照预期的方式行事是非常合理的。

#5


1  

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

如果您想在设置标签=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)

#6


1  

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:

在plot命令。记住把:

par(cex.axis=1.0)

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

在绘图之后,确保字体回到默认大小。

#1


104  

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, ...

你需要像cex=1.5这样的参数来缩放字体150%但是,确实看到了帮助(par),因为也有cex。实验室,cex。轴,…

#2


96  

Thus, to summarise the existing discussion, adding

因此,总结现有的讨论,并补充。

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

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.

在你的图中,1.5可以是2 3等等,而1的值是默认值会增加字体大小。

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)

如何在R中增加字体大小?

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)

如何在R中增加字体大小?

#3


20  

Notice that "cex" does change things when the plot is made with text. For example, the plot of an agglomerative hierarchical clustering:

请注意,“cex”在使用文本的时候确实会改变一些事情。例如,凝聚层次聚类的情节:

library(cluster)
data(votes.repub)
agn1 <- agnes(votes.repub, metric = "manhattan", stand = TRUE)
plot(agn1, which.plots=2)

will produce a plot with normal sized text:

将产生一个与正常大小的文本:

如何在R中增加字体大小?

and plot(agn1, which.plots=2, cex=0.5) will produce this one:

和情节(agn1。图=2,cex=0.5)将产生这个:

如何在R中增加字体大小?

#4


16  

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在hist()中不起作用。使用cex。轴上的数字轴,cex。实验室的标签。
  3. cex doesn't work in axis() either. Use cex.axis for the numbers on the axes.
  4. cex也不能在axis()中工作。使用cex。轴上的数字轴。
  5. 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.
  6. 在使用hist()设置标签时,可以使用mtext()设置它们。您可以使用cex设置字体大小,但是使用1的值实际上将字体设置为默认的1.5倍!!!您需要使用cex=2/3来获得默认字体大小。至少,在使用PDF输出的Mac OS X的R 3.0.2中是这样的情况。
  7. You can change the default font size for PDF output using pointsize in pdf().
  8. 您可以使用PDF()中的pointsize更改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.

我认为,期望R (a)实际上按照它的文档说明应该做的事情(b)按照预期的方式行事是非常合理的。

#5


1  

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

如果您想在设置标签=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)

#6


1  

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:

在plot命令。记住把:

par(cex.axis=1.0)

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

在绘图之后,确保字体回到默认大小。