Thomas MacFarland
2018-Sep-21 18:17 UTC
[R] Use ggplot to create a state map with counties and county names as labels
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 at nova.edu<mailto:tommac at nova.edu> [[alternative HTML version deleted]]
John
2018-Sep-21 21:19 UTC
[R] Use ggplot to create a state map with counties and county names as labels
On Fri, 21 Sep 2018 18:17:10 +0000 Thomas MacFarland <tommac at nova.edu> wrote:> 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. > > TomYou might want to try the ggplot2 group at Google Groups instead. They are specifically ggplot2. JWDougherty