问题的第一部分是: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()
答案 0 :(得分:0)
三件事:
geom_point
geom_path
,因为此时不需要任何其他geom。以下可能有效:
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直到!