Skip to content

Instantly share code, notes, and snippets.

@joebrew
Created September 8, 2017 13:07
Show Gist options
  • Save joebrew/b79e61d052b0b204fe2781d4e98671cf to your computer and use it in GitHub Desktop.
Save joebrew/b79e61d052b0b204fe2781d4e98671cf to your computer and use it in GitHub Desktop.
Make animation
# Define a function for adding zerio
add_zero <-
function (x, n)
{
x <- as.character(x)
adders <- n - nchar(x)
adders <- ifelse(adders < 0, 0, adders)
for (i in 1:length(x)) {
if (!is.na(x[i])) {
x[i] <- paste0(paste0(rep("0", adders[i]), collapse = ""),
x[i], collapse = "")
}
}
return(x)
}
dir.create('pngs')
dir.create('pngs2')
lmi <- length(max_indices)
for (i in c(seq(10, round(lmi), 10),
rep(max(lmi), 20))){
message(paste0(i, ' of ', lmi))
this_max <- max_indices[i]
sub_data <- hurricanes %>%
filter(max_index <= this_max)
# Get each line separetely for each id
sub_ids <- sort(unique(sub_data$id))
file_name <-
paste0(add_zero(this_max, 15), '.png')
png(filename = paste0('pngs2/', file_name),
width = 600,
height = 400)
this_year <- max(sub_data$year)
map('world',
xlim = c(-90, -10),
ylim = c(10, 40),
fill = TRUE,
col = adjustcolor('black', alpha.f = 1))
lines(irma,
col = 'purple',
lwd = 3)
text(x = -29,
y = 18,
label = 'Irma',
col = 'purple',
cex = 2)
title(main = this_year)
for (j in 1:length(sub_ids)){
sub_sub_data <- sub_data %>%
filter(id == sub_ids[j])
lines(sub_sub_data$lon,
sub_sub_data$lat,
col = adjustcolor('darkred', alpha.f = 0.8))
}
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment