Reputation: 51
I have a lot of temperature data collected continuously on 15-minute intervals in dd/mm/yyyy hh:mm:ss. I am novice at R, and I am struggling plotting the data. That code I wrote is:
install.packages("xts")
library("xts")
temp<-read.csv("C:\\Users\\data\\Temp Data.csv",header=TRUE)
str(temp)
temp$DateTime<-as.POSIXct(strptime(temp$DateTime,"%m/%d/%Y %H:%M"))
temp.xts<-xts(temp,order.by=temp$DateTime)
summary(temp.xts)
par(mfrow=c(1,1))
Temp.lab=seq(5,30,by=5)
Temp.ticks=seq(5,30,by=5)
plot(temp.xts$Temp.C["2015-05-01/2015-11-5"],axes=F,auto.grid=FALSE,col="gray48",ylim=c(5,30),main="",cex.main=1.0,lwd=1)
axis(2,at=Temp.ticks,labels=format(Temp.lab,scientific=FALSE),ylab="Temperature (C)",las=1,cex.axis=1)
mtext("Water Temperatuer",side=3,line=-1.25,cex=1,font=2,las=1,adj=0.025)
mtext("",side=2,line=3,las=3,cex=1)
mtext("",side=1,line=3,cex=1)
When I run these I get the error: Error in plot.xts(temp.xts$Temp.C["2015-05-01/2015-11-15"], axes = F, : 'x' must be a time-series object.
The structure of my data looks like;
head(temp) Station.ID DateTime Temp.C 1 Station.01 2015-05-08 14:00:00 14.002 2 Station.01 2015-05-08 14:15:00 13.906 3 Station.01 2015-05-08 14:30:00 13.978 4 Station.01 2015-05-08 14:45:00 14.026 5 Station.01 2015-05-08 15:00:00 14.074 6 Station.01 2015-05-08 15:15:00 14.098
str(temp) 'data.frame': 18283 obs. of 3 variables: $ Station.ID: Factor w/ 1 level "Station.01": 1 1 1 1 1 1 1 1 1 1 ... $ DateTime : Factor w/ 18279 levels "10/1/2015 00:00",..: 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 ... $ Temp.C : num 14 13.9 14 14 14.1 ...
Here is a snippet of my dataset:
head(temp,20) Station.ID DateTime Temp.C 1 Station.01 5/8/2015 14:00 14.002 2 Station.01 5/8/2015 14:15 13.906 3 Station.01 5/8/2015 14:30 13.978 4 Station.01 5/8/2015 14:45 14.026 5 Station.01 5/8/2015 15:00 14.074 6 Station.01 5/8/2015 15:15 14.098 7 Station.01 5/8/2015 15:30 14.122 8 Station.01 5/8/2015 15:45 14.146 9 Station.01 5/8/2015 16:00 14.146 10 Station.01 5/8/2015 16:15 14.146 11 Station.01 5/8/2015 16:30 14.146 12 Station.01 5/8/2015 16:45 14.146 13 Station.01 5/8/2015 17:00 14.122 14 Station.01 5/8/2015 17:15 14.122 15 Station.01 5/8/2015 17:30 14.122 16 Station.01 5/8/2015 17:45 14.098 17 Station.01 5/8/2015 18:00 14.122 18 Station.01 5/8/2015 18:15 14.098 19 Station.01 5/8/2015 18:30 14.098 20 Station.01 5/8/2015 18:45 14.098
Any suggestions? Help is greatly appreciated.
Upvotes: 2
Views: 2448
Reputation: 95
I had the same error "'x' must be a time-series object." for one of my (looking similar) scripts in a debian system (at the same time in a suse box it was fine) and
"solved" it by explicitly casting to numeric using as.numeric
. In your case something like plot(as.numeric(temp.xts$Temp.C["2015-05-01/2015-11-5"]),.....
Upvotes: 4