# ==============================================================================
# * Libraries
# ==============================================================================
library(lubridate)
library(dplyr)
library(data.table)
library(ggplot2)
library(hrbrthemes)
library(cowplot)
library(raster)
library(sf)
library(ggsn)
extrafont::loadfonts()
source("R/00_fxns.R")
# Directory for saving plots and KDE information
plot_out_dir = file.path("results", "figs")
# ==============================================================================
# * Read in GPS data with flying behaviour + tagging location
# ==============================================================================
# read in the behavior-classified acc and GPS data
gps_forage = readRDS("data/gps_forage.RDS")
df = gps_forage$K5309 %>%
filter(time_cut %in% 3)
kilombero_wgs = data.frame(site = "kilombero",
lon = 36.98726,
lat = -7.662083)
df_sf = df %>%
st_as_sf(coords = c("utm.easting", "utm.northing"), crs = 32737)
df_sp = as(df_sf, 'Spatial')
map_extent = extent(df_sp) %>%
as('SpatialPolygons') %>%
st_as_sf %>%
st_set_crs(st_crs(df_sp)) %>%
st_buffer(100) %>%
st_transform(crs = 4326)
g = ggplot() +
geom_sf(data = map_extent, fill = NA, color = NA) +
geom_path(data = df, aes(x = location.long, y = location.lat),
size = 0.25, color = "#8C0C53") +
geom_point(data = df, aes(x = location.long, y = location.lat, color = "#8C0C53"),
size = 2.5, alpha = 1) +
geom_point(data = df[ df$activity %in% "NotFlying", ],
aes(x = location.long, y = location.lat, color = "#F2CB70"),
size = 2.5, alpha = 1)
g_forage = g +
geom_point(data = df[ df$forage_pts, ],
aes(x = location.long, y = location.lat, color = "black"),
size = 1, alpha = 1)
g_forage = g_forage +
geom_point(data = kilombero_wgs, aes(x = lon, y = lat, color = "roost"),
size = 4, alpha = 1) +
scale_colour_manual(name = "",
values =c('#8C0C53'='#8C0C53',
"#F2CB70" = "#F2CB70",
"black" = "black",
"roost" = "#2C7BB7"),
labels = c(
"Flying",
"Not Flying",
"Foraging area/\nFeeding roost",
"Colony location"))
g_forage = g_forage +
theme_ipsum(base_size = 14,
plot_title_size = 14,
grid = FALSE,
axis = FALSE, ticks = TRUE,
axis_text_size = 12,
axis_title_size = 14) +
theme(legend.position="top") +
scalebar(map_extent,
dist = 4,
dist_unit = "km",
transform = TRUE,
model = "WGS84",
location = "bottomright",
height = 0.01,
st.dist = 0.025,
st.size = 4,
box.fill = c("white", "black"),
box.color = "black",
border.size = 0.25) +
xlab("Longitude") +
ylab("Latitude")
# ggsave("results/figs/5309_day3_gps.png", g_forage, dpi = 600)
cowplot::save_plot("results/figs/5309_day3_gps.png",
g_forage, base_height = 7, dpi = 600)
cowplot::save_plot("results/figs/5309_day3_gps.pdf",
g_forage, base_height = 7, dpi = 600)
# ==============================================================================
# * gps acc
# ==============================================================================
plot_info = readRDS("data/acc-gps_plot-info_yz.RDS")
ID = "K5309"
acc_df = plot_info[[ ID ]]$acc_df
acc_plot = plot_info[[ ID ]]$acc_plot
day_info = plot_info[[ ID ]]$day_info
gps_acc_N = plot_info[[ ID ]]$gps_acc_N
# Max value of any axis reading
max_y = max(acc_plot$value + 50, na.rm = TRUE)
# Get x axis labels
x_lab = data.frame(n = c(day_info$start_n, day_info$end_n),
timestamp = c(day_info$start_ts, day_info$end_ts))
x_lab = x_lab[ order(x_lab$n), ]
x_lab$label = paste0(lubridate::month(x_lab$timestamp, label = TRUE),
lubridate::day(x_lab$timestamp),
"\n",
format(x_lab$timestamp, "%H:%M"))
tdiff = difftime(x_lab$timestamp, lag(x_lab$timestamp), units = "hours")
x_lab = x_lab[ tdiff >= 10 | is.na(tdiff), ]
# ==============================================================================
# Plot base/skeleton without acceleration axes
# ==============================================================================
p = ggplot() +
geom_rect(data = day_info,
aes(xmin = start_n, xmax = end_n,
ymin = 0, ymax = max_y,
text = "Day"),
fill = "#F2F2F2") +
scale_color_manual(values=c("#D55E00", "#009E73", "#0072B2"),
labels = c("X", "Y", "Z")) +
theme_ipsum(base_size = 12,
base_family = "Helvetica",
grid = FALSE,
axis_col = "#919191", axis = TRUE, ticks = TRUE) +
scale_x_continuous(breaks = x_lab$n,
labels = x_lab$label) +
ggtitle("") +
labs(color = "Acceleration axis") +
ylab("Acceleration\nreading") +
xlab("Time") +
theme(axis.title.y = element_text(angle = 0, hjust = 0, vjust = 0.5),
legend.position = "bottom")
static_p = p + geom_segment(data = gps_acc_N[ gps_acc_N$activity %in% "NotFlying"],
aes(x = n,
y = (max_y + 50),
xend = n,
yend = (max_y + 200),
text = paste0(activity, "\n", timestamp)),
color = "#F2CB70") +
geom_segment(data = gps_acc_N[ gps_acc_N$activity %in% "Flying"],
aes(x = n,
y = (max_y + 50),
xend = n,
yend = (max_y + 200),
text = paste0(activity, "\n", timestamp)),
color = "#8C0C53")
gps_l = data.frame(activity = c("GPS: Flying", "GPS: Not Flying"), n = 2) %>%
ggplot(aes(activity, fill = activity)) +
geom_bar() + scale_fill_manual(values=c("#8C0C53", "#F2CB70")) +
theme(legend.position = "top",
legend.justification='right',
legend.key.size = unit(0.2, "cm"),
legend.text=element_text(size = 10, family = "Helvetica")) +
labs(fill = "")
gps_l = get_legend(gps_l)
day_l = data.frame(activity = "Day time", n = 2) %>%
ggplot(aes(x = activity, fill = activity))+
geom_bar() + scale_fill_manual(values="#F2F2F2") +
theme(legend.position = "top",
legend.justification='left',
legend.key.size = unit(0.2, "cm"),
legend.key.width = unit(0.6, "cm"),
legend.text=element_text(size = 12, family = "Helvetica")) +
labs(fill = "")
day_l = get_legend(day_l)
final_p = static_p +
annotation_custom(grob = day_l, ymin = max_y + 350) +
annotation_custom(grob = gps_l, ymin = max_y + 350)
cowplot::save_plot("results/figs/5309_day3_acc_base.pdf",
final_p, base_height = 6, dpi = 600)
# ==============================================================================
# Plot with acceleration axes
# ==============================================================================
static_p = static_p +
geom_line(data = acc_plot,
aes(x = n, y = value,
group = axis,
color = axis,
text = paste0(activity, "\n", timestamp)),
alpha = 0.6)
final_p = static_p +
annotation_custom(grob = day_l, ymin = max_y + 350) +
annotation_custom(grob = gps_l, ymin = max_y + 350)
cowplot::save_plot("results/figs/5309_day3_acc.png",
final_p, base_height = 6, dpi = 600)
After combining the above plots in a design software:
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] grid stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] gdtools_0.2.1 ggsn_0.5.0 sf_0.8-0 raster_2.9-5
## [5] sp_1.3-1 cowplot_1.0.0 hrbrthemes_0.6.0 ggplot2_3.3.0.9000
## [9] data.table_1.12.8 dplyr_0.8.4 lubridate_1.7.4
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.3 svglite_1.2.2 lattice_0.20-38 tidyr_1.0.2
## [5] png_0.1-7 class_7.3-15 assertthat_0.2.1 digest_0.6.23
## [9] R6_2.4.1 plyr_1.8.5 evaluate_0.14 e1071_1.7-2
## [13] httr_1.4.1 pillar_1.4.3 RgoogleMaps_1.4.3 rlang_0.4.4
## [17] extrafontdb_1.0 rmarkdown_1.17.1 labeling_0.3 rgdal_1.4-4
## [21] extrafont_0.17 stringr_1.4.0 foreign_0.8-71 munsell_0.5.0
## [25] compiler_3.6.0 xfun_0.12 pkgconfig_2.0.3 systemfonts_0.1.1
## [29] htmltools_0.4.0 tidyselect_1.0.0 tibble_2.1.3 codetools_0.2-16
## [33] crayon_1.3.4 withr_2.1.2 bitops_1.0-6 Rttf2pt1_1.3.7
## [37] gtable_0.3.0 lifecycle_0.1.0 DBI_1.0.0 magrittr_1.5
## [41] units_0.6-3 scales_1.1.0 KernSmooth_2.23-15 stringi_1.4.5
## [45] farver_2.0.3 vctrs_0.2.2 rjson_0.2.20 tools_3.6.0
## [49] ggmap_3.0.0 glue_1.3.1 purrr_0.3.3 jpeg_0.1-8.1
## [53] yaml_2.2.1 colorspace_1.4-1 maptools_0.9-5 classInt_0.4-2
## [57] knitr_1.26