2016-12-16 74 views
1

我一直在看文檔,但我無法弄清楚如何控制在R的filled.contour()圖的X軸上的時間戳。我試過axis.POSIXct()plot.axes = {}。但對我而言都不適用。控制fill.contour繪圖上的X軸時間戳 - R

這裏是我的簡單的例子:

x1 <- seq(as.Date("2016-01-01"),as.Date("2018-07-01"),by="month") 
z1 <- c(0.45062130 ,0.51136174 ,0.6 ,0.8 ,0.29481738 ,0.6 ,0.27713756 ,0.62638512 ,0.23547530,0.29253901 ,0.75899501 ,0.67779756 ,0.51831742 ,0.08050147 ,0.71183739 ,0.13154414 ,0.79406706 ,0.13154414,0.03434758 ,0.59573892 ,0.22102821 ,0.13154414 ,0.13154414 ,0.13154414 ,0.13154414 ,0.13154414 ,0.23692593,0.95215104 ,0.38810846 ,0.17970580 ,0.05176054) 
z2 <- z1^2 
z3 <- z2^2 
df <- data.frame(x1,z1,z2,z3) 


time <- c(x1) 
depths <- c(1,2,3) 
temp2 <- as.matrix(data.frame(df$z1,df$z2,df$z3)) 
temp2<- matrix(temp2,ncol=ncol(temp2), dimnames = NULL) 
filled.contour(time,depths,temp2, col=(matlab.like2(28)), 
       ylab="Depth", xlab="Time", 
       key.title=title(expression(' Temp ('*degree*'C)')),xaxs="i") 

,輸出:enter image description here

X軸標籤在一年格式。我希望每個月的格式爲%b-%y(例如5月16日,6月16日等)。我該怎麼做呢?

回答

1

下面介紹如何使用plot.axes選項和axis.Date來完成此操作。

filled.contour(time,depths,temp2, #col=(matlab.like2(28)), 
     ylab="Depth", xlab="Time", 
     plot.axes = { axis.Date(side=1,x=time,at=time,format="%b-%y"); axis(2) }, 
     key.title=title(expression(' Temp ('*degree*'C)')),xaxs="i") 

enter image description here

+0

當我嘗試這樣做,它的工作原理。對於我的實際數據集,它爲每個時間戳記打勾。有沒有辦法控制打勾頻率? – sqwish

+1

勾號頻率由'x = time,at = time'參數控制。將時間替換爲只包含您想要用於滴答的日期的向量。我無法使用你的數據,因爲它已經是每月一次了,但是在包'xts'中查找'to.monthly'。 –