简体   繁体   English

如何在地图上添加点?

[英]How to add points into map?

There is the first part of the question: How to join two maps into one in r? 问题的第一部分是: 如何在r中将两个映射合并为一个?

Complete code to bind two maps SWE and NOR (shp data were downloaded from: http://www.gadm.org/country ): 绑定两个地图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()

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 反转geom_point中x和y的geom_point
  2. Put the group aestethic into geom_path since it is not needed for any other geom at this point. 将组aethethic放入geom_path因为此时其他任何geom都不需要它。
  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! Lycka直到!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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