-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggplot2-map-example.r
More file actions
143 lines (127 loc) · 3.84 KB
/
ggplot2-map-example.r
File metadata and controls
143 lines (127 loc) · 3.84 KB
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
# load packages ----
library(sf)
library(dplyr)
library(httr)
library(ggplot2)
library(ggthemes)
library(ggview)
library(mapview)
# load spatial data ----
# load ky county polygons from KY Open Data arcgis api
# from here: https://opengisdata.ky.gov/datasets/kygeonet::kentucky-county-polygons/about
url <- parse_url("https://services3.arcgis.com/ghsX9CKghMvyYjBU/arcgis/rest/services/Ky_County_Polygons_WM/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson")
request <- build_url(url)
ky_county_poly <- st_read(request) |>
st_transform(6473) |>
janitor::clean_names()
ky_county_poly_select <- ky_county_poly |>
select(fips_id, name2, pop10)
# load county level data ----
# from here: https://www.countyhealthrankings.org/health-data/kentucky/data-and-resources
# Define the URL of the Excel file
url <- "https://www.countyhealthrankings.org/sites/default/files/media/document/2025%20County%20Health%20Rankings%20Kentucky%20Data%20-%20v3.xlsx" # Replace with your Office View URL
# Specify the destination file path
destfile <- "data/ky_county_health_rankings_2025.xlsx"
# Download the file
GET(url, write_disk(destfile, overwrite = TRUE))
# Read the Excel file
ky_county_health_rankings_2025 <- readxl::read_excel(destfile, sheet = "Additional Measure Data", skip = 1) |>
janitor::clean_names()
ky_county_health_rankings_2025_select <- ky_county_health_rankings_2025 |>
select(fips, life_expectancy) |>
mutate(
fips = as.integer(fips),
life_expectancy = round(life_expectancy, 2),
# create intervals using quantile breaks
life_expectancy_bin = cut(
life_expectancy,
breaks = quantile(
life_expectancy,
probs = seq(0, 1, length.out = 6),
na.rm = TRUE
),
include.lowest = TRUE
),
# create intervals based on pretty breaks
life_expectancy_pretty = cut(
life_expectancy,
breaks = classIntervals(life_expectancy, n = 5, style = "pretty")$brks,
digits = 2
)
)
# combine county data to spatial data ----
ky_county_poly_join <- left_join(
ky_county_poly_select,
ky_county_health_rankings_2025_select,
by = join_by(fips_id == fips)
)
# create map ----
# create data for labels
myLabels <- ky_county_poly_join |>
filter(
life_expectancy %in% head(sort(ky_county_poly_join$life_expectancy), 20) |
pop10 %in% tail(sort(ky_county_poly_join$pop10), 10)
)
myPlot <- ggplot(ky_county_poly_join) +
geom_sf(aes(fill = life_expectancy), color = "grey50", linewidth = 0.75) +
ggrepel::geom_label_repel(
data = myLabels,
aes(label = name2, geometry = geometry),
size = 2,
segment.color = "black",
stat = "sf_coordinates",
min.segment.length = 0
) +
scale_fill_viridis_c() +
theme_void() +
theme(
plot.margin = margin(0, 1, 0, 1, "cm"),
legend.position = "bottom"
) +
labs(
title = "Life Expectancy by County (Kentucky)",
fill = NULL
) +
canvas(
width = 8,
height = 4,
units = "in"
)
myPlot
save_ggplot(myPlot, file = "export/myPlot.png")
myPlotBinned <- ggplot(ky_county_poly_join) +
geom_sf(aes(fill = life_expectancy_pretty), color = "grey50", linewidth = 0.75) +
ggrepel::geom_label_repel(
data = myLabels,
aes(label = name2, geometry = geometry),
size = 2,
segment.color = "black",
stat = "sf_coordinates",
min.segment.length = 0
) +
scale_fill_brewer(
palette = "GnBu",
direction = -1,
guide = guide_legend(
keyheight = unit(3, units = "mm"),
keywidth = unit(12, units = "mm"),
label.position = "bottom",
nrow = 1
)
) +
theme_void() +
theme(
plot.margin = margin(0, 1, 0, 1, "cm"),
legend.position = "bottom"
) +
labs(
title = "Life Expectancy by County (Kentucky)",
fill = NULL
) +
canvas(
width = 8,
height = 4,
units = "in"
)
myPlotBinned
save_ggplot(myPlotBinned, file = "export/myPlotBinned.png")