R,绘图,字体大小在多个绘图中的变化
我正在为出版物创建图形,希望它们具有相同的字体大小.
I am creating graphs for a publication and would like them to have the same font size.
当我创建具有多幅图的图形时,即使我没有更改tiff()
分辨率或pointsize
参数,字体大小也会减小.
我根据最终拟合的图数增加了图的大小,并确保单图和多图的边距相等.
When I create a figure with multiple plots, the font size decreases even though I haven't changed the tiff()
resolution or pointsize
parameter.
I increased the figure size according to ultimately fit the number of plots, and made sure the margins are equivalent for single and multiple plot figures.
下面是一个示例代码(字体大小在1x1和2x1图形之间是一致的,但对于3x2图形是减小的):
Following is an example code (The font size is consistent between 1x1 and 2x1 figure, but decreases for 3x2 figure):
tiff("1x1.tif", width=3,height=2.5,units="in",res=600,pointsize=8,
compression="lzw",restoreConsole=T)
par(mfrow=c(1,1),mar=c(4,4,.5,.5)+0.1)
plot(x=rnorm(10),y=rnorm(10))
dev.off()
tiff("2x1.tif", height=2.5*2,width=3,units="in",res=600,pointsize=8,
compression="lzw",restoreConsole=T)
par(mfrow=c(2,1),mar=c(2,4,2.5,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="")
par(mar=c(4,4,0.5,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10))
dev.off()
tiff("3x2.tif", height=2.5*3,width=3*2,units="in",res=600,pointsize=8,
compression="lzw",restoreConsole=T)
par(mfrow=c(3,2),mar=c(.5,4,4,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="")
par(mar=c(.5,2,4,2.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="",yaxt="n",ylab="")
par(mar=c(2.5,4,2,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="")
par(mar=c(2.5,2,2,2.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="",yaxt="n",ylab="")
par(mar=c(4.5,4,0,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10))
par(mar=c(4.5,2,0,2.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),yaxt="n",ylab="")
dev.off()
为什么会这样?
PS:我没有使用ggplot2
或lattice
,因为我在实际"图形上使用了自己的错误栏功能(我现在不记得为什么,但是我尝试使用ggplot2错误酒吧,没有得到我想要的东西).
P.S.: I'm not using ggplot2
or lattice
because I'm using my own error bar function on the "actual" figures (I can't remember why right now but I tried working with the ggplot2 error bars and didn't get what I wanted).
控制图中对象(包括文本)总体相对大小的参数称为cex
.当您使用多个面板时,默认情况下会减少它,但是可以通过将其手动设置为1
来覆盖它.
The parameter controlling the overall relative size of objects in the plot (including text) is called cex
. When you use many panels it is decreased by default, but it can be overridden by manually setting it to 1
.
par(mfrow=c(3,2), mar=c(.5,4,4,0.5)+0.1, cex=1)
主题外提示
看起来您应该使用oma
(外边距),而不是在对plot
的调用之间调用par(mar=...)
.我发现它非常有用,但似乎几乎没人知道它.另外,ann=FALSE
会关闭所有注释,las=1
会水平关闭轴刻度标签.
Off-topic-tip
It looks like you should use oma
(outer margin) rather than calling par(mar=...)
between the calls to plot
. I find it very useful, but hardly anyone seems to know of it. Also ann=FALSE
turns off all anotations, las=1
turns axis tick labels horizontal.
par(mfrow=c(3,2), oma=c(4.5, 4, 4, 2.5), mar=rep(.1, 4), cex=1, las=1)
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, xaxt="n")
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, xaxt="n", yaxt="n")
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, xaxt="n")
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, xaxt="n", yaxt="n")
plot(x=rnorm(10), y=rnorm(10), ann=FALSE)
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, yaxt="n")
title("My plot", outer=TRUE)
mtext("X-axis label", 1, 3, outer=TRUE)
mtext("Y-axis label", 2, 3, outer=TRUE, las=0)