#Análise exploratória preps

rm(list=ls()) ## Removendo as variáveis
gc()

packages<-c('ggplot2','reshape','gdata','lubridate', 'dplyr','scales','readODS')

package.check <- lapply(packages, FUN = function(x) {
  if (!require(x, character.only = TRUE)) {
    install.packages(x, dependencies = TRUE)
    library(x, character.only = TRUE)
  }
})
load('output_data/preps_aggh_NE.Rda')


dh<-dh%>%dplyr::filter(vel>=2, lat<=10)
range(dh$date)
range(dh$lat)
unique(dh$modalidade)
length(unique(dh$cod_embarcacao))

#Plots exploratórios
mod<-dh%>%group_by(modalidade)%>%summarise(n_vessels=n_distinct(cod_embarcacao),
                                           n_hours=n()/1000)
mod$modalidade<-as.factor(mod$modalidade)
mod$modalidade<-reorder(mod$modalidade,mod$n_hours)
mod<-as.data.frame(mod)
mod.m<-melt(mod,by='modalidade')
write_ods(mod,path='~/github/ei_turtles/output_data/preps_modalidade.ods')
ggplot(mod.m,aes(x=modalidade,y=value))+geom_bar(stat='identity')+
  facet_wrap(~variable,scales = 'free_x')+
  theme_bw()+xlab(NULL)+ylab(NULL)+
  coord_flip()+
  theme(text = element_text(size=8, family='Times'))
ggsave('~/github/ei_turtles/figures/preps_modalidade.jpg',width=15, height = 12, units = 'cm',dpi=150)




ggplot(dh,aes(x=lat))+geom_density()+
  facet_wrap(~modalidade,scales='free_x')+
  theme_bw()+
  coord_flip()+
  theme(text = element_text(size=10, family='Times'))+
  xlab('Latitude')+ylab(NULL)
ggsave('figures/modalidade_lat.jpg',width=14, height = 10, units = 'cm', dpi=300)

