-
Notifications
You must be signed in to change notification settings - Fork 13
/
script_2019-12-17.R
66 lines (53 loc) · 2.06 KB
/
script_2019-12-17.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
library(tidyverse)
library(geojsonio)
library(scales)
library(broom)
library(rgeos)
#### Data ####
dog_moves <- read_csv("data/data_2019-12-17.csv", col_types = "cdddl")
spdf <- geojson_read("ressources/us_states_hexgrid.geojson", what = "sp")
#### Tables ####
df_states <-
dog_moves %>%
filter(inUS) %>%
select(id = location, total) %>%
mutate(id = if_else(id == "Washington DC", "District of Columbia", id))
spdf@data <-
spdf@data %>%
mutate(google_name = gsub(" \\(United States\\)", "", google_name))
spdf_fortified <-
spdf %>%
tidy(region = "google_name") %>%
left_join(df_states, by = "id") %>%
rename(x = long, y = lat)
centers <-
spdf %>%
gCentroid(byid = TRUE) %>%
data.frame(id = spdf@data$iso3166_2)
df_title <-
data.frame(x = rep(mean(range(centers$x)), 2), y = c(54.5, 52.5),
id = c("Snapshot of adoptable dogs", "from Petfinder.com in the USA"))
#### Plot ####
ggplot() +
aes(x = x, y = y, label = id) +
geom_polygon(data = spdf_fortified,
aes(group = group, fill = total),
color = "white") +
geom_text(data = centers, family = "balonez fantasia br", size = 9) +
geom_text(data = df_title, size = 12, family = "balonez fantasia br") +
scale_fill_distiller(palette = "YlGn", direction = 1,
trans = log10_trans()) +
coord_map() +
geom_text(x = -100, y = 50, label = "title") +
labs(fill = "Count",
caption = "Source: The Pudding \n@_abichat for #TidyTuesday \n") +
theme_void() +
theme(plot.background = element_rect(fill = "#f1fef9", color = "#f1fef9"),
plot.title = element_text(hjust = 0.5),
plot.caption = element_text(family = "Arial Rounded MT Bold", size = 14),
legend.position = c(0.08, 0.5),
legend.direction = "vertical",
legend.title = element_text(family = "Arial Rounded MT Bold", size = 15),
legend.text = element_text(family = "Arial Rounded MT Bold", size = 12),
legend.key.size = unit(1, "cm"))
ggsave("plots/plot_2019-12-17.png", width = 29, height = 18.55, units = "cm", dpi = "retina")