简体   繁体   中英

How to add points into map?

There is the first part of the question: How to join two maps into one in r?

Complete code to bind two maps SWE and NOR (shp data were downloaded from: http://www.gadm.org/country ):

library(maptools)

mapa_shp_swe <- readShapePoly("C:/r/SWE_adm/SWE_adm0.shp")
mapa_map_swe <- fortify(mapa_shp_swe)

swe <- ggplot(mapa_map_swe, aes(x = long, y = lat, group=group)) + 
  geom_path(size=1) +
  theme_bw()

mapa_shp_nor <- readShapePoly("C:/r/NOR_adm/NOR_adm0.shp")
mapa_map_nor <- fortify(mapa_shp_nor)

nor <- ggplot(mapa_map_nor, aes(x = long, y = lat, group=group)) + 
  geom_path(size=1) +
  theme_bw()

n <- length(slot(mapa_shp_swe, "polygons"))
newShape <- spChFIDs(mapa_shp_nor, as.character(n))

newShape2 <- spRbind(newShape, mapa_shp_swe)

map <- ggplot(newShape2, aes(x = long, y = lat, group=group)) + 
  geom_path(size=1) +
  theme_bw()

How to add cities (as points) to this map? This doesn't work:

cities <- data.frame(ID = c("stockholm","Oslo"),
                     x = c(59.32, 59.95),
                     y = c(18.06, 10.75))

ggplot(newShape2, aes(x = long, y = lat, group=group)) + 
  geom_path(size=1) +
  geom_point(data = cities, aes(x = x, y = y)) +
  theme_bw()

Three things:

  1. I think that you messed up what is long and what is lat. Reverse the order of x and y in your geom_point
  2. Put the group aestethic into geom_path since it is not needed for any other geom at this point.
  3. Add some color and size to your points the default black might be hard to spot.

The following does probably work:

ggplot(newShape2, aes(x = long, y = lat)) + 
   geom_path(aes(group=group), size=1) +
   geom_point(data = cities, aes(x = y, y = x),col="red", size=5)+
   theme_bw()

Lycka till!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM