---
title: "RSSI Histogram"
output:
  html_document:
    theme: cerulean
    number_sections: true
---

```{r setup, include=FALSE, echo=FALSE, message=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  echo = FALSE, 
  message = FALSE, 
  warning = FALSE,
  comment = "#>"
)

rm(list=ls())
gc()


#library(checkpoint)
#checkpoint("2019-09-17", checkpointLocation = tempdir())

# CRAN libraries 
library(cowplot)
library(tidyverse)
library(magrittr)
library(RColorBrewer)

# Download and install from Github https://github.com/jmueller17/sociometrics
library(sociometrics)

# Download and install from Zenodo https://doi.org/10.5281/zenodo.3446071
library(gediismtrx)
data("gedii_sociodemo_fix")
data("gedii_rr")
data("gedii_interaction")

source("S3_utility.fun.R")

```



Check RSSI distribution

```{r fig.width=3, fig.height=4}

box <- gedii_interaction %>% 
  filter(!is.na(RSSI)) %>% 
  ggplot(aes(y=RSSI)) + 
  geom_boxplot(outlier.colour = "red") +
  scale_y_continuous(breaks=seq(-95,-20, by=5)) +
  labs(x="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"))

box
```



### Frequency distribution of BT detects (all teams)

All teams with outliers marked in red. 

```{r fig.width=7, fig.height=4}

g0 <- gedii_interaction %>%
  filter(!is.na(RSSI)) %>% 
  group_by(RSSI) %>% 
  summarize(detects = n()) %>% 
  mutate(outlier = if_else((RSSI > -58 | RSSI < -91), T, F)) %>% 
  ggplot(aes(x=RSSI, y=detects, fill=outlier)) +
    geom_histogram(stat="identity") + 
    #geom_smooth(se=F) +
    #scale_x_reverse() + 
    scale_x_continuous(limits=c(-95, -43), breaks=seq(-95,-43, by=5)) +
    scale_fill_manual(values=c("#000000", "#ff0000"), guide=F) + 
    labs(x="RSSI", y="Total detects") +
    theme_bw() + 
    theme(plot.margin=unit(c(1.2,1,0,1),"cm"))

g0
```


