2012-09-11 122 views
3

我在正確地繪製下圖的標籤的問題:軸標籤與軸線重疊值

scatterhist = function(x, y, xlab="", ylab=""){ 
    zones=matrix(c(2,0,1,3), ncol=2, byrow=TRUE) 
    layout(zones, widths=c(4/5,1/5), heights=c(1/5,4/5)) 
    xhist = hist(x, plot=F, breaks=10) 
    yhist = hist(y, plot=F, breaks=10) 
    top = max(c(xhist$counts, yhist$counts)) 

    par(mar=c(3,3,1,1)) 
    plot(x, y) 

    par(mar=c(0,3,1,1)) 
    barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0) 

    par(mar=c(3,0,1,1)) 
    barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE) 

    par(oma=c(3,3,0,0)) 
    mtext(xlab, side=1, line=1, outer=TRUE, adj=0, 
    at=.8 * (mean(x)-min(x))/(max(x)-min(x))) 
    mtext(ylab, side=2, line=1, outer=TRUE, adj=0, 
    at=.8 * (mean(y)-min(y))/(max(y)-min(y))) 
} 

當I型:

scatterhist(x[,6], x[,7], xlab="Cost of Taxi", ylab="Cost of Fuel") 

標籤與所述軸的值重疊。但是,如果我輸入:

scatterhist(x[,6], x[,7], xlab="Cost of Taxi", ylab="Cost of Fuel") 
scatterhist(x[,6], x[,7], xlab="Cost of Taxi", ylab="Cost of Fuel") 

第二個圖形顯示正確......任何人都可以幫我解決這個問題嗎?我也試圖通過windows()打開圖形窗口之前的第一個陰謀,但這是行不通的...

謝謝! Stefano

回答

2

您的par(oma=c(3,3,0,0))行應該在第一個par(mar=...)調用之前出現,因爲它應該應用於整個設備區域(即,如果已經繪製了某些圖,則不能更改外邊距的大小)。

scatterhist = function(x, y, xlab="", ylab=""){ 
    zones=matrix(c(2,0,1,3), ncol=2, byrow=TRUE) 
    layout(zones, widths=c(4/5,1/5), heights=c(1/5,4/5)) 
    par(oma=c(3,3,0,0)) 

    xhist = hist(x, plot=F, breaks=10) 
    yhist = hist(y, plot=F, breaks=10) 
    top = max(c(xhist$counts, yhist$counts)) 

    par(mar=c(3,3,1,1)) 
    plot(x, y) 

    par(mar=c(0,3,1,1)) 
    barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0) 

    par(mar=c(3,0,1,1)) 
    barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE) 

    mtext(xlab, side=1, line=1, outer=TRUE, adj=0, 
     at=.8 * (mean(x)-min(x))/(max(x)-min(x))) 
    mtext(ylab, side=2, line=1, outer=TRUE, adj=0, 
     at=.8 * (mean(y)-min(y))/(max(y)-min(y))) 
    } 
+0

清晰直接的答案,非常感謝! – Stezzo