如何在地图中添加点数?

时间:2015-03-24 18:25:06

标签: r dictionary ggplot2

问题的第一部分是:How to join two maps into one in r?

完整的代码绑定两个地图SWE和NOR(shp数据从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()

如何将城市(作为积分)添加到此地图? 这不起作用:

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()

1 个答案:

答案 0 :(得分:0)

三件事:

  1. 我认为你搞砸了什么是长期,什么是拉特。在geom_point
  2. 中颠倒x和y的顺序
  3. 将该组保留为geom_path,因为此时不需要任何其他geom。
  4. 为您的点添加一些颜色和大小,可能很难发现默认的黑色。
  5. 以下可能有效:

    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直到!