在地图上绘制坐标

fla*_*chy 34 maps google-maps r coordinates ggmap

我试图用R绘制我的坐标.我已经尝试过跟随不同的帖子(R:在世界地图上绘制分组坐标 ; 在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")
Run Code Online (Sandbox Code Playgroud)

问题,现在我不知道如何添加我的点,我想为每个区域一种颜色.

谁能帮助我继续前进呢?

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

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
Run Code Online (Sandbox Code Playgroud)

但坐标以错误的位置结束,我不知道为什么

希望有人能提供帮助

Jaa*_*aap 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)
Run Code Online (Sandbox Code Playgroud)

你得到这个结果: 在此输入图像描述

  • 谷歌地图需要 API 密钥 (3认同)
  • 我不确定我的R版本是否有问题我一直收到此错误:download.file中的错误(url,destfile = destfile,quiet =!messaging,mode ="wb"):不可能打开URL'http:/ /maps.googleapis.com/maps/api/staticmap?center=39.23,-36.905&zoom=4&size=%20640x640&scale=%202&maptype=satellite&sensor=false'另外:警告消息:在download.file(url,destfile = destfile,quiet) =!messaging,mode ="wb"):无法打开:状态HTTP'403 Forbidden'你能告诉我什么是错的吗??? 我使用相同的命令...... (2认同)

bon*_*nna 8

另一种选择是使用传单包(如建议here)。与谷歌地图不同,它不需要任何 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()
Run Code Online (Sandbox Code Playgroud)

在 TCU 附近绘制 10 个随机点