-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_functions.R
More file actions
127 lines (112 loc) · 4.17 KB
/
map_functions.R
File metadata and controls
127 lines (112 loc) · 4.17 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# lload libraries
library(rgdal)
library(rgeos)
library(ggplot2)
library(gridExtra)
library(mapproj)
# function for adding the standard dual map plotting theme elements
getStandardTheme <- function(){
theme(
#plot.title = element_text(size=24, face="bold"),
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
#legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())
}
# returns a shaded district map element
getShadedDistrictMap <- function(x, shade=1){
ggplot(x, aes(long,lat,group=DISTRICT, fill=govper)) +
geom_polygon() +
geom_path(color="white") +
coord_equal() +
getShadeTheme(shade) +
getStandardTheme()
}
# returns a shaded county map element
getShadedCountyMap <- function(x, shade=1){
ggplot(x, aes(long,lat,group=COUNTY, fill=govper)) +
geom_polygon() +
geom_path(color="white") +
coord_equal() +
getShadeTheme(shade) +
getStandardTheme()
}
getShadeTheme <- function(x=1){
if(x==1){
# red to white to blue
scale_fill_gradientn(colours = c("red","white","blue"), limits=c(0,1), space="rgb")
} else if(x==2){
# red to blue through purple
scale_fill_continuous(low="red", high="blue", limits=c(0,1), space="rgb")
} else if(x==3){
# white to blue (dfl density)
scale_fill_continuous(low="white", high="blue", limits=c(0,1), space="rgb")
} else if(x==4){
# red to white (rep density)
scale_fill_continuous(low="red", high="white", limits=c(0,1), space="rgb")
}
}
# returns a shaded district map element with district names
getNumericDistrictMap <- function(x, textSize=6, bounds=c(0,1)){
cnames <- aggregate(cbind(long, lat, govper) ~ DISTRICT, data=x,
FUN=function(y)mean(range(y)))
ggplot(x, aes(long,lat)) +
geom_polygon(data=x, aes(group=DISTRICT, fill=govper), colour='black') +
geom_text(data=cnames, aes(long, lat, label = DISTRICT), size=textSize) +
coord_equal() +
scale_fill_continuous(low="red", high="blue", limits=bounds, space="rgb") +
getStandardTheme()
}
# returns a shaded county map element with county names
getNumericCountyMap <- function(x, textSize=6, bounds=c(0,1)){
cnames <- aggregate(cbind(long, lat, NAME) ~ COUNTY, data=x,
FUN=function(y)mean(range(y)))
ggplot(x, aes(long,lat)) +
geom_polygon(data=x, aes(group=COUNTY, fill=govper), colour='black') +
geom_text(data=cnames, aes(long, lat, label = NAME), size=textSize) +
coord_equal() +
scale_fill_continuous(low="red", high="blue", limits=bounds, space="rgb") +
getStandardTheme()
}
# combines map and data for shaded mapping
prepareDataForMap <- function(map, data, county=FALSE){
df <- fortify(map)
df <- merge(df,map@data,by.x="id",by.y="row.names")
if(county==TRUE){
df$COUNTY <- as.numeric(df$COUNTY)
data$district <- as.numeric(data$district)
merge(df,data, by.x="COUNTY", by.y="district", all=TRUE)
} else {
merge(df,data, by.x="DISTRICT", by.y="district", all=TRUE)
}
}
# creates a dual plot element and/or png
makeDualPlot <- function(x, y, title="Map and Cartogram", subtitle="", makePNG=FALSE){
if(makePNG){
png(file="mn_maps.png",width=1000,height=500,units="px")
}
grid.arrange(x, y, ncol=2,
main=textGrob(title, gp=gpar(cex=2.5), just="top"),
sub=textGrob(subtitle, gp=gpar(cex=1), vjust=-1))
if(makePNG){
dev.off(which = dev.cur())
}
}
### misc stuff
## plot maps
#library(maps)
#library(maptools)
#rb.col <- colorRamp(c("#FF0000","#0000FF"))
#getHEX <- function(x){
# rgb(rb.col(x)[1],rb.col(x)[2],rb.col(x)[3], maxColorValue=255)
#}
#d$color <- sapply(d$dflper,getHEX)
#mn <- map("county", "minnesota", col=d$color, fill=TRUE)