-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathchorMapFinal.R
58 lines (44 loc) · 1.82 KB
/
chorMapFinal.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## chorMap will create A choropleth Map of total Emissions by FIPS area for the given year
## setwd("C:/Users/rr046302/Documents/Bill's Stuff/Coursera/Exploratory Data Analysis/Project 2")
chorMap <- function(NEIyear){
if(!exists("NEI")) {
print("Reading NEI file")
setwd("C:/Users/rr046302/Documents/Bill's Stuff/Coursera/Exploratory Data Analysis/Project 2")
NEI <- readRDS("summarySCC_PM25.rds")
}
if(!exists("SCC")) {
print("Reading SCC file")
setwd("C:/Users/rr046302/Documents/Bill's Stuff/Coursera/Exploratory Data Analysis/Project 2")
SCC <- readRDS("Source_Classification_Code.rds")
}
require(maps)
require(ggmap)
require(doBy)
## set the color bands
colors = c("#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77",
"#980043")
## summarize by fips and year
fipssum <- summaryBy(Emissions ~fips + year, data = NEI, keep.names = TRUE, FUN = sum)
## cut Emissions into breaks
fipssum$colorBuckets <- cut(fipssum$Emissions, breaks=c(quantile(fipssum$Emissions, probs = seq(0, 1, by = 0.2))),
labels=c(1,2,3,4,5), include.lowest=TRUE)
## subset based on the NEIyear called in the function
year <- subset (fipssum, year == NEIyear)
colorsmatched <- year$colorBuckets
title <- paste("NEI PM2.5 Emissions by county ", NEIyear)
windows()
map("county")
map("county", col = colors[colorsmatched], fill = TRUE, resolution = 0,
lty = 0, projection = "polyconic")
title(title)
leg.txt <- c("bottom", "2nd", "3rd", "4th", "5th quintile")
legend("topright", leg.txt, horiz = TRUE, fill = colors)
if(!exists("NEI")) {
print("Assigning NEI to Global Environment")
assign("NEI", NEI, envir=globalenv())
}
if(!exists("SCC")) {
print("Assigning SCC to Global Environment")
assign("SCC", SCC, envir=globalenv())
}
}