2011-07-21 104 views
11

我想绘制标签中字体较大的图形中的数据。避免R中的重叠轴标签

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

如何避免标签重叠?

感谢您的帮助。

斯文

+0

注:如果你喜欢移动轴标签,分别打印:http://stackoverflow.com/questions/5506046/how-do-i-put-more-space-between-在axis-label-and-axis-title-in-an-r-boxplot – BurninLeo

回答

17

使用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被调用。

有几个参数,只能通过将呼叫设置“看齐()”:

• ‘"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. 
+0

当我使用mgp时,y标签以及x标签被移动。是否也可以只移动y标签?有没有一个很好的教程,教这些基本的东西?我总是迷失在帮助中,...... :-( –

+2

)如果你不喜欢默认的放置位置,而不是通常的方法是'ylab =“”'并使用'axis(...,line = )'。 ?轴 –

+0

@DWin:我会尝试一下你的建议,现在我首先设置'par(mgp)'为一个轴的标题,然后'par(mgp)'和'title()'为bext轴。 –

2

快速和肮脏的方法是使用parylab添加一个新行,即使它的概念太可怕了。

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看看,他们将收到的... arugments。还有一些打电话给无证函数(据我所知),如localWindowlocalBox,但我不知道他们会发生什么。我猜他们只是被忽略了。

0

你可以把MGP参数入题()函数,以避免重新设置之后您的默认值。这样该参数仅作用于由该函数添加的标签。像这样:

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