# I first need to change the dates from days to days for later reference
# First I will make a table of the dates wich will include a count
deaths.tab = table(gun.deaths$date)
# Calculate Cumulative Dead
cum.deaths = deaths.tab
for (i in 1:(length(cum.deaths)-1)) cum.deaths[i+1] = cum.deaths[i]+deaths.tab[i+1]

plot(deaths.tab, main="Daily Total US Deaths by Gun", ylab="Death count")

->

# Number of days in our data (constantly updated every time we run the code
ndays = length(deaths.tab)

# This complicated bit of code will force the dates which are currently factor variables into string variables
gun.deaths$dates = t(data.frame(lapply(gun.deaths$date, as.character), stringsAsFactors=FALSE))

# Create an empty factor to be filled
gun.deaths$day = NA
# This command loops through all of the days and checks if each individual entry in the data from is from that day.
# If it is, then it assigns that day to the day entry.
for (i in 1:ndays) gun.deaths$day[gun.deaths$dates==names(deaths.tab)[i]] = i

# We will cut the data into different age categories

# Some individuals have ages missing. We will code these as category 0.
gun.deaths$age[is.na(gun.deaths$age)]
gun.deaths$age.cat = ""
gun.deaths$age.cat[gun.deaths$age<12 children="" p=""> gun.deaths$age.cat[gun.deaths$age>11 & gun.deaths$age<20 p="" teens=""> gun.deaths$age.cat[gun.deaths$age>=20 & gun.deaths$age<30 adults20="" p=""> gun.deaths$age.cat[gun.deaths$age>=30 & gun.deaths$age<40 adults30="" p=""> gun.deaths$age.cat[gun.deaths$age>=40 & gun.deaths$age<65 madults="" p=""> gun.deaths$age.cat[gun.deaths$age>65 & gun.deaths$age<999 nbsp="" oadults="" p="">
# Adjust the latitude and logitude variables to account for a rescaling of the graph later
# as well as some noise which will help identify when there are multiple deaths in the same city.
nll = length(gun.deaths$lng)
gun.deaths$x = ((gun.deaths$lng+125)/(60))*ndays+rnorm(nll)*.006
# For the graph that will be produced the 20 year olds have the highest likelihood of death.
# Thus they will provide the y upper limit.
ymax = ceiling(sum(gun.deaths$age.cat=="adults20")/50)*50
gun.deaths$y = ((gun.deaths$lat-24)/(27))*ymax+rnorm(nll)*.06

ani.options(ani.width=1200, ani.height=900) saveGIF(deaths.animation())# In order for the saveGIF function to work you need install Image Magic Display (http://www.imagemagick.org/script/index.php)# Map Updated Jan-31-2012