I am trying to use ggplot to map an area and label based on values in the @data slot. My error is the same as in the questions here, here, and here, but I can't translate those answers to my problem. I can plot states from the US maps from getData in the raster package but am having trouble from there. For an example these are the data sets I tried (I listed packages because I thought they might have something to do with the problem)...
library(rgdal)
library(raster)
library(ggplot2)
library(maps)
library(maptools)
library(ggmap) # used for theme_nothing() later
us <- getData("GADM",country="USA",level=1)
PA <- us[us$NAME_1 == 'Pennsylvania',]
AK <- us[us$NAME_1 == 'Alaska',]
When I try this I get a funky map (PA is a single polygon).
ggplot(data=PA, aes(long, lat)) +
geom_polygon(aes(group=group), color='black', fill=NA) +
geom_text(data=PA, aes(long, lat, label = PA$NAME_1))
And when I try this I get the Aesthetics error (AK consists of 2 polygons).
ggplot(data=AK, aes(long, lat)) +
geom_polygon(aes(group=group), color='black', fill=NA) +
geom_text(data=AK, aes(long, lat, label = AK$NAME_1))
Regions defined for each Polygons Regions defined for each Polygons Error: Aesthetics must be either length 1 or the same as the data (816071): x, y, label
I'm not sure why I have to put the $ in the geom_text with a data call already present, but it doesn't recognize the variable without it. I also tried to just color the 2 AK polygons by their OBJECTID as in this code, but get the same Aesthetics error.
ggplot(data=AK, aes(long, lat)) +
geom_polygon(aes(group=group), color='black', fill=AK$OBJECTID)
Thanks for any insight. I really want to use R for mapping more often.