2013-06-05 58 views
2

我绘制从包属于MapData地图和我可以很容易地一些点添加到它时,不被投影它。 我想用墨卡托投影地图,我尝试投射点太多,但我没有发现将它们添加到这个地图的方式。也许我不使用正确的投影字符串?或者我犯了一些愚蠢的错误?到地图添加点与墨卡托投影中的R

# 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") 

回答

3

使用您的places对象的原始版本,你可以使用mapproj()功能点添加到您的投影地图,作为this webpage描述。

map(database="worldHires", projection="mercator", 
    xlim=c(-12,50), ylim=c(35, 70), 
    resolution=1, mar=c(0, 0, 0, 0)) 
points(mapproject(places$lon, places$lat), col="red") 
+0

感谢您的答复。真的很有帮助。如果有人知道我为什么用sptransform的第一个方法不起作用,我仍然感兴趣。还有财产以后我不明白mapproject:如果我明确地指定投影点是不是在地图上的正确位置:点(mapproject(地方$ LON,则以$ LAT,PROJ =“墨卡托”), col =“red”)。为什么? – Gilles

+0

关于投影,阅读'mapproj'帮助文件中的'orientation'参数下。 _'这意味着用他们自己的默认方向绘制的两张地图可能不一致。 **为避免这种情况,您不应该指定两次投影,而应该使用投影=“”来默认投影。**请参阅示例。'_ –

+0

好的,理解。再次感谢 ! – Gilles