-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
92 lines (92 loc) · 3.89 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
getwd()
rm(list=ls())
library(ggplot2)
library(lubridate)
library(dplyr)
library(zoo)
setwd("C:/Users/Steve Rolfe/Google Drive/Phenomics/Thermal_analysis")
input_file="./data/thermal imaging.csv"
input_file="./data/thermal imaging.csv"
output_file="./output/BR7_thermal_out.csv"
plant_codes="./data/BR7_plant_codes.csv"
#subset of data to plot using grep format
sset<-"BR7-0[1-5]1[1-3][1-4]"
#read in the data
tdata<-read.csv(input_file,header=TRUE,stringsAsFactors = FALSE)
if(is.object(tdata)==FALSE) {stop(c("Can't open file:",input_file))}
#check that there the required headings are there and throw an error if not
if(is.na(match("BR_code",colnames(tdata)))){stop("BR_code missing from input file header")}
if(is.na(match("date_time",colnames(tdata)))){stop("date_time missing from input file header")}
head(tdata)
#this file contains info about the plants used
plant_codes<-read.csv(plant_codes,header = TRUE,stringsAsFactors = FALSE)
if(is.object(plant_codes)==FALSE) {stop(c("Can't open file:",plant_codes))}
#dpi offset from first data point
#it's good to express everything as dpi but 1st data point might not be dpi
#set the date of infection
dpi_datetime<-"2015-7-6 00:00:00"
#now set the factors manually
tdata$BR_code<-as.factor(tdata$BR_code)
#and set the plants,spores,etc
#the plant code is the sixth character in the BR7 string
tdata$plant<-as.factor(substr(tdata$BR_code,6,6))
#the spore concentration code is the 8th character
tdata$spore<-as.factor(substr(tdata$BR_code,8,8))
#the repeat is the 9th
tdata$rep<-as.factor(substr(tdata$BR_code,9,9))
head(tdata)
#now merge the plant names with the data frame
tdata<-merge(tdata,plant_codes,by.x="plant",by.y="plant_code")
head(tdata)
#now do some standard time processing
#use difftime to get the days since inoculation
tdata$dpi<-difftime(tdata$date_time,dpi_datetime,units="days")
#make it a factor when we just have a few days (but not when we want a continuous x scale)
tdata$dpi<-as.factor(tdata$dpi)
subdata<-subset(tdata,grepl(sset,tdata$BR_code))
subdata<-droplevels(subdata)
#plot the raw data as a boxplot
p1<-ggplot(subdata,aes(x=interaction(dpi,spore),avg_leaf_temp))
p1<-p1+geom_boxplot(aes(fill=spore),outlier.colour="red",outlier.size=3)
p1<-p1+facet_wrap(~plant_name)
#the labels are untidy
new_labels<-rep(levels(as.factor(subdata$dpi)),length(levels(as.factor(subdata$spore))))
p1<-p1+scale_x_discrete(labels=new_labels,name="DPI")
p1<-p1+theme(axis.text.x=element_text(angle=90))
p1
```
#use the summarise function to calculate the average for each treatment
t<-summarise(group_by(subdata,plant,spore,dpi),mean_temp=mean(avg_leaf_temp))
t
#we only want the spore 1 data
t<-subset(t,spore==1)
t$int<-interaction(t$plant,t$dpi)
t$plant<-NULL
t$spore<-NULL
t$dpi<-NULL
#now merge with subdata
#merge doesn't like the interaction function so we need to add a new column and merge by that
subdata$int<-interaction(subdata$plant,subdata$dpi)
subdata<-merge(subdata,t,by="int")
#now calculate the difference
subdata<-mutate(subdata,diff_temp=avg_leaf_temp-mean_temp)
#plot the processed data as a boxplot
p2<-ggplot(subdata,aes(x=interaction(dpi,spore),diff_temp))
p2<-p2+geom_boxplot(aes(fill=spore),outlier.colour="red",outlier.size=3)
p2<-p2+facet_wrap(~plant_name)
#the labels are untidy
new_labels<-rep(levels(as.factor(subdata$dpi)),length(levels(as.factor(subdata$spore))))
p2<-p2+scale_x_discrete(labels=new_labels,name="DPI")
p2<-p2+theme(axis.text.x=element_text(angle=90))
p2
subdata<-mutate(subdata,lf_vs_env=env_temp-avg_leaf_temp)
p3<-ggplot(subdata,aes(x=interaction(dpi,spore),lf_vs_env))
p3<-p3+geom_boxplot(aes(fill=spore),outlier.colour="red",outlier.size=3)
p3<-p3+facet_wrap(~plant_name)
#the labels are untidy
new_labels<-rep(levels(as.factor(subdata$dpi)),length(levels(as.factor(subdata$spore))))
p3<-p3+scale_x_discrete(labels=new_labels,name="DPI")
p3<-p3+theme(axis.text.x=element_text(angle=90))
p3
write.csv(subdata,output_file)
source('~/.active-rstudio-document', echo=TRUE)