2015-12-19 89 views
3

我試圖繪製從2015年7月4日開始到2015年11月23日結束的以15分鐘爲間隔從一個流中收集的水溫。我希望x軸在每個刻度標記上標記爲「Apr」,「May」等,直到「Nov」。我寫的代碼使人看起來很好看,但溫度讀數與x軸刻度線沒有對齊;他們從2015年4月1日開始,而不是2015年4月7日;因此,所有的溫度讀數都要比他們應該的時間移動7天。換句話說,我希望x軸從4月1日開始,但數據要在收集日期開始。我認爲這與我如何排序時間和應用滴答有關,但我不知道如何糾正它。任何幫助將不勝感激。謝謝時間序列中的日期與X軸不一致

#Load Data 
trib<-read.csv("C:\\r.data\\trib.csv",header=TRUE)  

#Index Time 
    trib$DateTime<-as.POSIXct(strptime(trib$DateTime,"%m/%d/%Y %H:%M")) 

#Create Time Series 
trib.xts<-xts(trib,order.by=trib$DateTime) 

#Example Data 
DateTime      Temp 
22666 2015-04-07 13:30:00  NA 
22667 2015-04-07 13:45:00  2.983 
22668 2015-04-07 14:00:00  3.142 
22669 2015-04-07 14:15:00  3.274 
22670 2015-04-07 14:30:00  3.354 
22671 2015-04-07 14:45:00  3.433 
22672 2015-04-07 15:00:00  3.485 
22673 2015-04-07 15:15:00  3.670 
22674 2015-04-07 15:30:00  3.749 
22675 2015-04-07 15:45:00  3.827 

#Plot 
par(mfrow=c(1,1)) 

margins <- par(mar=c(2,2,1,1)+0.1) 
omargins <- par(oma=c(2,2,0.5,0.5)+0.1) 

Temp.lab=seq(0,30,by=5) 
Temp.ticks=seq(0,30,by=5) 
plot(trib.xts$Temp,axes=FALSE,auto.grid=FALSE,col="gray",ylim=c(0,30),main="Stream Name",cex.main=0.8,lwd=1) 
axis(2,at=Temp.ticks,labels=format(Temp.lab,scientific=FALSE),ylab="Temperature (C)",las=1,cex.axis=0.8) 

times <- time(trib.xts$DateTime["2015-04-01/2015-11-15"]) 
ticksm <- seq(times[1], times[length(times)], by = "months") 
month.lab = c("Apr","May","Jun","Jul","Aug","Sep","Oct","Nov") 
axis(1, at = ticksm, labels = month.lab, tcl = -0.5,cex.axis=0.8,xlab="Month") 
mtext("Temperature (°C)",side=2,line=3,las=3,cex=0.8) 
mtext("Month",side=1,line=3,cex=0.8) 
+0

我不認爲你有正確的索引列輸入。左側的整數列看起來並不像您已經將索引指定爲應該嘗試的日期時間。 'plot.xts'方法將使用索引作爲x軸定位。這進一步表明需要使用'dput'作爲輸出函數。 –

+0

感謝您的回覆,但是我會將日期時間正確編入索引嗎?再次感謝你。 – mkRuser

+0

假設你打算輸入「我該如何更好的索引」,那麼我不能建議。您尚未顯示如何創建此數據對象。 –

回答

0

這是一種方法,假設你不介意ggplot解決方案。

# Let's first make SampleData to your specifications: 
trib.xts <- data.frame(DateTime = seq.POSIXt(as.POSIXct("2015/04/07 13:30:00",Format="%Y/%m/%d %H:%M:%S", timezone ="CEST"), 
              as.POSIXct("2015/11/23 13:00:00",Format="%Y/%m/%d %H:%M:%S", timezone ="CEST"), 
              by="15 min")) 
# Then add temperature, a bit warmer in summer. 
trib.xts$Temp <- 2*sin(month(trib.xts$DateTime))+rnorm(22083,mean=7,sd=2) 

#Now, we need ggplot and scales 
library(ggplot2) 
library(scales) 

#then make a plot 
(plot1 <- ggplot(trib.xts, aes(x=DateTime, y=Temp)) + 
    geom_point(col="gray",shape=1) + 
    theme_bw(24) + 
    ggtitle("Stream Name") + 
    ylab("Temperature (°C)") + 
    xlab("Month")) 

# Now set the breaks, labels and x-limits as you please 
(plot1 <- plot1 + 
    scale_x_datetime(breaks = "1 month", 
       labels=date_format("%b"), 
       limits = as.POSIXct(c("2015-03-30","2015-11-25"), timezone="CEST"))) 

enter image description here 注意,我x軸是荷蘭語。你的將用你自己的語言,但是你可以通過改變Sys.setlocale來改變它。

+0

感謝您提供解決方案RHA,但是如何從.csv中讀取非ggplot方式的最終圖?再次感謝你。 – mkRuser

+0

@mkRuser我不知道。我是一個ggplot愛好者。索引的問題在於R無法知道要開始。您可以嘗試使用NA添加從1-apr到7-apr的所有日期。但我會去ggplot ... – RHA

0

謝謝,RHA。我在本月初添加了錯誤的溫度讀數(例如2015年4月1日00:00,北美),正如您所建議的那樣。這似乎是用我創建的x軸刻度標記「排列」數據。有時間熟悉ggplot。