[在地图上绘制坐标

问题描述 投票:34回答:4

我正在尝试使用R绘制坐标。我已经尝试遵循不同的文章(R: Plot grouped coordinates on world map; Plotting coordinates of multiple points at google map in R),但是我的数据并没有取得太大的成功。

我正在尝试使用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")

问题,现在我不知道如何添加点,我希望每个区域都使用一种颜色。

有人可以帮助我继续前进吗?

我尝试过的另一个选项是:

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

但是坐标以错误的位置结束,我不确定为什么

希望有人可以帮助您

r google-maps maps coordinates ggmap
4个回答
52
投票

作为RgoogleMaps的替代,也可以将ggplot2ggmap组合使用。

使用此代码:

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

您得到以下结果:“在此处输入图像描述”


4
投票

[另一种选择是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 

您得到:enter image description here


4
投票

另一种选择是使用leaflet package(建议here)。与Google Maps不同,它不需要任何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()

Plot 10 random points in vicinity of TCU


0
投票

这里是根据用户要求仅使用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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.