2011-11-11 32 views
17

我試圖簡單地繪製R中的一些數據,y軸標籤水平,y軸刻度標籤左側。我想下面的代碼將工作:y軸標籤落在圖形窗口之外?

set.seed(1) 
n.obs  <- 390 
vol.min  <- .20/sqrt(252 * 390) 
eps   <- rnorm(n = n.obs, sd = vol.min) 
mar.default <- c(5,4,4,2) + 0.1 
par(mar = mar.default + c(0, 4, 0, 0))    # add space to LHS of plot 
pdf("~/myplot.pdf", width=5.05, height=3.8) 
plot(eps, main = "Hello World!", las=1, ylab="") # suppress the y-axis label 
mtext(text="eps", side=2, line=4, las=1)    # add horiz y-axis label 
                # 4 lines into the margin 

my image

相反,你可能會看到,y軸的標籤幾乎完全落在了圖形窗口之外。無論我如何擴大LHS保證金,這種現象依然存在。

問:我做錯了什麼?有什麼我需要做與oma參數?我需要做什麼來按照我的意圖來繪製事物?我有點不知所措!

回答

29

這是一個經典之作,也許應該是一個FAQ。您必須在之後設置par設置調用pdf,創建繪圖設備。否則,你的默認設備上修改設置:

set.seed(1) 
n.obs  <- 390 
vol.min  <- .20/sqrt(252 * 390) 
eps   <- rnorm(n = n.obs, sd = vol.min) 
       # add space to LHS of plot 
pdf("~/myplot.pdf", width=5.05, height=3.8) 
mar.default <- c(5,4,4,2) + 0.1 
par(mar = mar.default + c(0, 4, 0, 0)) 
plot(eps, main = "Hello World!", las=1, ylab="") # suppress the y-axis label 
mtext(text="eps", side=2, line=4, las=1) 
dev.off() 

enter image description here

+0

這工作。精彩。謝謝。 – lowndrul

+0

歡呼的夥計。爲什麼ON EARTH不在par par helpfile中我不知道。多麼痛苦! –