forked from hadley/data-counties
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplore-al.r
54 lines (40 loc) · 1.73 KB
/
explore-al.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
al <- subset(raw, substr(id, 0, 2) == "2.")
# Weird error in data?
al <- transform(al, long = ifelse(long > 150, -long, long))
ggplot(al, aes(long, lat, group = group)) +
geom_polygon(fill = NA, colour = "grey50")
centres <- ddply(al, .(id), colwise(midrng, .(long, lat)))
al$hash <- paste(al$long, al$lat)
if (is.null(al$order)) al$order <- 1:nrow(al)
neighbours <- ddply(al, .(hash), nrow)
names(neighbours) <- c("hash", "count")
al <- merge(al, neighbours, by = "hash")
al <- al[order(al$order), ]
al <- ddply(al, .(group), transform,
change = diff(c(count[length(count)], count)) > 0
)
ggplot(al, aes(long, lat, group = group)) +
geom_polygon(fill = NA, colour = "grey50") +
geom_point(aes(colour = factor(count)), subset(al, change))
al <- ddply(al, .(group), rotate)
ddply(al, .(group), function(df) df$change[c(1, nrow(df))])
al <- ddply(al, .(id), thin_poly, .progress = "text")
al$hash <- NULL
ggplot(subset(al, tol > 0.1), aes(long, lat, group = group)) +
geom_polygon(fill = "grey80", colour = NA)
# All in one step: test
al <- subset(raw, substr(id, 0, 2) == "2.")
al <- transform(al, long = ifelse(long > 150, -long, long))
al <- add_tol(al)
thinned <- subset(al, tol > 0.1)
ggplot(thinned, aes(long, lat, group = group)) +
geom_polygon(fill = "grey80", colour = "white")
# Explore removing polygons with small areas.
source("poly.r")
areas <- ddply(thinned, .(group),
function(df) c(area = with(df, poly_area(long,lat))))
thinned <- merge(thinned, areas, by = "group")
ggplot(subset(thinned, area > 20), aes(long, lat, group = group)) +
geom_polygon(fill = "grey80", colour = "white")
ggplot(subset(thinned, area > 20), aes(long, lat, group = group)) +
geom_polygon(aes(fill = factor(id)), colour = "white")