Loading...
Use
Create
Develop
FAQ
Troubleshooting
History: Profile_r_test11_animation
View page
Source of version: 2
(current)
! 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")} # We need to download data with Quandl to save it to a vcData.rda. # 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") ##dim(vcData) ##head(vcData, 2) ##tail(vcData, 2) # There are 51 rows and 53 columns in the dataset. Data is for 51 years # (2010-12-31 through 1960-12-31). 53 columns include a column for Year, each state and # the District of Columbia, and the average for the country. Let's now do four things. # 1. Create a column denoting the year # 2. Drop the column involving date (the existing 'Year' column) # 3. Drop the column involving the average for the country # 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 #Instead of looking at annual data, let's focus on different decades (2001-2010 #[rows 1-10], 1991-2000 [rows 11-20], 1981-1990 [rows 21-30], 1971-1980 [rows #31-40], and 1961-1970 [rows 41-50]). Let us compute the mean violent crime #rates for each state for each decade. Since 1960 does not fit in to our plans, #let us drop that row. vcData = vcData[-51, ] # Removing the row for 1960, the 51st row decades = data.frame(Decade2001_2010 = colMeans(vcData[1:10, ], na.rm = TRUE), Decade1991_2000 = colMeans(vcData[11:20, ], na.rm = TRUE), Decade1981_1990 = colMeans(vcData[21:30, ], na.rm = TRUE), Decade1971_1980 = colMeans(vcData[31:40, ], na.rm = TRUE), Decade1961_1970 = colMeans(vcData[41: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. ##dim(decades) ##head(decades, 2) ##tail(decades, 2) # Choropleths of different decades using ggplot2 and animation # ============================================================= # The first order of business was to get a US map. A typical map file generated # from the maps package does not include Alaska and Hawaii. Scouring the web for # someone who might've addressed this issue took me to this post of Kristopher # Kapphahn ( http://loloflargenumbers.com/blog/#.Up5D6sSkpS4 ). # The post was terrific and provided the complete code for generating # such a map. This code, which is in a separate file for those interested in it # ( https://github.com/patilv/choroplethanimation/tree/master/AllUSShapeFile ), # was used to generated a file all_us.rda. We use this file below for the map. # A preliminary choropleth for the 2001-2010 decade # ------------------------------------------------- # You can fetch the data from github (359Kb) # 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" ## For some reason this message is shown as of 2014 March when attempting to download the package with the method commented out below: ## "WARNING: certificate common name `www.github.com' doesn't match requested host name `raw.githubusercontent.com'." ## "To connect to raw.githubusercontent.com insecurely, use `--no-check-certificate'." # downloader(ur_l_all_us, file_all_us, "auto", quiet = TRUE, mode = "w", # cacheOK = TRUE ) ## Therefore, we will use wget directly with that param: system(paste("wget", ur_l_all_us, "-c -O", file_all_us, "--no-check-certificate", sep=" ")) load("all_us.rda") uslessdc = all_us[all_us$STATE_NAME != "District of Columbia", ] uslessdc$STATE_NAME = factor(uslessdc$STATE_NAME) uslessdc$CrimeRate = decades[, 1][match(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(paste("Violent Crime Rate in", names(decades[1]))) + xlab("") + ylab("") {RR} {RR(cacheby="pagename", echo="TRUE", ln="1", caption="R Code - 2nd part", loadandsave="1", wikisyntax="0")} # Tweaking Data and Animating Choropleths # --------------------------------------- # To bring in more clarity, let us classify states into 3 different groups based # on their violent crime rates for a decade and color code them differently. Top # 1/3 in crime rates (high, denoted by number 3) are most dangerous and middle # 1/3 (medium, denoted by number 2) are more dangerous than the bottom 1/3 (low, # denoted by number 1). # Create a new dataframe with crime rate data in decades classified into 3 # levels of crime rate for States for a decade. # 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] < quantile[1], "1", ifelse(decades[, i] < quantile[2], "2", "3")))) } ##head(decadespct, 2) save(decadespct, file = "decadespct.rda") } # 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[, i][match(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(decadespct[i]))) + 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} {iframe name="myChart" width="850" height="500" align="middle" frameborder="0" marginheight="0" marginwidth="0" scrolling="auto" src="temp/cache/{{domainslash_if_multitiki}}R_{{page}}/decadeplots.html"}
Menu
Featured profiles
Profiles in Wizard
Profiles Todos
Handlers
Profiles Tester
Admin
of this site
Current Profiles
Tiki version
21.x
24.x
26.x
27.x
28.x
Deprecated
25.x
state
Not fully functional yet
alpha
beta
release
type
Available in the Profiles Wizard (12+)
Featured profiles
Full profile (out of the box & ready to go)
Learning profile (to show off feature)
Long tail
Mini-profile (can be included in other)
Profile-snippet (optional but needs another "parent" profile)
Security
Tests
Latest Changes
Test_all_tracker_field_types_profile
Scheduler_Presets_20
Tracker_as_Calendar_09
Tracker_as_Calendar_10
Hide Fixed Top Nav Bar on Scroll 19
Easy_GeoBlog
test_profile_change
Profiles_in_Wizard
Random_header_images_14
Hide Fixed Top Nav Bar on Scroll 19
How to Create Profiles
Test_All_Plugins
JonnyBs_Luxury_Tiki_Setup
Collaborative_Multilingual_Terminology
Profile_Test_all_Modules_Test_all_Modules_page
...more
Like almost all *.tiki.org sites, you can log in with your login from
https://tiki.org
(register over there)
Search
Find
Most Popular Tags
admin
agenda
alias
antibot
antibot captcha
app
archive
articles
banning
batch
blogs
calculations
calendars
cart
categories
cluster
codemirror
comments
computation
contact us
datachannel
debug
debug console
dropdown with other
error messages
features
file galleries
forums
geo
geocms
geolocation
group homepages
group watches
header
i18n
images
item link
items list
jquery
languages
location
map
maps
maths
menu
menupage
multilingual
ol3
ol5
openlayers
paypal
plugin
plugin alias
plugin datachannel
plugin fade
plugin tabs
plugin trackerlist
pretty trackers
print
project management
r
r project
realnames
rss
static
static text
statistics
stats
structures
syntax highlighter
tablesorter
template
trackers
user watches
visualization
watches
webservice
wiki
wiki argument variables
wiki structures
Tiki Newsletter
Subscribe to the Tiki newsletter.
Don't miss major announcements and other news!