Friday, October 3, 2014

10 3 2014 How to Convert and Plot HoboLogger Temp Data

TempDataConcatScript.R
#Temp concat
library(plyr)
library(ggplot2)
library(scales)
oysaugfeb<-read.table('OysAugtoFeb.csv', row.names=1)
#read in data, first change column names and remove data name in excel to make it work
head(oysaugfeb)
##          V2    V3    V4
## 1 8/17/2013 12:42 23.77
## 2 8/17/2013 12:57 23.20
## 3 8/17/2013 13:12 23.20
## 4 8/17/2013 13:27 23.39
## 5 8/17/2013 13:42 23.58
## 6 8/17/2013 13:57 23.77
oysaugfeb<-rename(oysaugfeb, c("V2"="Date",'V3'='Time','V4'='Temp'))
#rename columns
oysaugfeb$Date<-as.Date(oysaugfeb$Date, "%m/%d/%Y")
#tell R that these are dates
tmptst<-ddply(oysaugfeb,.(Date),summarise, mean_temp=mean(Temp,na.rm=T))
#creates mean temperature per date using summary statistics and ddply
plot(mean_temp~Date,data=tmptst)

plot of chunk unnamed-chunk-1

#plot using native plotting function to view temp graph, fix later with ggplot2 graph
oysfebmay<-read.table('Oyster_BayFebtoMay2014.csv', row.names=1)
oysmayjun<-read.table('OysterBayMaytoJune14.csv', row.names=1)
oysjun<-read.table('OysterBayJune14.csv',row.names=1)
oysjunjul<-read.table('Oyster_BayJuntoJul14.csv',row.names=1)
oysjunsep<-read.table('OysterBayJuntoSept.csv',row.names=1)
oysjulaug<-read.table('Oyster_BayJultoAug.csv',row.names=1)
oysaugsep<-read.table('Oyster_BayAug14toSep.csv',row.names=1)
#reads in as tables the other 4 temp data sets
oysfebmay<-rename(oysfebmay, c("V2"="Date",'V3'='Time','V4'='Temp'))
oysmayjun<-rename(oysmayjun, c("V2"="Date",'V3'='Time','V4'='Temp'))
oysjun<-rename(oysjun, c("V2"="Date",'V3'='Time','V4'='Temp'))
oysjunjul<-rename(oysjunjul, c("V2"="Date",'V3'='Time','V4'='Temp'))
oysjunsep<-rename(oysjunsep, c("V2"="Date",'V3'='Time','V4'='Temp'))
oysjulaug<-rename(oysjulaug, c("V2"="Date",'V3'='Time','V4'='Temp'))
oysaugsep<-rename(oysaugsep, c("V2"="Date",'V3'='Time','V4'='Temp'))
#renames the columns of the other 4 data sets
oysfebmay$Date<-as.Date(oysfebmay$Date, "%m/%d/%Y")
oysmayjun$Date<-as.Date(oysmayjun$Date, "%m/%d/%Y")
oysjun$Date<-as.Date(oysjun$Date, "%m/%d/%Y")
oysjunjul$Date<-as.Date(oysjunjul$Date, "%m/%d/%Y")
oysjunsep$Date<-as.Date(oysjunsep$Date, "%m/%d/%Y")
oysjulaug$Date<-as.Date(oysjulaug$Date, "%m/%d/%Y")
oysaugsep$Date<-as.Date(oysaugsep$Date, "%m/%d/%Y")
#tells R that all the date columns are dates
oysaugmay<-merge(oysaugfeb,oysfebmay,by = c("Date","Time","Temp"),all=T)
#merges oysaugfeb to oysfebmay into one continuous data file
oysy1<-rbind(oysaugfeb,oysfebmay,oysmayjun,oysjun,oysjunjul,oysjunsep,oysjulaug,oysaugsep)
#merges all temp files into a single table
oysmeantemp<-ddply(oysy1,.(Date),summarise,mean_temp=mean(Temp,na.rm=T))
#creates mean temp file
oysmintemp<-ddply(oysy1,.(Date),summarise,min_temp=min(Temp,na.rm=T))
#creates min temp file
oysmaxtemp<-ddply(oysy1,.(Date),summarise,max_temp=max(Temp,na.rm=T))
#creates max temp file
oysmedtemp<-ddply(oysy1,.(Date),summarise,med_temp=median(Temp,na.rm=T))
#creates median temp file
ggplot(data=oysmedtemp, aes(Date, med_temp, group=1))+geom_line(color="orange",size=1.5)+geom_abline(intercept=12.5, slope=0,color="red", size=2)+scale_x_date(breaks="1 month", minor_breaks="1 week",labels=date_format("%B %Y"))+theme(axis.text.x=element_text(angle=45, size=10, vjust=0.5))

plot of chunk unnamed-chunk-1

#generates plot of median temps
ggplot(data=oysmeantemp, aes(Date, mean_temp, group=1))+geom_line(color="orange",size=1.5)+geom_abline(intercept=12.5, slope=0,color="red", size=2)+scale_x_date(breaks="1 month", minor_breaks="1 week",labels=date_format("%B %Y"))+theme(axis.text.x=element_text(angle=45, size=10, vjust=0.5))

plot of chunk unnamed-chunk-1

#creates plot of mean temp data with 12.5 C line
ggplot(data=oysmintemp, aes(Date, min_temp, group=1))+geom_line(color="orange",size=1.5)+geom_abline(intercept=12.5, slope=0,color="red", size=2)+scale_x_date(breaks="1 month", minor_breaks="1 week",labels=date_format("%B %Y"))+theme(axis.text.x=element_text(angle=45, size=10, vjust=0.5))

plot of chunk unnamed-chunk-1

#creates plot of min temp data with 12.5 C line
ggplot(data=oysmaxtemp, aes(Date, max_temp, group=1))+geom_line(color="orange",size=1.5)+geom_abline(intercept=12.5, slope=0,color="red", size=2)+scale_x_date(breaks="1 month", minor_breaks="1 week",labels=date_format("%B %Y"))+theme(axis.text.x=element_text(angle=45, size=10, vjust=0.5))

plot of chunk unnamed-chunk-1

#creates plot of max temps with 12.5 C line
ggplot()+geom_line(data=oysmeantemp, aes(Date,mean_temp, group=1), color="orange",size=1)+geom_line(data=oysmintemp, aes(Date,min_temp,group=1),color="blue",size=1)+geom_line(data=oysmaxtemp,aes(Date,max_temp,group=1),color="red",size=1)+geom_abline(intercept=12.5, slope=0,color="red", size=0.5)+labs(x="Date",y="Min|Max|Mean Temperatures(C)")+scale_x_date(breaks="1 month", minor_breaks="1 week",labels=date_format("%B %Y"))+theme(axis.text.x=element_text(angle=45, size=10, vjust=0.5))

plot of chunk unnamed-chunk-1

#generates plot containing min,max, and mean temp color coordinated as well as 12.5 C line. 
meanmax<-merge(oysmeantemp,oysmaxtemp,by="Date",all=T)
#merges mean and max frames
medmin<-merge(oysmedtemp,oysmintemp,by="Date",all=T)
#merges median and min frames
oystmpstat<-merge(meanmax,medmin,by="Date",all=T)
#merges meanmax and medmin frames together
oysy1stat<-merge(oysy1,oystmpstat,by="Date",all=T)
#merges year 1 temp info with stats for each date
write.csv(oysy1stat, file="oysY1stats.csv",row.names=F)
#creates CSV file with all temp and stat info

No comments:

Post a Comment