2011-07-21 252 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).