1

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.

zanahorias
  • 140
  • 12
  • I figured out one way to do it - filter world.cities for USA. But any input on why the us.cities data frame isn't working would still be helpful :) – zanahorias Mar 05 '17 at 08:40
  • Cannot reproduce this problem. Do you have the latest version of the maps package installed? Please post `head(us.cities)` – Richard Telford Mar 05 '17 at 08:52
  • @RichardTelford I added more code! hopefully it is sufficient – zanahorias Mar 05 '17 at 09:03
  • Replace `us.cities <- data(us.cities)` with `data(us.cities)`. Wait for sometime till you see the `us.cities` variable with the data in your environment before executing the subsequent steps. – Karthik Arumugham Mar 05 '17 at 10:12
  • 1
    @KarthikArumugham or just skip this line completely. R will find `us.cities` directly from `maps` - as it does in the next line for `world.cities` – Richard Telford Mar 05 '17 at 10:35

0 Answers0