Skip to content

Instantly share code, notes, and snippets.

@theosanderson
Created December 14, 2020 21:56
Show Gist options
  • Save theosanderson/e27c4b81c7bf8749169e03ec1d49b705 to your computer and use it in GitHub Desktop.
Save theosanderson/e27c4b81c7bf8749169e03ec1d49b705 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(gganimate)
library(lubridate)
data <- read_csv("D:/cases_anim/cog_metadata_microreact_public.csv")
kent <- data %>% filter(latitude==.7334,longitude==51.2245)
data$sample_date = ymd(data$sample_date)
data$diff_days = data$sample_date - ymd("2020-09-01")
data_sub <- data %>% filter(sample_date>"2020-09-01")
data_sub <- data_sub %>% filter(sample_date<"2020-11-20")
jitter_amount = 0.5
data_sub <- mutate(data_sub,x= longitude+runif(nrow(data_sub),-jitter_amount,jitter_amount))
data_sub <- mutate(data_sub,y= latitude+runif(nrow(data_sub),-jitter_amount,jitter_amount))
dt_enter = ymd("2020-01-03")- ymd("2020-01-01")
dt_exit = ymd("2020-01-03")- ymd("2020-01-01")
dt_persist = ymd("2020-01-02")- ymd("2020-01-01")
x<- ggplot(data_sub,aes(x=x,y=y,color=n501y=="Y"))+geom_point(stroke=0,size=0.01) +coord_fixed(xlim=c(-7.5,2.5),ylim=c(50,58))+
transition_events(range=c(ymd("2020-09-04",ymd("2020-11-13"))),start=sample_date,end=sample_date+dt_persist,enter_length=dt_enter,exit_length=dt_exit) +theme_classic()+scale_color_manual(values=c("gray","red"))+enter_fade()+exit_fade()+ labs(title = "Date: {frame_time}") +labs(x="Longitude",y="Latitude",color="N501Y")
animate(x, height = 700, width =800,end_pause=35)
anim_save("spread.gif")
anim_save("spread.mp4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment