This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hexed-toilets.R
131 lines (113 loc) · 5.17 KB
/
hexed-toilets.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
QUIET = FALSE
# ===------------------------------------------------------=== #
# Load additional packages and the font.
# ===------------------------------------------------------=== #
if (!QUIET) write("--- Loading required packages and fonts.", file = "")
library(methods)
if (!require("ggplot2", quietly = TRUE))
stop("The ggplot2 package is required to run this script.")
if (!require("grid", quietly = TRUE))
stop("The grid package is required to run this script.")
if (!require("gridExtra", quietly = TRUE))
stop("The gridExtra package is required to run this script.")
if (!require("dplyr", quietly = TRUE, warn.conflicts = FALSE))
stop("The dplyr package is required to run this script.")
suppressPackageStartupMessages(
if (!require("showtext", quietly = TRUE))
stop("The showtext package is required to run this script.")
)
# Envy Code R is a thin monospaced font, mostly used for programming. But it
# looks stylish and fits the graphic in this case. Minion Pro is the standard
# serif Adobe font.
font.add("envycoder", regular = "Envy Code R.ttf")
font.add("minion", regular = "MinionPro-Regular.otf")
# ===------------------------------------------------------=== #
# Read in the toilet data and construct the atribution string.
# ===------------------------------------------------------=== #
if (!QUIET) write("--- Loading required data.", file = "")
attribution <- paste(
scan(file = "AUTHOR.txt", what = character(), sep = "\n", quiet = TRUE),
"Data: Government of Australia (2014) and GeoHack (2014)",
"You may redistribute this graphic under the terms of the CC-by-SA license.",
sep = "\n"
)
toilets <-
read.csv(gzfile("toiletmap.csv.xz"),
header = TRUE, stringsAsFactors = FALSE) %>%
mutate(long = Longitude, lat = Latitude) %>%
select(ToiletID, long, lat)
poi <- data.frame(
name = c("Perth", "Sydney", "Melbourne", "Brisbane", "Adelaide",
"Alice\nSprings", "Darwin", "Hobart\n(Tasmania)"),
long = c(115.858889, 151.209444, 144.963056, 153.027778, 138.601, 133.87,
130.833333, 147.325),
lat = c(-31.952222, -33.859972, -37.813611, -27.467917, -34.929, -23.7,
-12.45, -42.880556),
vjust = c(1, 1, 0, 0.5, 0, 0.5, 0.5, 0),
hjust = 0.5
)
# ===------------------------------------------------------=== #
# Construct graphic
# ===------------------------------------------------------=== #
if (!QUIET) write("--- Constructing graphic.", file = "")
# We can set the background of the PNG using the "bg" parameter.
png(file = "australia-raw.png",
width = 7 * 300, height = 6.5 * 300, res = 300, bg = "gray90")
# The custom font is rendered by the `showtext` library.
showtext.begin()
# We create a graphic with a title and subtext using the arrangeGrob object.
graphic <- arrangeGrob(
ggplot(toilets, aes(x = long, y = lat)) +
stat_binhex(aes(fill = cut(..count..,
c(0, 5, 10, 50, 100, 500, 1000, Inf))),
colour = NA, alpha = 0.8) +
coord_equal() +
labs(fill = NULL) +
scale_fill_brewer(
palette = "OrRd",
labels = c("<5 ", "5-9 ", "10-49 ", "50-99 ",
"100-499 ", "500-999 ", "1000+ ")
) +
geom_text(aes(x = long, y = lat, label = name, vjust = vjust,
hjust = hjust), data = poi,
family = "envycoder", lineheight = 0.8, size = 4) +
theme(panel.background = element_rect(fill = "gray90", colour = NA),
plot.background = element_rect(fill = "gray90", colour = NA),
# Remove titles, ticks, gridlines, and borders.
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
# Move the legend somewhere nice.
legend.text = element_text(family = "envycoder", size = 8),
legend.position = c(0.35, 0.1),
# legend.position = c(0.27, 0.975),
legend.direction = "horizontal",
legend.background = element_rect(fill = NA, colour = NA),
legend.key = element_rect(fill = NA, colour = NA),
legend.key.size = unit(0.5, "line"),
# Set margins so that the graphic fills the whole space.
plot.margin = unit(c(0, 0, -0.5, -0.5), "line")
),
# We can create a two-line title using a textGrob.
main = textGrob(
label = c("Australia, In Public Toilets",
"(You'll Just Have to Hold It 'Till Perth)"),
hjust = c(0, 0), vjust = c(1, 1), x = unit(c(0.04, 0.04), "npc"),
y = unit(c(0, -1.4), "line"),
gp = gpar(fontsize = c(18, 10), fontfamily = "minion",
fontface = "plain", col = "black")),
# And similarly, a more complex subtext using another textGrob.
sub = textGrob(
x = unit(0.01, "npc"), y = unit(0.2, "npc"),
hjust = 0, vjust = 0,
gp = gpar(fontsize = 7, fontfamily = "minion", lineheight = 1.0,
col = "black"),
label = attribution)
)
# Actually write the graphic to the PNG file.
print(graphic)
showtext.end()
tmp <- dev.off()
if (!QUIET) write("--- Output written to <australia-raw.png>.", file = "")