-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDietVisualization.R
More file actions
186 lines (163 loc) · 6.03 KB
/
DietVisualization.R
File metadata and controls
186 lines (163 loc) · 6.03 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#########################################################################
## This script was created by Jen Cruz and Andrew Baker to summarize #
# preliminary diet data from 4 nest cameras placed on Prairie Falcon #
# nests during the breeding season in 2023. #
#########################################################################
##############################################################
##### Set up your workspace and load relevant packages -----
#install relevant packages
install.packages( "overlap" )
# load packages relevant to this script:
library( tidyverse ) #easy data manipulation
options( dplyr.width = Inf, dplyr.print_min = 100 )
#library( lubridate ) #easy date and time manipulation
#library( sf ) #for spatial data
library( overlap ) # to turn data into circular metrics
## end of package load ###############
##########################################################
#### Load or create data -----------------------------------------
# Clean your workspace to reset your R environment. #
rm( list = ls() )
# set path to data#
#datapath <- "Z:/Common/PrairieFalcons/DietDataCameras/"
datapath <- "G:/My Drive/QCLabShared/Projects/Prairie Falcons/Diet/CameraDatabases/"
#import csv extracted from access database
dietraw <- read.csv( file = paste0( datapath,"DietPRFANest2023.csv" ),
header = TRUE )
#import drop down options used in data table
dropdown <- read.csv(file = paste0( datapath, "Column Lookup Entries.csv"),
header = TRUE )
##############################################################
################ clean and manipulate data ##########################
#check diet table
head(dietraw)
str( dietraw)
#check prey list
dropdown
#create cleand diet dataframe
dietdf <- dietraw
#check
head( dietdf);dim(dietdf)
#change column name to match dropdown names
dietdf <- dietdf %>%
mutate( ID = Prey.Item ) %>%
select( -Prey.Item )
#check
head( dietdf);dim(dietdf)
#join with relevant columns of dropdown
dietdf <- dropdown %>%
#select relevant columns that we want to join
select( ID, Prey.Item ) %>%
# join dropdown to dietdf using ID as common column
right_join( dietdf, by = "ID" )
#check
head( dietdf);dim(dietdf)
#remove superfluous columns
dietdf <- dietdf %>%
select( -Number, -Recorder, -Check )
#create lubridate date
dietdf <- dietdf %>%
mutate( date = lubridate::mdy_hm( paste( Date, Time.of.Delivery, sep = " "),
tz = "MST" ) )
#check
head( dietdf);dim(dietdf)
#create date summaries
dietdf <- dietdf %>%
mutate( hr = hour(date),
dayofyear = yday(date) )
#create a day of year with no time
dietdf <- dietdf %>%
mutate( doy = lubridate::mdy( Date,tz = "MST" ) )
#fix the WAterfall nest dates
dietdf$doy[which(dietdf$Nest == "WF")] <- dietdf$doy[which(dietdf$Nest == "WF")] + days(14)
#check
head( dietdf);dim(dietdf)
#remove year from nest label
dietdf <- dietdf %>%
dplyr::rowwise() %>%
mutate( Nest = strsplit( Nest.ID, split = "_" )[[1]][1] )
#check
unique(dietdf$Nest)
#add new labels summarizing diet at coarser levels
birdnames <- c( "bird", "horned lark", "starling" )
smallnames <- c( "kangaroo rat", "small mammal" )
dietdf <- dietdf %>%
mutate( Prey = ifelse( Prey.Item %in% birdnames, "Bird",
ifelse( Prey.Item %in% smallnames, "Mammal",
ifelse( Prey.Item == "piute ground squirrel", "Ground Squirrel",
ifelse( Prey.Item == "other", "Reptile", "Unidentified" ) ) ) ) )
#check
tail( dietdf);dim(dietdf)
#now we reorder prey times to match Marzluff et al. 1997 The Condor.
dietdf$Prey <- factor( dietdf$Prey,
levels = c( "Unidentified", "Reptile","Bird",
"Mammal","Ground Squirrel") )
##########
############### visualize data summaries #######################
#bargraphs of prey items by nest
dietdf %>%
group_by( Nest ) %>%
count( Prey ) %>%
ggplot(.) +
theme_bw( base_size = 15 ) +
geom_bar( aes( x = Nest, y = n, fill = Prey ),
stat = "identity" )
#create pallete using colors we like
cbpalette <- c( "#56B4E9","#E69F00","#009E73", "#CC79A7","#D55E00" )
#maybe we remove the other and unknowns from the list
#bargraphs of prey items by nest
dietdf %>%
# filter( Prey != "other" ) %>%
group_by( Nest ) %>%
count( Prey ) %>%
mutate( perc = n / sum( n ) * 100 ) %>%
ggplot(., aes( x = Nest, y = perc, fill = Prey ) ) +
theme_classic( base_size = 15 ) +
theme( legend.position = "top" ) +
geom_bar( stat = "identity" ) +
scale_fill_manual(values=cbpalette) +
geom_text( aes( label = paste0( round( perc,0 ), " % (",
n, ")") ),
position = position_stack(vjust = 0.5 ) ) +
labs( x = "Nest ID", y = "Prey items (%)", fill = "Prey groups" )
#### now summarizing through time ####
dietdf %>%
group_by( doy ) %>%
count( Prey ) %>%
mutate( perc = n / sum( n ) * 100 ) %>%
ggplot(.) +
theme_bw( base_size = 15 ) +
labs( x = "Date", y = "Prey items (%)" ) +
scale_fill_manual(values=cbpalette) +
geom_bar( aes( x = doy, y = perc, fill = Prey ),
stat = "identity" )
#or
dietdf %>%
mutate( dayofyear = lubridate::mdy( Date,tz = "MST" ) ) %>%
group_by( dayofyear, Nest ) %>%
count( Prey ) %>%
# mutate( perc = n / sum( n ) * 100 ) %>%
ggplot(.) +
theme_bw( base_size = 15 ) +
# labs( x = "Date", y = "Prey items (%)" ) +
geom_bar( aes( x = dayofyear, y = n, fill = Prey ),
stat = "identity" ) +
facet_wrap( ~Nest )
#now hourly deliveries
dietdf %>%
group_by( hr ) %>%
count( Prey ) %>%
mutate( perc = n / sum( n ) * 100 ) %>%
ggplot(.) +
theme_bw( base_size = 15 ) +
labs( x = "Hour of the day", y = "Prey items (%)" ) +
geom_bar( aes( x = hr, y = n, fill = Prey ),
stat = "identity" )
dietdf %>%
mutate( tm_pi <- hr* pi )
############### save relevant objects ###########################
#########
### save desired plot
ggsave( paste0( datapath, "Prey_percentage_plot.png"),
dpi = 500, height = 15, width = 20, units = "cm" )
################## end of script ####################################