## Load the necessary libraries
require(maps)
## Loading required package: maps
## Warning: package 'maps' was built under R version 2.15.2
require(ggplot2)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 2.15.2
require(mapproj)
## Loading required package: mapproj
## Warning: package 'mapproj' was built under R version 2.15.2
require(maptools)
## Loading required package: maptools
## Warning: package 'maptools' was built under R version 2.15.2
## Loading required package: foreign
## Loading required package: sp
## Warning: package 'sp' was built under R version 2.15.2
## Loading required package: grid
## Loading required package: lattice
## Checking rgeos availability: FALSE Note: when rgeos is not available,
## polygon geometry computations in maptools depend on gpclib, which has a
## restricted licence. It is disabled by default; to enable gpclib, type
## gpclibPermit()
require(plyr)
## Loading required package: plyr
## Warning: package 'plyr' was built under R version 2.15.2
require(gtools)
## Loading required package: gtools
## Warning: package 'gtools' was built under R version 2.15.2
library(sp)
library(gpclib)
## General Polygon Clipper Library for R (version 1.5-1) Type 'class ?
## gpc.poly' for help
gpclibPermit()
## [1] TRUE
## Draw a map of Oregon Counties and save it into an object
ormap <- map("county", "oregon", plot = FALSE, fill = TRUE)
## Fortify map
ormap <- fortify(ormap)
## Load data and save as dataframe object
ue.df <- read.csv("ORUnemployment.csv", header = TRUE)
## Take note of the columns in the map dataframe and the dataframe with
## attribute data
names(ormap)
## [1] "long" "lat" "group" "order" "region" "subregion"
names(ue.df)
## [1] "County" "Year" "Labor.Force" "Employed" "Unemployed"
## [6] "UE.Rate"
# Make entries in the attribute database lowercase
ue.df$County <- tolower(ue.df$County)
## Change column 'County' to 'subregion' to make it mergeable with the map
## dataframe
names(ue.df) <- c("subregion", "Year", "Labor.Force", "Employed", "Unemployed",
"UE.Rate")
## Add a 'region' column to the attribute dataframe so that it can merge
## with attribute dataframe. Populate each cell with the region 'oregon.'
ue.df$region <- rep("oregon", 36)
## Merge dataframes
or.ue.map <- merge(ormap, ue.df, sort = FALSE, by = c("region", "subregion"))
# Save map in object and plot it
map <- ggplot(or.ue.map, aes(long, lat, group = group)) + geom_polygon(aes(fill = UE.Rate),
colour = "grey", size = 0.05)
map
map1 <- map + scale_fill_continuous(low = "blue", high = "red", limits = c(8,
15))
## Map with continuous variable broken into 'bins'
fill_factor <- quantcut(or.ue.map$UE.Rate, q = seq(0, 1, by = 0.2))
othermap <- ggplot(or.ue.map, aes(long, lat, group = group)) + geom_polygon(aes(fill = fill_factor),
colour = "grey", size = 0.05)
othermap
map2 <- othermap + scale_fill_brewer(guide = guide_legend(reverse = TRUE, title = "Rate of Unemployment"))
map2
map3 <- map2 + scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
labs(list(title = "Oregon Unemployment 2011", x = "", y = ""))
map3
## Create a center to plot county names
orcnames <- aggregate(cbind(long, lat) ~ subregion, data = ormap, FUN = mean)