forked from xingyaochen/turtleAnt_dataAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plottingVideo.Rmd
161 lines (135 loc) · 4.75 KB
/
plottingVideo.Rmd
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: "Turtle Ant Video Data Analysis"
author: "Xingyao Chen"
date: "7/20/2017"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(cache = TRUE)
```
Install/library in Packages and Load Data from GDrive
```{r}
setwd('../turtleAnt_dataAnalysis/')
library(ggplot2)
library(gsheet)
library(reshape2)
urls=c(
#'https://docs.google.com/spreadsheets/d/1OCKoUgQ_A-do18Uc_ulIzoQ_P22nb8lim9wBlwROR4c/edit#gid=1849028624',
'https://docs.google.com/spreadsheets/d/1OCKoUgQ_A-do18Uc_ulIzoQ_P22nb8lim9wBlwROR4c/edit#gid=0',
'https://docs.google.com/spreadsheets/d/1OCKoUgQ_A-do18Uc_ulIzoQ_P22nb8lim9wBlwROR4c/edit#gid=433819656',
'https://docs.google.com/spreadsheets/d/1OCKoUgQ_A-do18Uc_ulIzoQ_P22nb8lim9wBlwROR4c/edit#gid=1138436910')
colData=data.frame()
colonies=c('V1','V2','V3')
for(i in 1:length(urls)){
coli=read.csv(text=gsheet2text(urls[i], format='csv'))[-1,1:14]
coli$colony=colonies[i]
print(names(coli))
colData=rbind(colData,coli)
}
```
#Clean Up and Add Time Data
```{r}
colData=na.omit(colData)
colData$Posix=as.POSIXct(colData$Time , format = "%I:%M:%S")
colData$Enter.Box=as.numeric(as.character(colData$Enter.Box))
colData$Box=as.factor(colData$Box)
colData$colony=as.factor(colData$colony)
colData=na.omit(colData)
summary(colData)
colData$Enter.Nest=as.numeric(as.character(colData$Enter.Nest))
colData$Exit.Nest=as.numeric(as.character(colData$Exit.Nest))
```
#Plot # of Entries into Nest Over Time for Each Colony
```{r}
#system('mkdir 7-17-17')
for(i in colonies){
#png(paste0('7-17-17/enterNest_col',i,'png'))
p=ggplot(colData[c(colData$colony==i&colData$Nest!=colData$Nest[1]),], aes(y=Enter.Nest, x=Posix, fill=Nest, color=Nest))+
#geom_bar(stat='identity')+
geom_line(size=1.5)+
labs(x='Time')+
scale_colour_manual(values=c( 'dodgerblue2','deepskyblue1','dodgerblue4', 'firebrick3', 'firebrick1', 'firebrick4'))+
theme(axis.title=element_text(size=14), legend.title=element_text(size=14),legend.text=element_text(size=12))+
theme_classic()
plot(p)
#graphics.off()
}
```
#Add a Column for Which Section (R or D)
```{r}
r=c(2,3,5,6)
d=c(7,8,9,10,11,12)
for(i in 1:nrow(colData)){
if(colData$Box[i]%in%r){
colData$Section[i]='R'
}
else if(colData$Box[i]%in%d){
colData$Section[i]='D'
}
}
colData_sec=colData[!is.na(colData$Section),]
#Make new, shortened dataframe
colData_s=colData_sec[,c('Posix','Section','Enter.Box','Exit.Box','colony')]
#colData_s=colData_s[colData_s$Box!=colData_s$Box[1],]
colData_s$Enter.Box=as.numeric(as.character(colData_s$Enter.Box))
colData_s$Exit.Box=as.numeric(as.character(colData_s$Exit.Box))
colData_s$colony=as.factor(colData_s$colony)
colData_split=split(colData_s, colData_s$colony)
```
Add up the total number of entries and exits every 10 minutes
```{r}
cols=c('V1','V2','V3')
fullSum=data.frame()
for(i in 1:length(colData_split)){
sumdf=cbind(melt(acast(colData_split[[i]][,c(1,2,3)], Section~Posix, sum)),
melt(acast(colData_split[[i]][,c(1,2,4)], Section~Posix, sum))$value)
sumdf$Colony=cols[i]
names(sumdf)=c('Section','Time','Exit','Enter','Colony')
fullSum=rbind(fullSum,sumdf)
}
fullSum=melt(fullSum, id.vars=c('Section','Colony','Time'))
fullSum$Time=as.POSIXct(fullSum$Time)
head(fullSum)
```
#Plot the results
```{r}
#png('finalPoster/activity_ct.png')
ggplot(fullSum,aes(y=value, x=Time, color=Section, group=Section))+
geom_line(size=1.3)+
geom_point()+
facet_wrap(~Colony)+
scale_color_manual(values=c('dodgerblue2','firebrick'),name=c('Section'))+
theme(title=element_text(size=14, face='bold'),axis.title=element_text(size=13),
axis.text=element_text(size=12, angle=45, hjust=1),
legend.title=element_text(size=13),legend.text=element_text(size=12))+
labs(y='Ant Activity (# of entries and exits')+
theme_classic()
#graphics.off()
##
```
#Plot Movment from O to R and O to D
```{r}
colData=data.frame()
colonies=c(4,5,6)
for(i in 1:length(urls)){
coli=read.csv(text=gsheet2text(urls[i], format='csv'))[-1,c(2,5,16:17)]
coli$colony=colonies[i]
print(names(coli))
colData=rbind(colData,coli)
}
colDataOR=na.omit(colData[,-3])
colDataOD=na.omit(colData[,-4])
colData$Posix=as.POSIXct(colData$Time , format = "%I:%M:%S")
#colData$Enter.Box=as.numeric(as.character(colData$Enter.Box))
#colData$Box=as.factor(colData$Box)
colData$colony=as.factor(colData$colony)
dts=na.omit(melt(colData[,-c(1:2)], id.vars=c("Posix",'colony')))
ggplot(dts, aes(x=Posix,y=value, color=as.factor(variable)))+
geom_point()+
geom_line(size=1.3)+
facet_wrap(~colony)+
scale_color_manual(values=c('dodgerblue2','firebrick'),name=c('Section'))+
theme(title=element_text(size=14, face='bold'),axis.title=element_text(size=13), legend.title=element_text(size=13),legend.text=element_text(size=12))+
theme_classic()
```