Chargement...
 
Imprimer

r_demo11_animation

Displaying a ggplot2 map with Animation

Derived from http://www.analyticsandvisualization.com/2014/01/animated-choropleths-using-animation.html

{RR(cacheby="pagename", echo="TRUE", ln="1", caption="R Code - 1st part", loadandsave="1", wikisyntax="0")}

  1. We need to download data with Quandl to save it to a vcData.rda.
  2. If you already have the required data in that file, don't run this code again any more.

if (!file.exists('vcData.rda')) {
# Check if Quandl needs to be installed
if(!require(Quandl)){ install.packages("Quandl", repos="http://ftp.heanet.ie/mirrors/cran.r-project.org/") }
require(Quandl)
# Not required but removes a warning message--- Quandl.auth('yourauthcode')
# Quandl.search('violent crime')
vcData = Quandl("FBI_UCR/USCRIME_TYPE_VIOLENTCRIMERATE")
save(vcData,file='vcData.rda')
}
load("vcData.rda")

    1. dim(vcData)
    2. head(vcData, 2)
    3. tail(vcData, 2)

  1. There are 51 rows and 53 columns in the dataset. Data is for 51 years
  2. (2010-12-31 through 1960-12-31). 53 columns include a column for Year, each state and
  3. the District of Columbia, and the average for the country. Let's now do four things.
  4. 1. Create a column denoting the year
  5. 2. Drop the column involving date (the existing 'Year' column)
  6. 3. Drop the column involving the average for the country
  7. 4. Drop the column for District of Columbia, which is not a State.

vcData$Yearonly = 2010:1960 # Creating a new column with Year
vcData = vcData, -1 # Dropping the existing 'Year' column
vcData = vcData, -52 # Dropping the column with average for the country
vcData = vcData, -9 # Dropping the column for Washington DC, the 9th column

  1. Instead of looking at annual data, let's focus on different decades (2001-2010
  2. rows 1-10, 1991-2000 rows 11-20, 1981-1990 rows 21-30, 1971-1980 rows
  3. 31-40, and 1961-1970 rows 41-50). Let us compute the mean violent crime
  4. rates for each state for each decade. Since 1960 does not fit in to our plans,
  5. let us drop that row.

