-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_mapping.Rmd
105 lines (96 loc) · 4.49 KB
/
04_mapping.Rmd
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
---
title: "mapping smart cities"
author: "cm"
date: "2024-01-10"
output: html_document
---
```{r}
options(scipen=16)
library(tidyverse)
```
```{r}
df <- read_csv("data/smart_cities20240110.csv")
```
# how prevalent smart cities, towns, and villages are in the US?
```{r}
# total number of smart cities
sum(df$smart)
```
```{r}
# total number of people in smart cities
df %>% filter(smart == 1) %>% summarise(total_pop=sum(POP20))
```
```{r}
# summarize by state
df %>% group_by(State) %>% summarise(muni_count=n(), smart_count=sum(smart), smart_den=smart_count/muni_count*100) %>% arrange(smart_count)
```
```{r}
# summarize by population
df <- df %>% mutate(pop_size=case_when(POP20>=1000000 ~ 4, POP20<1000000&POP20>=100000 ~3, POP20<100000&POP20>= 10000 ~2, POP20<10000 ~1))
df %>% group_by(pop_size) %>% summarise(muni_count=n(), smart_count=sum(smart), smart_den=round(smart_count/muni_count*100,1))
```
```{r}
map_df <- df %>% filter(State!="AK") %>% filter(State!="HI") %>% arrange(smart) # to make smart cities pop up in map
```
```{r}
#mycolor <- c("#596174","#d32d41" )
#mycolor <- c("#1c4e80","#d32d41" )
mycolor <- c("#596174","#ea6a47" )
states <- map_data("state")
```
```{r}
#ggplot() +
# theme_void() +
# geom_polygon(data = states, aes(x=long, y = lat, group = group), linewidth=0.4 ,color ="white",fill = 'gray90')
```
```{r}
ggplot() +
geom_polygon(data = states, aes(x=long, y = lat, group = group), linewidth=0.4 ,color ="white",fill = 'gray95') +
geom_point(data = map_df %>% filter(smart==1), aes(x = INTPTLON, y = INTPTLAT, stroke =0, color=as.factor(smart)), size=2) +
theme_void() +
xlab('') + ylab('') +
scale_color_manual(name="", values = "salmon")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.position = c(0.2, 0.15))+
guides(color="none", fill="none")
```
```{r}
ggplot() +
geom_polygon(data = states, aes(x=long, y = lat, group = group), linewidth=0.4 ,color ="white",fill = 'gray95') +
geom_point(data = map_df, aes(x = INTPTLON, y = INTPTLAT, stroke =0, color=as.factor(smart)), size=0.8, alpha = 0.7) +
geom_path(data = states, aes(x = long, y = lat, group = group), color = "white", size = 0.2) +
geom_point(data = map_df %>% filter(smart==1), aes(x = INTPTLON, y = INTPTLAT, stroke =0, color=as.factor(smart)), size=0.8) +
theme_void() +
xlab('') + ylab('') +
scale_color_manual(name="", labels=c("1"="Smart cities", "0"="Non-smart cities"), values = mycolor)+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.position = c(0.2, 0.15))+
guides(color=guide_legend(reverse=T, override.aes = list(size = 5)))
#ggsave("figure/map.png")
```
```{r}
ggplot() +
geom_polygon(data = states, aes(x=long, y = lat, group = group), linewidth=0.2, color ="white", fill = 'gray90') +
geom_point(data = map_df, aes(x = INTPTLON, y = INTPTLAT, stroke=0.6, color=as.factor(smart),size=as.factor(pop_size)), alpha = 0.4) +
geom_path(data = states, aes(x = long, y = lat, group = group), color = "gray80", size = 0.1) +
theme_void() +
xlab('') + ylab('') +
scale_size_ordinal(name=" Population", range=c(0.5,7),
labels=c("4"="1 million or more" ,"3"= "100,000 to 999,999", "2"="10,000 to 99,999", "1"="Less than 10,000"), )+
scale_color_manual(name=" ", labels=c("1"="Smart city", "0"="Non-smart city"), values = mycolor)+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.position = c(0.98 , 0.24))+
theme(plot.margin = unit(c(1,2,1,1), "cm"))+
guides(size = guide_legend(reverse=T, override.aes = list(shape=1)), color=guide_legend(reverse=T, override.aes = list(size = 5)))
```
```{r}
mycolor2 <- c("grey75","#ea6a47" )
ggplot() +
geom_polygon(data = states, aes(x=long, y = lat, group = group), linewidth=0.4 ,color ="grey",fill = 'white') +
geom_point(data = map_df, aes(x = INTPTLON, y = INTPTLAT, stroke =0, color=as.factor(smart)), size=0.6, alpha = 0.7) +
geom_path(data = states, aes(x = long, y = lat, group = group), color = "white", size = 0.3) +
geom_point(data = map_df %>% filter(smart==1), aes(x = INTPTLON, y = INTPTLAT, stroke =0, color=as.factor(smart)), size=1) +
theme_void() +
xlab('') + ylab('') +
scale_color_manual(name="", labels=c("1"="Smart cities", "0"="Non-smart cities"), values = mycolor2)+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.position = c(0.2, 0.15))+
guides(color=guide_legend(reverse=T, override.aes = list(size = 5)))
#ggsave("figure/map.pdf", dpi=500)
```