update ggrepel labels

This commit is contained in:
2026-01-02 18:49:41 -05:00
parent 8a0b00dce5
commit 14ab179021
+45 -14
View File
@@ -467,6 +467,8 @@ for(lf in unique_lf_ids) {
#| fig-align: "left" #| fig-align: "left"
for(lf in unique_lf_ids) { for(lf in unique_lf_ids) {
lf_split <- split_full_lf_id(lf)
growth <- get_normalized_metric_growth(storm, lf) %>% growth <- get_normalized_metric_growth(storm, lf) %>%
mutate( mutate(
population_opacity = rescale( population_opacity = rescale(
@@ -518,7 +520,6 @@ for(lf in unique_lf_ids) {
base_growth_map <- ggplot() + base_growth_map <- ggplot() +
geom_sf(data = world, fill = "#e5e5e5", color = "#999999", size = 0.3) + geom_sf(data = world, fill = "#e5e5e5", color = "#999999", size = 0.3) +
geom_sf(data = states, fill = NA, color = "#cccccc", size = 0.2) + geom_sf(data = states, fill = NA, color = "#cccccc", size = 0.2) +
geom_sf_label(data = growth_sf, aes(label = growth_sf$name)) +
coord_sf( coord_sf(
xlim = c(lon_range[1] - lon_padding, lon_range[2] + lon_padding), xlim = c(lon_range[1] - lon_padding, lon_range[2] + lon_padding),
ylim = c(lat_range[1] - lat_padding, lat_range[2] + lat_padding), ylim = c(lat_range[1] - lat_padding, lat_range[2] + lat_padding),
@@ -531,24 +532,29 @@ for(lf in unique_lf_ids) {
theme_minimal() + theme_minimal() +
plot_theme plot_theme
set.seed(2004)
h_growth_map <- base_growth_map + h_growth_map <- base_growth_map +
geom_sf(data = growth_sf, fill = "blue", color = "#e5e5e5", size = 0.5, alpha = growth_sf$housing_opacity) + geom_sf(data = growth_sf, fill = "blue", color = "#e5e5e5", size = 0.5, alpha = growth_sf$housing_opacity) +
geom_sf_label(data = growth_sf, aes(label = growth_sf$name)) + ggrepel::geom_label_repel(
data = growth_sf,
aes(label = name, geometry = geom_wkt),
stat = "sf_coordinates"
) +
labs( labs(
title = paste(lf, "Housing Growth Map By County"), title = paste(lf, "Housing Growth Map By County"),
) + ) +
coord_sf( coord_sf(
xlim = c(lon_range[1] - lon_padding, lon_range[2] + lon_padding), xlim = c(lon_range[1] - lon_padding, lon_range[2] + lon_padding),
ylim = c(lat_range[1] - lat_padding, lat_range[2] + lat_padding), ylim = c(lat_range[1] - lat_padding, lat_range[2] + lat_padding),
expand = FALSE expand = FALSE
) )
latest_opacity <- growth %>% latest_opacity <- growth %>%
filter(year == max(year)) %>% filter(year == max(year)) %>%
select(county_fips, name, housing_opacity, population_opacity) select(name, housing_opacity, population_opacity)
growth_colored <- growth %>% growth_colored <- growth %>%
left_join(latest_opacity, by = c("county_fips", "name"), suffix = c("", "_latest")) left_join(latest_opacity, by = "name", suffix = c("", "_latest"))
housing_colors <- latest_opacity %>% housing_colors <- latest_opacity %>%
distinct(name, housing_opacity) %>% distinct(name, housing_opacity) %>%
@@ -565,15 +571,25 @@ for(lf in unique_lf_ids) {
h_plot <- ggplot(growth_colored, aes(x = year, y = normalized_housing, h_plot <- ggplot(growth_colored, aes(x = year, y = normalized_housing,
color = name, color = name,
group = county_fips)) + group = name)) +
geom_line() + geom_line() +
scale_color_manual(values = housing_color_values) + scale_color_manual(values = housing_color_values) +
geom_text_repel( geom_text_repel(
data = growth_colored %>% filter(year == max(year)), data = growth_colored %>% filter(year == max(year)),
aes(label = name, x = year + 1), aes(label = name),
direction = "y", direction = "y",
hjust = 0, hjust = 0,
size = 3 size = 3,
color = "black",
segment.size = .5,
segment.alpha = .5,
segment.linetype = "dotted",
box.padding = .4,
segment.curvature = -0.1,
segment.ncp = 3,
segment.angle = 20,
min.segment.length = 0,
xlim = c(max(growth_colored$year), NA)
) + ) +
coord_cartesian(clip = "off") + coord_cartesian(clip = "off") +
guides(color = "none") + guides(color = "none") +
@@ -589,16 +605,21 @@ for(lf in unique_lf_ids) {
print(h_growth_map) print(h_growth_map)
print(h_plot) print(h_plot)
set.seed(2004)
p_growth_map <- base_growth_map + p_growth_map <- base_growth_map +
geom_sf(data = growth_sf, fill = "red", color = "#e5e5e5", size = 0.5, alpha = growth_sf$population_opacity) + geom_sf(data = growth_sf, fill = "red", color = "#e5e5e5", size = 0.5, alpha = growth_sf$population_opacity) +
geom_sf_label(data = growth_sf, aes(label = growth_sf$name)) + ggrepel::geom_label_repel(
data = growth_sf,
aes(label = name, geometry = geom_wkt),
stat = "sf_coordinates"
) +
labs( labs(
title = paste(lf, "Population Growth Map By County"), title = paste(lf, "Population Growth Map By County"),
) + ) +
coord_sf( coord_sf(
xlim = c(lon_range[1] - lon_padding, lon_range[2] + lon_padding), xlim = c(lon_range[1] - lon_padding, lon_range[2] + lon_padding),
ylim = c(lat_range[1] - lat_padding, lat_range[2] + lat_padding), ylim = c(lat_range[1] - lat_padding, lat_range[2] + lat_padding),
expand = FALSE expand = FALSE
) )
population_colors <- latest_opacity %>% population_colors <- latest_opacity %>%
@@ -616,15 +637,25 @@ for(lf in unique_lf_ids) {
p_plot <- ggplot(growth_colored, aes(x = year, y = normalized_population, p_plot <- ggplot(growth_colored, aes(x = year, y = normalized_population,
color = name, color = name,
group = county_fips)) + group = name)) +
geom_line() + geom_line() +
scale_color_manual(values = population_color_values) + scale_color_manual(values = population_color_values) +
geom_text_repel( geom_text_repel(
data = growth_colored %>% filter(year == max(year)), data = growth_colored %>% filter(year == max(year)),
aes(label = name, x = year + 1), aes(label = name),
direction = "y", direction = "y",
hjust = 0, hjust = 0,
size = 3 size = 3,
color = "black",
segment.size = .5,
segment.alpha = .5,
segment.linetype = "dotted",
box.padding = .4,
segment.curvature = -0.1,
segment.ncp = 3,
segment.angle = 20,
min.segment.length = 0,
xlim = c(max(growth_colored$year), NA)
) + ) +
coord_cartesian(clip = "off") + coord_cartesian(clip = "off") +
guides(color = "none") + guides(color = "none") +