Reputation: 1
I am attempting to add a layer with state abbreviations to this map:
US Map-- user locations represented by heat plot; no state name abbreviations
I have a list of users from a suvery whose self-reported location I mapped to determine the most popular states. I would like to add a "layer" to this map with state abbreviations.
This is an example of the dataset I'm using. I have a column with the state abbreviations, I just don't know how to layer them on top of the map. Btw I am using library(usmap).
state | stateabbr | n | fips |
---|---|---|---|
Alabama | AL | 1 | 01 |
Arkansas | AR | 1 | 05 |
California | CA | 32 | 06 |
Florida | FL | 19 | 12 |
Lastly, the code for the map:
usmap3 <- plot_usmap(data = statecounts, regions = "state", values = "n") +
scale_fill_gradientn(name = "Count", colours = myPalette(100)) +
labs(title = "Frequency of Unique Users in the United States",
caption = "",
fill = "Count")
usmap3
Upvotes: 0
Views: 1749
Reputation: 124013
One simple option would be to use the labels
argument of plot_usmap
which will automatically add labels with state abbreviations for all states.
In case that you want only some states to be labelled then you could first get the coordinates of the state centroids which are available via usmapdata::centroid_labels("states")
and join these coordinates to your dataset. Afterwards you could add the state abbreviations to your map via geom_text
like so:
library(usmap)
library(ggplot2)
# Get centroids
centroid_labels <- usmapdata::centroid_labels("states")
# Join centroids to data
state_labels <- merge(statecounts, centroid_labels, by = "fips")
plot_usmap(data = statecounts, regions = "state", values = "n") +
geom_text(data = state_labels, aes(
x = x, y = y,
label = stateabbr,
), color = "white") +
labs(title = "Frequency of Unique Users in the United States",
caption = "",
fill = "Count")
DATA
statecounts <- data.frame(
stringsAsFactors = FALSE,
state = c("Alabama", "Arkansas", "California", "Florida"),
stateabbr = c("AL", "AR", "CA", "FL"),
n = c(1L, 1L, 32L, 19L),
fips = c("01", "05", "06", "12")
)
Upvotes: 1