简体   繁体   English

在地图上绘制坐标

[英]Plot coordinates on map

I am trying to plot my coordinates using R. I have tried already to follow different post ( R: Plot grouped coordinates on world map ; Plotting coordinates of multiple points at google map in R ) but I am not having much success with my data.我正在尝试使用 R 绘制我的坐标。我已经尝试遵循不同的帖子( R:在世界地图上绘制分组坐标在 R 中的谷歌地图上绘制多个点的坐标),但我的数据并没有取得太大的成功。

I am trying to achieve a flat map of the world with my gps coordinate as colored dots (each area a specific color):我正在尝试使用我的 gps 坐标作为彩色点(每个区域具有特定颜色)来实现世界的平面地图:

area         lat    long
Agullhas    -38,31  40,96
Polar       -57,59  76,51
Tasmanian   -39,47  108,93

library(RgoogleMaps)
lat <- c(-38.31, -35.50) #define our map's ylim
lon <- c(40.96,37.50) #define our map's xlim
center = c(mean(lat), mean(lon))  #tell what point to center on
zoom <- 2 #zoom: 1 = furthest out (entire globe), larger numbers = closer in
terrmap <- GetMap(center=center, zoom=zoom, maptype= "satallite", destfile = "satallite.png")

problem that now I don't know how to add my points and I will like one color for each region.问题是现在我不知道如何添加我的积分,我会为每个区域喜欢一种颜色。

Could anyone help me going forward with it?有人可以帮我继续前进吗?

the other option I have tried is :我尝试过的另一个选择是:

library(maps)
library(mapdata)
library(maptools)
map(database= "world", ylim=c(-38.31, -35.5), xlim=c(40.96, 37.5), col="grey80", fill=TRUE, projection="gilbert", orientation= c(90,0,225))
lon <- c(-38.31, -35.5)  #fake longitude vector
lat <- c(40.96, 37.5)  #fake latitude vector
coord <- mapproject(lon, lat, proj="gilbert", orientation=c(90, 0, 225))  #convert points to projected lat/long
points(coord, pch=20, cex=1.2, col="red")  #plot converted points

but the coordinates ends in a wrong position and I am not sure why但坐标以错误的位置结束,我不知道为什么

Hope someone can help希望有人能帮忙

As an alternative to RgoogleMaps , you can also use the combination ggplot2 with ggmap .作为RgoogleMaps的替代方案,您还可以将ggplot2ggmap结合使用。

With this code:使用此代码:

# loading the required packages
library(ggplot2)
library(ggmap)

# creating a sample data.frame with your lat/lon points
lon <- c(-38.31,-35.5)
lat <- c(40.96, 37.5)
df <- as.data.frame(cbind(lon,lat))

# getting the map
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 4,
                      maptype = "satellite", scale = 2)

# plotting the map with some points on it
ggmap(mapgilbert) +
  geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) +
  guides(fill=FALSE, alpha=FALSE, size=FALSE)

you get this result:你得到这个结果:在此处输入图片说明

Another option is using the leaflet package (as suggested here ).另一种选择是使用传单包(如建议here )。 Unlike Google Maps it does not require any API key.与谷歌地图不同,它不需要任何 API 密钥。

install.packages(c("leaflet", "sp"))
library(sp)
library(leaflet)
df <- data.frame(longitude = runif(10, -97.365268, -97.356546), 
                 latitude = runif(10, 32.706071, 32.712210))

coordinates(df) <- ~longitude+latitude
leaflet(df) %>% addMarkers() %>% addTiles()

在 TCU 附近绘制 10 个随机点

An other alternative, is the plotGoogleMaps package which allows to plot in a navigator, allowing to zoom in and out etc. You can then make a screenshot of your picture to save it (though remember google maps are legally supposed to be used for the internet).另一种选择是plotGoogleMaps包,它允许在导航器中绘图,允许放大和缩小等。 然后您可以制作图片的屏幕截图以保存它(尽管记住谷歌地图在法律上应该用于互联网)。

 library("plotGoogleMaps")
 lat <- c(-38.31, -35.50) #define our map's ylim
 lon <- c(40.96,37.50) #define our map's xlim

 # make your coordinates a data frame 
 coords <- as.data.frame(cbind(lon=lon,lat=lat))

 # make it a spatial object by defining its coordinates in a reference system
 coordinates(coords) <- ~lat+lon 

 # you also need a reference system, the following should be a fine default
 proj4string(coords) <- CRS("+init=epsg:4326")
 # Note: it is a short for: 
 CRS("+init=epsg:4326")
 > CRS arguments:
 >  +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 

 # then just plot
 a <- plotGoogleMaps(coords)
 # here `a <-` avoids that you get flooded by the html version of what you plot 

And you get :你得到: 在此处输入图片说明

Here is a solution using only Rgooglemaps, as requested by the user.这是用户要求的仅使用 Rgooglemaps 的解决方案。

# get map (from askers OP, except changed map type = "Satallite" to type = "Satellite")
library(RgoogleMaps)
lat <- c(-38.31, -35.50) #define our map's ylim
lon <- c(40.96,37.50) #define our map's xlim
center = c(mean(lat), mean(lon))  #tell what point to center on
zoom <- 2 #zoom: 1 = furthest out (entire globe), larger numbers = closer in
terrmap <- GetMap(center=center, zoom=zoom, type= "satellite", destfile = "satellite.png")

# plot points and save image
lat <- c(-38.31, -57.59, -39.47)
lon <- c(40.96, 76.51, 108.93)
png('map.png')
PlotOnStaticMap(terrmap, lat = lat, lon = lon, pch = 20, col = c('red', 'blue', 'green'))
dev.off()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM