#file.choose()
sd <- read.csv("C:\\Users\\gkrochmal\\Downloads\\Seed Dispersal Data - Sheet1.csv") #naming data

library(plyr) #loading packages
library(ggplot2)

head(sd) #summary of first 6 rows
##    Factor Seed_Type Distance Angle Season
## 1 No wind       Dog       15    86      1
## 2 No wind       Dog       10    43      1
## 3 No wind Butterfly       34   132      1
## 4 No wind       Dog       55    71      1
## 5 No wind Butterfly       55    82      1
## 6 No wind       Fox       68    65      1
str(sd) #looking at data types
## 'data.frame':    182 obs. of  5 variables:
##  $ Factor   : Factor w/ 2 levels "No wind","Wind": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Seed_Type: Factor w/ 3 levels "Butterfly","Dog",..: 2 2 1 2 1 3 3 1 2 1 ...
##  $ Distance : int  15 10 34 55 55 68 95 120 119 118 ...
##  $ Angle    : int  86 43 132 71 82 65 39 49 51 69 ...
##  $ Season   : int  1 1 1 1 1 1 1 1 1 1 ...
#Using ddply to summarize seed type, season, experimental factor and mean ofdistance data

seed <- ddply(sd, c("Seed_Type", "Season", "Factor"), summarise, meanDistance=mean(Distance))

head(seed)#summmary of first six rows of data
##   Seed_Type Season  Factor meanDistance
## 1 Butterfly      1 No wind     81.75000
## 2 Butterfly      1    Wind     68.55556
## 3 Butterfly      2 No wind    119.80000
## 4 Butterfly      2    Wind    111.50000
## 5 Butterfly      3 No wind    124.66667
## 6 Butterfly      3    Wind     71.20000
#creating ddply for ggplots

graph1 <- ddply(sd, c("Seed_Type", "Season", "Factor"), summarise,
                 dataRep  = sum(!is.na(Distance)),
                 dataMean = mean(Distance, na.rm=T), 
                 dataSD   = sd(Distance, na.rm=T),
                 dataSE   = dataSD / sqrt(dataRep)) 

head(graph1)
##   Seed_Type Season  Factor dataRep  dataMean   dataSD   dataSE
## 1 Butterfly      1 No wind       4  81.75000 43.86627 21.93314
## 2 Butterfly      1    Wind       9  68.55556 89.19517 29.73172
## 3 Butterfly      2 No wind       5 119.80000 84.89228 37.96498
## 4 Butterfly      2    Wind       8 111.50000 34.88348 12.33317
## 5 Butterfly      3 No wind       6 124.66667 56.96900 23.25750
## 6 Butterfly      3    Wind       5  71.20000 41.11812 18.38858
#creating boxplot of experimental factor and distance
boxplotsd <- ggplot(data = sd, aes(x = Factor, y = Distance))

boxplotsd + 
  theme_classic() +
  geom_boxplot()

#creating boxplot with experimental factor, seed type, and distance(two categorical variables)

boxplot2 <- ggplot(data = sd, aes(x = Factor, y = Distance, fill = Seed_Type))

boxplot2 + 
  theme_classic() +
  geom_boxplot()

#creating a two factor bar graph(seed type, factor and distance mean)

bar_2v <- ddply(sd, c("Factor", "Seed_Type"), summarise,
                dataRep  = sum(!is.na(Distance)),
                dataMean = mean(Distance, na.rm=T), 
                dataSD   = sd(Distance, na.rm=T),
                dataSE   = dataSD / sqrt(dataRep))

head(bar_2v)#summary of six rows of data
##    Factor Seed_Type dataRep  dataMean   dataSD   dataSE
## 1 No wind Butterfly      27  93.88889 61.59192 11.85337
## 2 No wind       Dog      24  90.04167 71.78863 14.65379
## 3 No wind       Fox      32 102.75000 60.08757 10.62208
## 4    Wind Butterfly      32  81.00000 70.03271 12.38015
## 5    Wind       Dog      32 100.40625 71.84026 12.69968
## 6    Wind       Fox      35  84.88571 61.13956 10.33447
#loading the graphing data for x and y axis
bar_2 <- ggplot(data = bar_2v, aes(x = Factor, y = dataMean, fill = Seed_Type))

#setting error bar limits
limits <- aes(ymax = dataMean + dataSD, ymin = dataMean - dataSD) 

dodge <- position_dodge(width = 0.9) # Dodge overlapping objects side-to-side

#running my graph
bar_2 + 
  theme_classic() +                   
  geom_bar(stat="identity", position = dodge, colour = "black") +         
  geom_errorbar(limits, width = 0.2, position = dodge) +
  scale_fill_brewer(palette="Greens")# Limits

#Assessing my data normality

#histogram of distance
hist(seed$meanDistance)

qqnorm(seed$meanDistance) #qqplots
qqline(seed$meanDistance)

#data was not that normal so log transformation

qqnorm(log10(seed$meanDistance)) #qqplots of transformed data
qqline(log10(seed$meanDistance))

#Running an ANOVA

anova <- aov(log10(meanDistance)~ Seed_Type, data=seed)

plot(anova)

summary(anova)
##             Df Sum Sq Mean Sq F value Pr(>F)
## Seed_Type    2 0.0218 0.01090   0.542  0.588
## Residuals   27 0.5436 0.02013
anova2 <- aov(log10(meanDistance)~ Seed_Type * Factor, data=seed)
plot(anova2)

summary(anova2) #checking interactions
##                  Df Sum Sq  Mean Sq F value Pr(>F)
## Seed_Type         2 0.0218 0.010903   0.532  0.594
## Factor            1 0.0084 0.008427   0.411  0.527
## Seed_Type:Factor  2 0.0434 0.021701   1.059  0.362
## Residuals        24 0.4918 0.020490
#creating my map
#file.choose()
dispersalmap <- read.csv("C:\\Users\\gkrochmal\\Downloads\\Seed Dispersal Data - Sheet1.csv")
summary(dispersalmap)
##      Factor       Seed_Type     Distance          Angle       
##  No wind:83   Butterfly:59   Min.   :  1.00   Min.   :  4.00  
##  Wind   :99   Dog      :56   1st Qu.: 34.25   1st Qu.: 50.25  
##               Fox      :67   Median : 80.00   Median :127.00  
##                              Mean   : 92.09   Mean   :128.59  
##                              3rd Qu.:134.00   3rd Qu.:190.50  
##                              Max.   :324.00   Max.   :350.00  
##      Season    
##  Min.   :1.00  
##  1st Qu.:2.00  
##  Median :3.00  
##  Mean   :3.06  
##  3rd Qu.:4.00  
##  Max.   :5.00
#X coordinate
dispersalmap$cartX <- dispersalmap$Distance * sin(dispersalmap$Angle)

#y coordinate
dispersalmap$cartY <- dispersalmap$Distance * cos(dispersalmap$Angle)

SeedMap <- ggplot(dispersalmap, aes(x=cartX, y=cartY))

SeedMap +
  geom_point() +
  geom_text(aes(label=Seed_Type), hjust=-0.5, vjust=-0.5)