2017-06-16 17 views
-1

我有兩年的時間(2012-2013)每15分鐘獲得的潮汐高度數據,格式如下:時間41000,41000.01042,41000.02083等;身高1.813,1.802,1.774等來自Excel時間序列號的R圖?

時間序列號是從Excel,我的理解是1900年1月1日後的天數。我希望繪製R中的數據與x軸上的日期, y軸上的軸和高度。有人可以請教如何做到最好?謝謝。

+0

'as.Date(41000,origin ='1899-12-30')'您可以將它們轉換爲像這樣的日期。 – Masoud

+1

或使用包readxl中的read_excel函數導入Excel數據並保持不變。然後,您需要在軟件包lubridate中使用force_tz()來分配正確的時區。 –

回答

1

如果您使用的是Windows那麼這將適合你:

as.POSIXct(as.Date(41000.02083,origin="1899-12-30")) 

> "2012-03-31 20:29:59 EDT" #This has been converted based on my local timezone 

在Mac上,你需要一個不同的origin="1904-01-01"

我在此示例性數據集:

datenum <- c(41000, 41000.01042, 41000.02083) 
height <- c(2.813, 1.802, 0.774) #Edit the heights to make it visually appealing 
df <- data.frame(cbind(datenum = datenum, height=height)) 

然後用適當的日期時間的格式添加列:

df$Date <- as.POSIXct(as.Date(df$datenum,origin="1899-12-30")) #Origin for windows excel 

而繪製的條形圖:

barplot(df$height,names.arg=df$Date,col="lightblue") 

這將是圖表:

enter image description here