Skip to content

Instantly share code, notes, and snippets.

@kissmygritts
Created January 31, 2018 22:43
Show Gist options
  • Save kissmygritts/e48402b0ddb257f651d02d7020f26abd to your computer and use it in GitHub Desktop.
Save kissmygritts/e48402b0ddb257f651d02d7020f26abd to your computer and use it in GitHub Desktop.
Code to reproduce figures for TWS presentation
dat <- readr::read_csv('sandbox/test-muld.csv')
df <- dat %>%
filter(ndowid == 915)
mig_df <- df %>%
filter(timestamp >= mdy('1/1/2014') &
timestamp <= mdy('6/30/2014'))
mig_df <- mig_df %>%
mutate(nsd = calc_nsd(x, y, normalize = T)) %>%
filter(nsd < .25)
## trajectory plot for presentation
ggplot(mig_df, aes(x = x, y = y, color = as.numeric(timestamp))) +
geom_point(alpha = .3) +
viridis::scale_color_viridis(option = 'viridis') +
theme_void() +
coord_equal() +
theme( legend.position = 'none')
## focused trajectory plot
mig_df %>%
filter(timestamp < mdy('3/12/2014')) %>%
ggplot(aes(x = x, y = y, color = as.numeric(timestamp))) +
geom_point(alpha = .3) +
viridis::scale_color_viridis() +
theme_void() +
coord_equal() +
theme( legend.position = 'none')
## NSD plot for presentation
ggplot(mig_df, aes(x = timestamp, y = nsd, color = timestamp)) +
geom_point(alpha = .4) +
viridis::scale_color_viridis() +
theme_minimal() +
theme( legend.position = 'none' )
# create lch for beginning ----------------------------------------------------
lch_start <- locoh(dat = mig_df %>% filter(nsd < .125), c('x', 'y', 'timestamp', 'ndowid'))
## plot start
ggplot() +
geom_path(data = lch_start$mcp_df, aes(x = x, y = y, color = id, group = id), alpha = .25) +
viridis::scale_color_viridis() +
coord_equal() +
theme_void() +
theme(
legend.position = 'none'
)
# create lch for end of migration ---------------------------------------------
end_df <- mig_df %>%
arrange(desc(timestamp)) %>%
mutate(nsd = calc_nsd(x, y, T)) %>%
filter(nsd < .125)
ggplot(end_df, aes(x = timestamp, y = nsd, color = timestamp)) +
geom_point(alpha = .4) +
viridis::scale_color_viridis() +
theme_minimal() +
theme( legend.position = 'none' )
lch_end <- locoh(dat = end_df, c('x', 'y', 'timestamp', 'ndowid'))
# plot end
ggplot() +
geom_path(data = lch_end$mcp_df, aes(x = x, y = y, color = id, group = id), alpha = .25) +
viridis::scale_color_viridis() +
coord_equal() +
theme_void() +
theme(
legend.position = 'none'
)
# plot start and end together ----
## cool figure bro
ggplot() +
geom_point(data = mig_df, aes(x = x, y = y), color = 'lightgrey', alpha = .5) +
geom_path(data = lch_start$mcp_df, aes(x = x, y = y, group = id), alpha = .25, color = ggthemes::gdocs_pal()(2)[1]) +
geom_path(data = lch_end$mcp_df, aes(x = x, y = y, group = id), alpha = .25, color = ggthemes::gdocs_pal()(2)[2]) +
coord_equal() +
theme_void() +
theme(
legend.position = 'none'
)
# filter lch start ----
hullset_start <- filter_hullset(lch_start)
## plot it
ggplot() +
geom_path(data = hullset_start$hullset, aes(x = x, y = y, group = id), alpha = .25, color = ggthemes::gdocs_pal()(2)[1]) +
viridis::scale_color_viridis() +
coord_equal() +
theme_void() +
theme(
legend.position = 'none'
)
## find start time
start_date <- hullset_start$meta %>%
extract2('start_date') %>%
max()
start_date
# filter lch end ----
hullset_end <- filter_hullset(lch_end)
## plot it
ggplot() +
geom_path(data = hullset_end$hullset, aes(x = x, y = y, group = id), alpha = .25, color = ggthemes::gdocs_pal()(2)[2]) +
viridis::scale_color_viridis() +
coord_equal() +
theme_void() +
theme(
legend.position = 'none'
)
## find end date
end_date <- hullset_end$meta %>%
extract2('start_date') %>%
min()
end_date
# label migration ----
mig_df$migratory <- mig_df$timestamp >= start_date & mig_df$timestamp <= end_date
## plotit - trajectory
ggplot(mig_df, aes(x = x, y = y, color = migratory)) +
geom_point(alpha = .3) +
ggthemes::scale_color_gdocs(list(title = 'Migratory')) +
theme_void() +
coord_equal()
## plotit - nsd
ggplot(mig_df, aes(x = timestamp, y = nsd, color = migratory)) +
geom_point(alpha = .3) +
ggthemes::scale_color_gdocs(list(title = 'Migratory')) +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment