我从 mapdata 包中绘制了一张地图,当它没有被投影时,我可以很容易地向它添加一些点。我想对地图使用墨卡托投影,我也尝试投影点,但我没有找到将它们添加到这张地图的方法。也许我没有使用正确的投影字符串?还是我犯了一些愚蠢的错误?
# points dataset with coordinates of some European cities
places <- structure(list(city = structure(c(3L, 12L, 1L, 2L, 9L, 5L, 11L,
15L, 7L, 13L, 4L, 16L, 10L, 14L, 8L, 6L, 17L), .Label = c("Amsterdam",
"Berlin", "Brussels", "Copenhagen", "Dublin", "Genève", "Helsinki",
"Lisboa", "London", "Madrid", "Oslo", "Paris", "Reykjavik", "Roma",
"Stockholm", "Warsaw", "Wien"), class = "factor"), lon = c(4.3517103,
2.3522219, 4.8951679, 13.4060912, -0.1198244, -6.2603097, 10.7522454,
18.06491, 24.9410248, -21.89521, 12.5683371, 21.0122287, -3.7037902,
12.4825199, -9.1500364, 6.1422961, 16.3738189), lat = c(50.8503396,
48.856614, 52.3702157, 52.519171, 51.5112139, 53.3498053, 59.9138688,
59.32893, 60.1733244, 64.135338, 55.6760968, 52.2296756, 40.4167754,
41.8929163, 38.7252993, 46.1983922, 48.2081743)), .Names = c("city",
"lon", "lat"), row.names = c(NA, -17L), class = "data.frame")
library(maps)
library(mapdata)
# simple map with long lat as plot coordinates
map(database = "worldHires", xlim=c(-12,50), ylim=c(35, 70), resolution = 1, mar=c(0,0,0,0))
points(places$lon, places$lat, col = "red")
# transform the points data.frame into a spatial object
library(sp)
coordinates(places) <- c("lon", "lat")
proj4string(places) <- CRS("+proj=longlat")
# this map works
map(database = "worldHires", xlim=c(-12,50), ylim=c(35, 70), resolution = 1, mar=c(0,0,0,0))
plot(places, add=TRUE, col = "red")
# project the points with Mercator projection (maybe not the wright one?)
library(rgdal)
places <- spTransform(places, CRS("+proj=merc"))
# The map with mercator projection is ok but the points are not there (and their
# coordinates values are indeed very different from the map coordinates)
map(database = "worldHires",projection = "mercator", xlim=c(-12,50), ylim=c(35, 70), resolution = 1, mar=c(0,0,0,0))
plot(places, add=TRUE, col = "red")