vcData = vcData-51, # Removing the row for 1960, the 51st row
decades = data.frame(Decade2001_2010 = colMeans(vcData1:10, , na.rm = TRUE),
Decade1991_2000 = colMeans(vcData11:20, , na.rm = TRUE), Decade1981_1990 = colMeans(vcData21:30,
, na.rm = TRUE), Decade1971_1980 = colMeans(vcData31:40, , na.rm = TRUE),
Decade1961_1970 = colMeans(vcData41:50, , na.rm = TRUE))
decades$State = row.names(decades) # We needed a column for State and the row names provided that
decades = decades-51, # Remove the row for Yearonly, which is irrelevant.

    1. dim(decades)
    2. head(decades, 2)
    3. tail(decades, 2)

  1. Choropleths of different decades using ggplot2 and animation
  2. =============
  3. The first order of business was to get a US map. A typical map file generated
  4. from the maps package does not include Alaska and Hawaii. Scouring the web for
  5. someone who might've addressed this issue took me to this post of Kristopher
  6. Kapphahn ( http://loloflargenumbers.com/blog/#.Up5D6sSkpS4 ).
  7. The post was terrific and provided the complete code for generating
  8. such a map. This code, which is in a separate file for those interested in it
  9. ( https://github.com/patilv/choroplethanimation/tree/master/AllUSShapeFile ),
  10. was used to generated a file all_us.rda. We use this file below for the map.

  1. A preliminary choropleth for the 2001-2010 decade
  2. -----------------
  3. You can fetch the data from github (359Kb)
  4. Since data is in a url with https, we'll need to use the 'downloader' package

if(!require(downloader)){ install.packages("downloader", repos="http://ftp.heanet.ie/mirrors/cran.r-project.org/") }
require(downloader)
ur_l_all_us = "https://github.com/patilv/choroplethanimation/raw/master/AllUSShapeFile/all_us.rda"
file_all_us = "all_us.rda"

    1. For some reason this message is shown as of 2014 March when attempting to download the package with the method commented out below:
    2. "WARNING: certificate common name `www.github.com' doesn't match requested host name `raw.githubusercontent.com'."
    3. "To connect to raw.githubusercontent.com insecurely, use `--no-check-certificate'."
  1. downloader(ur_l_all_us, file_all_us, "auto", quiet = TRUE, mode = "w",
  2. cacheOK = TRUE )
    1. Therefore, we will use wget directly with that param:

system"wget", ur_l_all_us, "-c -O", file_all_us, "--no-check-certificate", sep=" "?
load("all_us.rda")
uslessdc = all_usall_us$STATE_NAME != "District of Columbia",
uslessdc$STATE_NAME = factor(uslessdc$STATE_NAME)
uslessdc$CrimeRate = decades, 1match(uslessdc$STATE_NAME, decades$State) # bring value of decade to map data
if(!require(ggplot2)){ install.packages("ggplot2", repos="http://ftp.heanet.ie/mirrors/cran.r-project.org/") }
require(ggplot2)
ggplot(data = uslessdc, aes(x = x_proj, y = y_proj, group = DRAWSEQ, fill = CrimeRate)) +
geom_polygon(color = "black") + ggtitle"Violent Crime Rate in", names(decades11" class="wiki wikinew paste">?) +
xlab("") + ylab("")
{RR}

{RR(cacheby="pagename", echo="TRUE", ln="1", caption="R Code - 2nd part", loadandsave="1", wikisyntax="0")}

  1. Tweaking Data and Animating Choropleths
  2. ---------------
  3. To bring in more clarity, let us classify states into 3 different groups based
  4. on their violent crime rates for a decade and color code them differently. Top
  5. 1/3 in crime rates (high, denoted by number 3) are most dangerous and middle
  6. 1/3 (medium, denoted by number 2) are more dangerous than the bottom 1/3 (low,
  7. denoted by number 1).

  1. Create a new dataframe with crime rate data in decades classified into 3
  2. levels of crime rate for States for a decade.
  3. But make all this processing only once; if file already created, don't do it again :-)

if (!file.exists("decadespct.rda")) {
decadespct = decades
for (i in 1:5) {
quantile = quantile(decades, i, c(1/3, 2/3))
decadespct, i = with(decades, factor(ifelse(decades, i < quantile1,
"1", ifelse(decades, i < quantile2, "2", "3"))))
}
##head(decadespct, 2)
save(decadespct, file = "decadespct.rda")
}

  1. On to the animation.

if(!require(animation)){ install.packages("animation", repos="http://ftp.heanet.ie/mirrors/cran.r-project.org/") }
require(animation)
require(ggplot2)
saveHTML({
for (i in 1:5) {
uslessdc$CrimeRate = decadespct, imatch(uslessdc$STATE_NAME, decadespct$State) # bring value of decade to map data
mycols = c("#4daf4a", "#fc8d62", "red") # Setting a color palette
ggchoropleth = ggplot(data = uslessdc, aes(x = x_proj, y = y_proj, group = DRAWSEQ,
fill = CrimeRate)) + geom_polygon(color = "black") + ggtitle(paste("Violent Crime Rate in",
names(decadespcti))) + xlab("") + ylab("") + scale_fill_manual(values = mycols,
labels = c("Low", "Medium", "High")) + theme(legend.position = "top")
print(ggchoropleth)
}
}, img.name = "decadeplots", imgdir = "decadeplots", htmlfile = "decadeplots.html",
outdir = getwd(), autobrowse = FALSE, ani.height = 400, ani.width = 600,
verbose = FALSE, autoplay = TRUE, title = "Violent Crime Rates")
{RR}

Erreur Plugiciel en attente d'approbation

Ce plugiciel a été ajouté/modifié récemment. Son exécution ne sera pas possible avant que l'éditeur de site ne valide ces paramètres.