us.cities from maps package in r not working

1.6k Views Asked by At

I am trying to add US cities to my map in R.

When I add world cities:

ggplot(data = usa) +
  geom_polygon(aes(x = longitude, y = latitude, group = group, fill = id)) +
  coord_quickmap() + 
  guides(fill = FALSE) +
  theme(axis.text = element_blank()) +
  theme(axis.ticks = element_blank()) +
  theme(axis.title = element_blank()) +
  xlim(-169, -67) +
  ylim(17,72)+
  scale_fill_discrete()+
  geom_point(data=world.cities, aes(x=long, y = lat, size= capital))

or canada cities:

ggplot(data = usa) +
  geom_polygon(aes(x = longitude, y = latitude, group = group, fill = id)) +
  coord_quickmap() + 
  guides(fill = FALSE) +
  theme(axis.text = element_blank()) +
  theme(axis.ticks = element_blank()) +
  theme(axis.title = element_blank()) +
  xlim(-169, -67) +
  ylim(17,72)+
  scale_fill_discrete()+
  geom_point(data=canada.cities, aes(x=long, y = lat, size= capital))

it works just fine. However, when I try replacing it with us.cities, which is supposed to be part of the maps package (https://cran.r-project.org/web/packages/maps/maps.pdf), it gives the error:

ggplot2 doesn't know how to deal with data of class character.

any ideas on what could be the issue??

edit: more code:

library(maps)
library(fiftystater)

usa <- data("fifty_states")
us.cities <- data(us.cities)
usa.cities <- world.cities %>% filter(country.etc=="USA")
map <- map_data("world")
hawaii <- read.csv("hawaii.csv", stringsAsFactors = FALSE)
alaska <- read.csv("alaska.csv", stringsAsFactors = FALSE)
forty8states <- fifty_states %>% filter(id != "hawaii") %>% filter(id    !="alaska") %>% select(long, lat, id, group)

alaska <- read.csv("alaska.csv", stringsAsFactors = FALSE)
hawaii <- read.csv("hawaii.csv", stringsAsFactors = FALSE)

usa <- rbind(forty8states, alaska, hawaii)
usa <- usa %>% mutate(longitude = round(long, digits = 2)) 
usa <- usa %>% mutate(latitude = round(lat, digits = 2))

the hawaii and alaska csv's are the hawaii and alaska data from the world data frame from maps, with some editing so they're in the same format as the rest.

0

There are 0 best solutions below