2014-11-01 86 views
0

我正在繪製如下代碼中的幾個圖(示例代碼應該生成三個不同的圖)。我遇到的問題是y軸的縮放。R:在幾個圖中定義ylim在abline y = 0,軸縮放

我的問題:我怎麼能定義我的ylimit,使y = 0處的abline總是可見的?

此刻yaxis自動縮放。我正在尋找baseR中的解決方案。

xy <- structure(list(NAME = structure(c(2L, 3L, 1L, 1L), .Label = c("CISCO","JOHN", "STEPH"), class = "factor"), ID = c(41L, 49L, 87L, 87L), X_START_YEAR = c(1965L, 1948L, 1959L, 2003L), Y_START_VALUE = c(940L,-1760L, 110L, 866L), X_END_YEAR = c(2005L, 2000L, 2000L, 2007L), Y_END_VALUE = c(940L, -1760L, 110L, 866L), LC = structure(c(1L,1L, 2L, 2L), .Label = c("CA", "US"), class = "factor")), .Names = c("NAME", "ID", "X_START_YEAR", "Y_START_VALUE", "X_END_YEAR", "Y_END_VALUE","LC"), class = "data.frame", row.names = c(NA, -4L)) 

ind <- split(xy,xy$ID) # split by ID for different plots 

# Plots 
for (i in ind){ 
    xx = unlist(i[,grep('X_',colnames(i))]) 
    yy = unlist(i[,grep('Y_',colnames(i))])  
    fname <- paste0(i[1, 'ID'],'.png') 
    png(fname, width=1679, height=1165, res=150) 

    # Define ylim so that y=0 (abline) is always in plots 

    par(mar=c(6,8,6,5)) 
    plot(xx,yy,type='n',main=unique(i[,1]), xlab="Time [Years]", ylab="Value [mm]") 
    abline(h=0, col = "gray60") 
    i <- i[,-1] 
    segments(i[,2],i[,3],i[,4],i[,5],lwd=2) 
    points(xx, yy, pch=21, bg='white', cex=0.8) 
    dev.off() 
} 
+0

謝謝你,這個作品! – kurdtc 2014-11-01 21:24:55

回答

2

只要確保包括高於/低於0一些數字,用range()給予最大上方和下方零最小值。怎麼樣,包括

ylim = range(c(yy,-.5,.5)) 

作爲參數傳遞給plot()