避免在R中重叠轴标签

时间:2011-07-21 15:16:47

标签: r plot overlap

我想在标签中使用较大的字体大小绘制数据。

x = c(0:10)
y = sin(x) + 10

plot (
    x, y, type="o",
    xlab = "X values",
    ylab = "Y values",
    cex.axis = "2",
    cex.lab = "2",
    las = 1
)

不幸的是,y轴上的数字与y轴的标签重叠。我试图使用mar,但是没有用(顺便说一句,我怎样才能找出哪些图形参数可以直接用在plot命令中,哪些必须用par() - 方法设置?)。

如何避免标签重叠?

感谢您的帮助。

斯文

3 个答案:

答案 0 :(得分:18)

使用par(mar)增加绘图边距,使用par(mgp)移动轴标签。

par(mar = c(6.5, 6.5, 0.5, 0.5), mgp = c(5, 1, 0))
#Then call plot as before

在帮助页?par中,它解释了哪些参数可以直接用于plot,哪些参数必须通过par进行调用。

  

有几个参数只能通过调用'par()'来设置:

    • ‘"ask"’,

    • ‘"fig"’, ‘"fin"’,

    • ‘"lheight"’,

    • ‘"mai"’, ‘"mar"’, ‘"mex"’, ‘"mfcol"’, ‘"mfrow"’, ‘"mfg"’,

    • ‘"new"’,

    • ‘"oma"’, ‘"omd"’, ‘"omi"’,

    • ‘"pin"’, ‘"plt"’, ‘"ps"’, ‘"pty"’,

    • ‘"usr"’,

    • ‘"xlog"’, ‘"ylog"’

 The remaining parameters can also be set as arguments (often via
 ‘...’) to high-level plot functions such as ‘plot.default’,
 ‘plot.window’, ‘points’, ‘lines’, ‘abline’, ‘axis’, ‘title’,
 ‘text’, ‘mtext’, ‘segments’, ‘symbols’, ‘arrows’, ‘polygon’,
 ‘rect’, ‘box’, ‘contour’, ‘filled.contour’ and ‘image’.  Such
 settings will be active during the execution of the function,
 only.  However, see the comments on ‘bg’ and ‘cex’, which may be
 taken as _arguments_ to certain plot functions rather than as
 graphical parameters.

答案 1 :(得分:2)

快速而肮脏的方式是使用par并在ylab中添加换行符,即使它在概念上非常糟糕。

x = 0:10
y = sin(x) + 10

par(mar=c(5,7,4,2))
plot (
    x, y, type="o",
    xlab = "X values",
    ylab = "Y values\n",
    cex.axis = "2",
    cex.lab = "2",
    las = 1
)

关于您可以直接在plot中设置哪些参数,请查看?plot.default?plot.xy,因为他们会收到...个句子。还有几个调用未记录的函数(据我所知),如localWindowlocalBox,但我不知道它们会发生什么。我猜他们只是被忽略了。

答案 2 :(得分:0)

您可以将mgp参数放入title()函数中,以避免之后重置默认值。这样,参数仅作用于函数添加的标签。像这样:

plot (
x, y, type="o",
xlab = "",         #Don't include xlab in main plot
ylab = "Y values",
cex.axis = "2",
cex.lab = "2",
las = 1
)
title(xlab="X values"
 ,mgp=c(6,1,0))    #Set the distance of title from plot to 6 (default is 3).