避免R中坐标轴标签重叠问题

13

我想在图表中使用更大的字体大小来标记数据。

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()方法设置?)。

我该如何避免标签重叠?

谢谢您的帮助。

Sven


注意:如果您想移动轴标签,请单独打印它:https://dev59.com/SG035IYBdhLWcg3wSOGr - BurninLeo
3个回答

20

使用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.

当我使用mgp时,y-label和x-label都会移动。是否也可以仅移动y-label?是否有一个好的教程来教授这些基本知识?我总是在帮助中迷失... :-( - R_User
2
如果您不喜欢默认的标签位置,通常的方法是使用 ylab="" 并使用 axis(... , line=<integer>)。?axis - IRTFM
@DWin:我会尝试你的建议。现在,我先为一个轴设置 ´par(mgp)´ 的标题,然后再次使用 ´par(mgp)´,并为下一个轴设置 ´title()´。 - R_User

3

一种快速且不太正规的方法是使用 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,但我不知道它们会发生什么。我猜想它们只是被忽略了。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接