-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.R
62 lines (48 loc) · 1.71 KB
/
animation.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
59
60
61
62
library(dplyr)
library(rgdal)
library(ggplot2)
library(ggthemes)
annexations <- readOGR("data/with_area/with_area.shp")
annexations@data$id <- rownames(annexations@data)
df <- fortify(annexations, region = "id")
df <- merge(df, annexations@data, by = "id")
areas <- data.frame(year = numeric(), area_sq_m = numeric(), new_area_sq_m = numeric())
df$addition = df$YEAR >= 2010
ggplot() +
geom_polygon(data = df, aes(x = long, y = lat, group = group, fill = addition)) +
coord_fixed(xlim = c(272000, 295000), ylim = c(3567000, 3595000)) +
annotate("text", x = 275000, y = 3568000, label = "2017") +
theme_map() +
guides(fill = FALSE) +
ggsave("animation/2017.png")
for (i in seq(1940, 2017, 10)) {
print(i)
tyler <- subset(df, YEAR < i)
tyler$addition <- tyler$YEAR >= i - 10
area <- tyler %>%
group_by(id) %>%
filter(row_number() == 1) %>%
ungroup() %>%
summarise(total = sum(AREA))
new_area <- tyler %>%
group_by(id) %>%
filter(row_number() == 1) %>%
ungroup() %>%
filter(addition == TRUE) %>%
summarise(total = sum(AREA))
areas[nrow(areas) + 1, ] <- c(i, area$total, new_area$total)
ggplot() +
geom_polygon(data = tyler, aes(x = long, y = lat, group = group, fill = addition)) +
coord_fixed(xlim = c(272000, 295000), ylim = c(3567000, 3595000)) +
annotate("text", x = 275000, y = 3568000, label = i) +
theme_map() +
guides(fill = FALSE) +
ggsave(paste0("animation/", i, ".png"))
}
areas[nrow(areas) + 1, ] <- c(2017, sum(annexations@data$AREA))
areas <- areas %>%
mutate(
area_sq_mi = area_sq_m * 0.0000003861022,
new_area_sq_mi = new_area_sq_m * 0.0000003861022
)
write.csv(areas, "results/areas.csv", row.names = FALSE)