I am working on creating a choropleth map in R with certain cities and certain providers plotted as points on top of the map. I'm stuck with getting the names of the providers to move off the map into the margins (either at the top, to the right, or at the bottom).
When keeping all the providers in one data frame, I cannot figure out how to move some of the labels to the top, to the left, and to the bottom. I can move them all to the top via:
Choropleth +
geom_polygon(data=map, aes(x=long, y=lat, group= group), alpha=0,
color="black", size=1) +
geom_point(data=provider, aes(x=Longitude, y=Latitude, group=County),
color='red',
alpha=4,
size=3) +
geom_point(data=city, aes(x=Longitude, y=Latitude, group=Name),
color='yellow',
alpha=2,
size=2.5,
shape=8) +
geom_text_repel(data=city, aes(x=Longitude, y=Latitude, group=Name, label=Name),
min.segment.length = unit(0, 'lines')) +
geom_label_repel(data=provider, aes(x=Longitude, y=Latitude, group=Name, label=Name),
min.segment.length = unit(0, 'lines'),
nudge_y = 40)
But it comes out looking like this with all the labels on top of each other:
Or all to the bottom:
Choropleth +
geom_polygon(data=map, aes(x=long, y=lat, group= group), alpha=0,
color="black", size=1) +
geom_point(data=provider, aes(x=Longitude, y=Latitude, group=County),
color='red',
alpha=4,
size=3) +
geom_point(data=city, aes(x=Longitude, y=Latitude, group=Name),
color='yellow',
alpha=2,
size=2.5,
shape=8) +
geom_text_repel(data=city, aes(x=Longitude, y=Latitude, group=Name, label=Name),
min.segment.length = unit(0, 'lines')) +
geom_label_repel(data=provider, aes(x=Longitude, y=Latitude, group=Name, label=Name),
min.segment.length = unit(0, 'lines'),
nudge_y = -40)
But it comes out looking like this with all the labels on top of each other:
Then, I tried separating out the providers into their own data frames and tried manually moving the positions, but I'm stuck moving left to right and some of the labels still overlap with each other.
Choropleth +
geom_polygon(data=map, aes(x=long, y=lat, group= group), alpha=0,
color="black", size=1) +
geom_point(data=provider, aes(x=Longitude, y=Latitude, group=County),
color='red',
alpha=4,
size=3) +
geom_point(data=city, aes(x=Longitude, y=Latitude, group=Name),
color='yellow',
alpha=2,
size=2.5,
shape=8) +
geom_text_repel(data=city, aes(x=Longitude, y=Latitude, group=Name, label=Name),
min.segment.length = unit(0, 'lines')) +
geom_label_repel(data=North, aes(x=Longitude, y=Latitude, group=Name, label=Name),
min.segment.length = unit(0, 'lines'),
nudge_y = 40) +
geom_label_repel(data=Central, aes(x=Longitude, y=Latitude, group=Name, label=Name),
min.segment.length = unit(0, 'lines'),
nudge_x = -40) +
geom_label_repel(data=South, aes(x=Longitude, y=Latitude, group=Name, label=Name),
min.segment.length = unit(0, 'lines'),
nudge_y = -40)
But the northern labels are overlapping and I cannot get the labels any more to the left. I tried adding xlim but that zooms out the graph and I want to keep it zoomed in like I have it. Here is what that code above looks like:
I'm really stuck with these labels. I still need to clean up the map so ignore the other parts.
I just need help figuring out how to get the labels how I want them.
The choropleth data comes from zip_choropleth + a dataframe that I got from the U.S. Census Bureau that include ZCTA's with total population 60+.
Here is a reproducible example.
library(maps)
library(tidyverse)
library(ggrepel)
ohiocounty_map <-
map_data("county") %>%
subset(region == "ohio") %>%
mutate(County = str_to_sentence(subregion)) %>%
group_by(County) %>%
filter(County %in% c("Butler", "Clermont", "Clinton", "Hamilton", "Warren"))
## make the name longer, so you can reproduce the problem.
Provider_Name <- sapply(letters[1:8], function(x) {
paste(rep(x, each = 20), collapse = "")
}, USE.NAMES = F)
## change to numeric
Latitude_P <- as.numeric(c("39.18268", "39.09723", "39.06838", "39.13517", "39.243835", "39.323032", "39.445957", "39.505478"))
Longitude_P <- as.numeric(c("-84.48057", "-84.64043", "-84.10078", "-84.612465", "-84.463478", "-84.504646", "-84.27858", "-84.739334"))
provider <- data.frame(Provider_Name, Latitude_P, Longitude_P)
ggplot() +
geom_polygon(data = ohiocounty_map, aes(x = long, y = lat, group = group), fill = NA, color = "black") +
geom_point(data = provider, aes(x = Longitude_P, y = Latitude_P), color = "red", size = 3) +
geom_label_repel(
data = provider, aes(x = Longitude_P, y = Latitude_P, label = Provider_Name),
nudge_y = 40
)
Created on 2022-07-13 by the reprex package (v2.0.1)