2013-06-05 57 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