Last active
April 21, 2017 14:31
-
-
Save pakdamie/457f309344f7074a9cf5b00f0f683731 to your computer and use it in GitHub Desktop.
LYME DISEASE INCIDENCE IN MICHIGAN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(dplyr) | |
library(viridis) | |
library(reshape2) | |
library(gganimate) | |
library(animation) | |
library(maps) | |
devtools::install_github("dgrtwo/gganimate") | |
###LYME DISEASE CASES- DATA | |
#stringsasFactors = False, I'm telling R to treat the county names as strings not | |
#a categorical variable | |
main.dat <- read.csv("lyme_county_0015.csv",stringsAsFactors = FALSE) | |
head(main.dat) | |
#I only want Michigan so I only subset and I get rid of the last row because | |
#that is not related | |
MI.main.dat <- subset(main.dat, STNAME=="Michigan") | |
MI.main.dat <- MI.main.dat[-nrow(MI.main.dat),] | |
#I want to rename the CTYNAME to county | |
colnames(MI.main.dat)[2] <- 'county' | |
unique(MI.main.dat$county) | |
###SPATIAL MAP DATA | |
map <- map_data("county", "michigan") | |
#I want to rename the subregion to county | |
colnames(map)[6] <- 'county' | |
unique(map$county) | |
###Eyeballing- I just check to make sure that the counties are the same | |
#between the columns so I can use the county column from the spatial map dataframe | |
#to replace the tick data's county column | |
cbind(MI.main.dat$county, unique(map$county)) | |
###Replacement | |
MI.main.dat$county <- unique(map$county) | |
###Merged dataframe | |
tick.dat.map <- left_join(map,MI.main.dat, by= c('county')) | |
###RESHAPING OUR DATA | |
tick.dat.map_sub<- tick.dat.map[,c(1,2,3,6,10:25)] | |
colnames(tick.dat.map_sub)[5:20]<- (seq(2000,2015,1)) | |
tick_melt<-melt(tick.dat.map_sub, id = c('long','lat','group','county')) | |
###PLOTTING | |
ggplot(tick_melt, aes(x = long, y= lat, group=group))+ | |
geom_polygon(aes(fill = value),color='black')+ | |
facet_wrap(~variable)+coord_map()+ | |
scale_fill_viridis(option='plasma')+ | |
theme_void() | |
###RUN THIS IF PROBLEMS OCCUR | |
#magickPath <- shortPathName("C:\\Program Files\\ImageMagick-7.0.5-Q16\\magick.exe") | |
#ani.options(convert=magickPath) | |
p<- ggplot(tick_melt, aes(x = long, y= lat, group=group,frame = variable))+ geom_polygon(aes(fill = value),color='black')+ | |
coord_map()+ scale_fill_viridis()+theme_void() | |
gganimate(p, "output.gif") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment