---
title: "2019_07_10_Analysis"
output: html_document
editor_options: 
  chunk_output_type: console
---


```{r}
source("helpFunctions.R")
```


```{r}
df <-  read_csv2("../data/paula_paper1.csv")
```


```{r}
df <- df %>% 
  mutate(cont = 1)

df <- df %>% 
  mutate(Month = factor(Month, levels = c("February", "April", "August", "October"))) %>% 
  group_by(Month, Track, Level, Species, Gender, Blood, Night, Point) %>% 
  summarise(cont = sum(cont))


dfSpread <- spread(df, key = "Species", value = "cont")
```


```{r}
#rpelace NA by 0
dfSpread <-  dfSpread %>% 
  replace(.,is.na(.),0)
```


```{r}
#Compute species number
Species <-  dfSpread[,c(8:85)]
Species <- Species %>% 
  mutate_all(funs(ifelse(.>=1,1,0)))

dfSpread$nSpecies <- rowSums(Species)
dfSpread$speciesAb <-  rowSums(dfSpread[,c(8:85)])
#Create species number

dfSpread %>% 
  select(nSpecies,speciesAb) %>% 
  head()
```


```{r}
dataFig2 <- dfSpread %>%  
  group_by(Month,Track,Level) %>% 
  summarise(meanAbund = mean(speciesAb),
            meanRich = mean(nSpecies))

write_csv(dataFig2, path = "../results/FreqABundRiq.csv")

#Figura Postosi
dataFig2 %>% 
  filter(Track == "Potosi") %>% 
  ggplot() +
  geom_bar(aes(x = Month, y = meanAbund), stat = "identity") +
  geom_point(aes(x = Month, y = meanRich * 6), size = 8) +
  labs(x = NULL) +
  scale_y_continuous(name = "Mean abundance", 
                     sec.axis = sec_axis(~./6, "Species number mean")) +
  facet_wrap(~Level) +
  theme_bw(base_size = 48) +
  ggsave("../results/Figure2A.tiff", device = "tiff", width = 32, height = 20)

#Figura Outro lugar
dataFig2 %>% 
  filter(Track == "Santa Maria") %>% 
  ggplot() +
  geom_bar(aes(x = Month, y = meanAbund), stat = "identity") +
  geom_point(aes(x = Month, y = meanRich * 7), size = 8) +
  labs(x = NULL) +
  scale_y_continuous(name = "Mean abundance", 
                     sec.axis = sec_axis(~./7
                                         , "Species number mean")) +
  facet_wrap(~Level) +
  theme_bw(base_size = 48) +
  ggsave("../results/Figure2B.tiff", device = "tiff", width = 32, height = 20)
```


```{r}
dfEngorged <-  df %>%
  group_by(Track, Level, Species, Blood) %>% 
  summarise(cont = sum(cont))
```


```{r}
potosiGround <- mostCommSpe(data = dfEngorged, track = "Potosi", level = "Ground")
potosiCanopy <- mostCommSpe(data = dfEngorged, track = "Potosi", level = "Canopy")
smGround <- mostCommSpe(data = dfEngorged, track = "Santa Maria", level = "Ground")
smCanopy <- mostCommSpe(data = dfEngorged, track = "Santa Maria", level = "Canopy")
```


```{r}
dataPG <- freqCompute(data = selectSpecies(
  data = dfEngorged,
  speciesList = potosiGround,
  track = "Potosi",
  level = "Ground"
))

dataPC <- freqCompute(data = selectSpecies(
  data = dfEngorged,
  speciesList = potosiCanopy,
  track = "Potosi",
  level = "Canopy"
))

dataSMG <- freqCompute(data = selectSpecies(
  data = dfEngorged,
  speciesList = smGround,
  track = "Santa Maria",
  level = "Ground"
))

dataSMC <- freqCompute(data = selectSpecies(
  data = dfEngorged,
  speciesList = smCanopy,
  track = "Santa Maria",
  level = "Canopy"
))
```


```{r}
makePlot(data = dataPC) %>% 
  ggsave(filename = "../results/Fig3PotosiCanopy.tiff", device = "tiff", width = 32, height = 20)

makePlot(data = dataPG) %>% 
  ggsave(filename = "../results/Fig3PotosiGround.tiff", device = "tiff", width = 32, height = 20)

makePlot(data = dataSMC) %>% 
  ggsave(filename = "../results/Fig3SMCanopy.tiff", device = "tiff", width = 32, height = 20)

makePlot(data = dataSMG) %>% 
  ggsave(filename = "../results/Fig3SMGround.tiff", device = "tiff", width = 32, height = 20)
```


```{r}
freqdata <- rbind(dataPC,dataPG,dataSMC,dataSMG)
write_csv(freqdata, path = "../results/FreqResult.csv")
```

