knitr::opts_chunk$set(echo = TRUE)
library(knitr) library(kableExtra) library(tidyverse) library(leaflet) library(RColorBrewer) library(maptools) library(sf) library(rgeos) library(dplyr) library(RPostgreSQL) library(dbplyr) library(lubridate) library(units) library(data.table) library(ggplot2) library(here)
source('~/github/whalesafe4r/R/db.R') source('~/github/whalesafe4r/R/crawlers.R') source('~/github/whalesafe4r/R/readers.R') source('~/github/whalesafe4r/R/utils.R') source('~/github/whalesafe4r/R/seg_stats.R')
con=db_connect() ihs_data = dbGetQuery(con, "select * from ihs_data;") vsr_segs_ihs = merge_ihs_vsr()
ship_stats_2019 = ship_statistics(data= vsr_segs_ihs, date_start = '2019-01-01', date_end = '2019-12-31', tonnage = 300) operator_stats_2019 = operator_statistics(data= vsr_segs_ihs, date_start = '2019-01-01', date_end = '2019-12-31', tonnage = 300)
# 4160 mmsi total in IHS data length(unique(ihs_data$mmsi)) # 4094 mmsi's over 300 gt length(unique(ihs_data$mmsi[ihs_data$gt>=300])) # 1812 unique mmsi's in vsr_segments database table dbGetQuery(con, "select count(distinct mmsi) from vsr_segments;") # 897 unique mmsi's in vsr_segments database table in 2019 dbGetQuery(con, "select count(distinct mmsi) from vsr_segments where year = 2019;") # 1441 unique mmsi's (merged df) in vsr in 2018 & 2019 length(unique(vsr_segs_ihs$mmsi)) # 706 unique mmsi's in vsr in 2019 length(unique(vsr_segs_ihs$mmsi[vsr_segs_ihs$year==2019])) # 683 unique mmsi's in vsr in 2019 with gross tonnage >= 300 length(unique(vsr_segs_ihs$mmsi[vsr_segs_ihs$year==2019 & vsr_segs_ihs$gt>=300])) # DOUBLE CHECKIN' # 683 unique mmsi's in vsr in 2019 length(unique(ship_stats_2019$mmsi)) # 683 unique mmsi's in vsr in 2019 with gross tonnage >= 300 length(unique(ship_stats_2019$mmsi[ship_stats_2019$gt>=300])) # 549 unique mmsi's in vsr in 2019 with gross tonnage >= 300 and travelled >= 100 km length(unique(ship_stats_2019$mmsi[ship_stats_2019$gt>=300 & ship_stats_2019$`total distance (km)`>=100]))
# 1095 operators total in IHS data length(unique(ihs_data$operator)) # 1062 operators over 300 gt length(unique(ihs_data$operator[ihs_data$gt>=300])) # 467 unique operators in vsr in 2018 & 2019 length(unique(vsr_segs_ihs$operator)) # 267 unique operators in vsr in 2019 length(unique(vsr_segs_ihs$operator[vsr_segs_ihs$year==2019])) # 257 unique operators in vsr in 2019 with gross tonnage >= 300 length(unique(vsr_segs_ihs$operator[vsr_segs_ihs$year==2019 & vsr_segs_ihs$gt>=300])) # DOUBLE CHECKIN' # 257 unique operators in vsr in 2019 with gt>=300 length(unique(operator_stats_2019$operator)) # 257 unique operators in vsr in 2019 with vessels gt >= 300 length(unique(ship_stats_2019$operator[ship_stats_2019$gt>=300])) # 208 operators with over 100 km travelled total length(unique(operator_stats_2019$operator[operator_stats_2019$`total distance (km)`>=100]))
operator_stats_2019_100k=operator_stats_2019 %>% filter(operator_stats_2019$`total distance (km)`>=100) theme_set(theme_bw()) op_grade <- ggplot(data.frame(operator_stats_2019_100k), aes(x=grade)) + geom_bar() + theme(plot.title = element_text(hjust = 0.5)) + ggtitle("Operator VSR Cooperation Grade Distribution: 2019") + labs(caption="Source: Benioff Ocean Initiative", x="VSR Cooperation Grade", y="Count") + geom_text(stat='count', aes(label=..count..), vjust=1.5, color="white")+ # geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5),size=4, color = "white") + theme(plot.title = element_text(size = 14)) op_grade
ship_stats_2019_100k=ship_stats_2019 %>% filter(ship_stats_2019$`total distance (km)`>=100) # png("/Users/seangoral/Library/Mobile Documents/com~apple~CloudDocs/_Benioff_SRA_Limited/100kHighRes150dpi2.png", units="px", width=1200, height=800, res=150) ggplot(data.frame(ship_stats_2019_100k), aes(x=noaa_grade)) + geom_bar() + theme(plot.title = element_text(hjust = 0.5)) + theme(plot.subtitle = element_text(hjust = 0.5)) + ggtitle("Ship VSR Cooperation Grade Distribution: 2019", subtitle = "Modified NOAA Rubric") + labs(caption="Source: Benioff Ocean Initiative", x="VSR Cooperation Grade", y="Count") + geom_text(stat='count', aes(label=..count..), vjust=1.5, color="white") # dev.off()
t = operator_stats_2019 %>% kable(escape = F) %>% kable_styling(bootstrap_options = c("striped", "hover", "responsive")) t
summary_stats = data.table( nm_0_10=sum(ship_stats_2019$`distance (nautcal miles) 0-10 knots`), nm_10_12=sum(ship_stats_2019$`distance (nautcal miles) 10-12 knots`), nm_12_15=sum(ship_stats_2019$`distance (nautcal miles) 12-15 knots`), nm_over_15=sum(ship_stats_2019$`distance (nautcal miles) over 15 knots`)) summary_stats_percents = data.table( `% Travelled 0-10 kn` = (sum(ship_stats_2019_100k$`distance (nautcal miles) 0-10 knots`)/sum(ship_stats_2019_100k$`total distance (nautcal miles)`)*100), `% Travelled 10-12 kn`= sum(ship_stats_2019_100k$`distance (nautcal miles) 10-12 knots`)/sum(ship_stats_2019_100k$`total distance (nautcal miles)`)*100, `% Travelled 12-15 kn` = sum(ship_stats_2019_100k$`distance (nautcal miles) 12-15 knots`)/sum(ship_stats_2019_100k$`total distance (nautcal miles)`)*100, `% Travelled over 15 kn` = sum(ship_stats_2019_100k$`distance (nautcal miles) over 15 knots`)/sum(ship_stats_2019_100k$`total distance (nautcal miles)`)*100) sum_stats=melt(summary_stats_percents)
pie <- ggplot(sum_stats, aes(x="", y=value, fill=variable)) + geom_bar(stat="identity", width=1) + coord_polar("y", start=0) + geom_text(aes(label = paste0(round(value), "%")), position = position_stack(vjust = 0.5)) + scale_fill_manual(values=c("palegreen1", "skyblue1", "gold1" ,"coral2")) + labs(x = NULL, y = NULL, fill = NULL, title = "Summary of VSR cooperation for 2019") + theme_classic() + theme(axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), plot.title = element_text(hjust = 0.5, color = "#666666")) + theme(plot.title = element_text(hjust = -0.5)) + theme(plot.title = element_text(size = 16)) pie
ggplot(data.frame(operator_stats_2019_100k), aes(x=operator_stats_2019_100k$number_of_distinct_mmsi)) + geom_bar() + theme(plot.title = element_text(hjust = 0.5)) + ggtitle("Vessels per Operator: 2019") + labs(caption="Source: Benioff Ocean Initiative", x="Number of Vessels (MMSI)", y="Number of Operators") + geom_text(stat='count', aes(label=..count..), vjust=-0.18, color="black")+ # geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5),size=4, color = "white") + theme(plot.title = element_text(size = 16)) + scale_x_continuous(breaks = scales::pretty_breaks(n = 22)) + scale_y_continuous(breaks = scales::pretty_breaks(n = 8))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.