-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregions.r
150 lines (124 loc) · 5.08 KB
/
regions.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
library("maps")
library("RColorBrewer")
library("scales")
map_world <- map_data("world")
map_world <- map_world %>%
mutate(region = recode(region, `USA` = "United States")) %>%
mutate(region = recode(region, `UK` = "United Kingdom")) %>%
mutate(region = recode(region, `Ivory Coast` = "Cote d`Ivoire"))
f_countries <- f_countries %>%
mutate(country = recode(country, `Congo, Democratic Republic of` = "Democratic Republic of the Congo")) %>%
mutate(country = recode(country, `Congo, Republic of` = "Republic of Congo"))
world_choro <- left_join(map_world, f_countries, by = c("region" = "country"))
world_choro %>%
mutate(net_donations = net_donations / 1e9) %>%
ggplot(aes(long, lat)) +
geom_polygon(aes(group = group, fill = net_donations)) +
coord_quickmap() +
# scale_fill_viridis_b()
# scale_fill_continuous()
scale_fill_gradient2()
world_choro %>%
mutate(net_donations = net_donations / 1e9) %>%
ggplot(aes(long, lat)) +
geom_polygon(aes(group = group, fill = net_donations)) +
coord_quickmap() +
scale_fill_distiller(name = "Net Donations", palette = "RdYlBu", direction = 1, limits = c(-30, 30))
# histogram of net donations
f_countries %>%
ggplot(aes(x = net_donations / 1e9)) +
geom_histogram(binwidth = 1)
# histogram colored by the RdYlBu scale
f_countries %>%
ggplot(aes(x=net_donations / 1e9)) +
geom_histogram( aes(fill = ..x..), binwidth = 1) +
scale_fill_distiller(name = "Net Donations", palette = "RdYlBu", direction = 1, limits = c(-30, 30)) +
xlim(-30, 30) +
xlab("Net Donations (Billions, USD)") +
theme(legend.position = "bottom", legend.title = element_blank()) +
theme(legend.key.width=unit(50, "points"))
# gradient colors
world_choro %>%
mutate(net_donations = net_donations / 1e9) %>%
ggplot(aes(long, lat)) +
geom_polygon(aes(group = group, fill = net_donations)) +
coord_quickmap() +
scale_fill_gradient2(name = "Net Donations", low = "red", mid = "white", high = "blue")
f_countries %>%
ggplot(aes(x=net_donations / 1e9)) +
geom_histogram( aes(fill = ..x..), binwidth = 1) +
scale_fill_gradient2(name = "Net Donations", low = "red", high = "blue") +
xlab("Net Donations") +
xlim(-30, 30)
# Red - White - Blue
log_mid = log(1 - min(f_countries$net_donations/1e9))
f_countries %>%
ggplot(aes(x=log(net_donations/1e9 + 1 - min(net_donations/1e9)))) +
geom_histogram( aes(fill = ..x..), binwidth = .235) +
xlab("Log Transform of Net Donations") +
xlim(0, 2*log_mid) +
scale_fill_gradient2(low = "red", high = "blue", midpoint = log_mid) +
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.text = element_blank()) +
theme(legend.key.width=unit(90, "points"))
log_mid = log(10 - min(f_countries$net_donations/1e9))
min_net_donations = min(f_countries$net_donations/1e9)
world_choro %>%
mutate(log_net_donations = log(net_donations/1e9 + 10 - min_net_donations)) %>%
ggplot(aes(long, lat)) +
geom_polygon(aes(group = group, fill = log_net_donations)) +
coord_quickmap() +
scale_fill_gradient2(name = "Net Donations",
low = "red",
mid = "white",
high = "blue",
midpoint = log_mid) +
theme(legend.text = element_blank())
world_choro %>%
mutate(net_donations = net_donations / 1e9) %>%
ggplot(aes(long, lat)) +
geom_polygon(aes(group = group, fill = net_donations)) +
coord_quickmap() +
scale_fill_gradientn(name = "Net Donations",
colors = c("red", "white", "blue"),
breaks = c(-10, 0, 30),
limits = c(-30, 30))
max = max(world_choro$net_donations, na.rm = TRUE)
min = min(world_choro$net_donations, na.rm = TRUE)
world_choro %>%
mutate(metric = ifelse(net_donations > 0,
net_donations/max,
-net_donations/min)) %>%
ggplot(aes(long, lat)) +
geom_polygon(aes(group = group, fill = metric)) +
coord_map("azequidistant", orientation = c(0, 0, 0)) +
scale_fill_gradient2("Net Donations",
low = "#D73027",
mid = "#FFFFFF",
high = "#4575B4")
library(ggplot2)
library(ggalt)
library(ggthemes)
library(sf)
world_choro %>%
mutate(metric = ifelse(net_donations > 0,
net_donations/max,
-net_donations/min)) %>%
ggplot(aes(long, lat)) +
geom_polygon(aes(group = group, fill = metric)) +
coord_proj("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs") +
scale_fill_gradient2("Net Donations",
low = "#D73027",
mid = "#FFFFFF",
high = "#4575B4")
# states <- map_data("state")
# arrests <- USArrests
# names(arrests) <- tolower(names(arrests))
# arrests$region <- tolower(rownames(USArrests))
#
# choro <- merge(states, arrests, sort = FALSE, by = "region")
# choro <- choro[order(choro$order), ]
# ggplot(choro, aes(long, lat)) +
# geom_polygon(aes(group = group, fill = assault)) +
# coord_map("albers", at0 = 45.5, lat1 = 29.5)