gpt4 book ai didi

r - 在 R 中使用墨卡托投影向 map 添加点

转载 作者:行者123 更新时间:2023-12-04 04:54:31 25 4
gpt4 key购买 nike

我从 mapdata 包中绘制了一张 map ,当它没有被投影时,我可以很容易地向它添加一些点。
我想对 map 使用墨卡托投影,我也尝试投影点,但我没有找到将它们添加到这张 map 的方法。也许我没有使用正确的投影字符串?还是我犯了一些愚蠢的错误?

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

最佳答案

使用您的 places 的原始版本对象,您可以使用 mapproj() 向投影 map 添加点功能,如 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")

关于r - 在 R 中使用墨卡托投影向 map 添加点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16942245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com