[R] Use ggplot to create a state map with counties and county names as labels

Thomas MacFarland tomm@c @ending from nov@@edu
Fri Sep 21 20:17:10 CEST 2018


Everyone:

Using multiple resources I've been able to create a state (Kentucky) map that shows all 120 counties, with two selected counties highlighted in red.

Fine, except - I also want to show the names of the two selected counties, likely as labels.

Any ideas on how to achieve this aim would be greatly appreciated.

The code shows below.

Best wishes.

Tom

library(ggplot2)
library(ggthemes)
library(ggmap)
library(maps)
library(mapdata)

ls()
rm(list=ls(all=TRUE))
ls()


# U.S. States

states <- map_data("state")
head(states)
str(states)
ky_df <- subset(states, region == "kentucky")
head(ky_df)
str(states)

# U.S. Counties

counties <- map_data("county")
ky_county <- subset(counties, region == "kentucky")
head(ky_county)
str(ky_county)

ky_base <- ggplot2::ggplot(data = ky_df,
  mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = "aliceblue")
par(ask=TRUE); ky_base # Map of Kentucky with no counties, yet

par(ask=TRUE)
ky_base +
geom_polygon(data = ky_county, fill = NA, color = "black") +
geom_polygon(color = "black", fill = NA)  # Map of Kentucky with counties

# Select Pike County and Warren County as subregions
multiple_county_ky <- subset(ky_county, subregion=="pike" | subregion=="warren")

# Create a map with Pike County and Warren County in red
ky_base +
labs(title = "Kentucky Counties of Importance to the Study") +
geom_polygon(data = ky_county, fill = NA, color = "black") +
geom_polygon(color = "black", fill = NA) +
geom_polygon(data = multiple_county_ky, fill = "red", color = "white") +
theme_void()

# But how can I add the county name as a label to the two
# selected counties, Pike County and Warren County?


----------
Thomas W. MacFarland, Ed.D.
Senior Research Associate; Institutional Effectiveness and Associate Professor
Nova Southeastern University
Voice 954-262-5395 tommac using nova.edu<mailto:tommac using nova.edu>


	[[alternative HTML version deleted]]



More information about the R-help mailing list