如何在地图中添加点?
How to add points into map?
有第一部分问题:
绑定两个地图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()
三件事:
- 我认为你弄乱了什么是长什么是纬度。颠倒
geom_point
中 x 和 y 的顺序
- 将 group aestethic 放入
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 直到!
有第一部分问题:
绑定两个地图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()
三件事:
- 我认为你弄乱了什么是长什么是纬度。颠倒
geom_point
中 x 和 y 的顺序
- 将 group aestethic 放入
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 直到!