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

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Dr. Thomas W. MacFarland
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 [hidden email]<mailto:[hidden email]>


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Use ggplot to create a state map with counties and county names as labels

JohnDee
On Fri, 21 Sep 2018 18:17:10 +0000
Thomas MacFarland <[hidden email]> 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.
>
> Tom
You might want to try the ggplot2 group at Google Groups instead.  They
are specifically ggplot2.

JWDougherty

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.