You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R语言中无API密钥绘制GPS坐标点至全球地图的问题

问题描述

我在R环境中尝试将特定GPS坐标点绘制到全球地图(优先卫星图形式,无需API密钥),参考相关内容后无法适配自身数据集。我的数据集子集如下:

LocationLonLat
Zakynthos20.7537.85
Zafaraan18.7330.39
Fethiye29.0636.67

我进行了四次尝试(使用ggmap、maps包,以及无法安装plotGoogleMaps包)均未成功,寻求解决方案。尝试代码如下:

尝试1

# My data stored in dataframe called "Data"
library("ggmap")
library(maptools)
library(maps)
mapWorld <- borders("world", colour="gray50", fill="white")
mp <- ggplot() + mapWorld

mp + geom_point(data = Data, aes(x = Longitude, y = Latitude), alpha = 0.5)

尝试2

library("ggmap")
library(maptools)
library(maps)

lon <- c(Data$Longitude)
lat <- c(Data$Latitude)
df <- as.data.frame(cbind(lon,lat))

# USING MAPS
map("world", fill=TRUE, col="white", bg="lightblue", ylim=c(-60, 90), mar=c(0,0,0,0))
points(lon,lat, col="red", pch=16)

尝试3

library("ggmap")
library(maptools)
library(maps)

#Using GGPLOT, plot the Base World Map
mp <- NULL
mapWorld <- borders("world", colour="gray50", fill="gray50") # create a layer of borders
mp <- ggplot() +   mapWorld

#Now Layer the cities on top
mp <- mp+ geom_point(aes(x=Data$Longitude, y=Data$Latitude) ,color="blue", size=3) 
mp

尝试4 - 无法安装plotGoogleMaps包

# Create maps 5
#install.packages("plotGoogleMaps", repos="http://R-Forge.R-project.org")
#install.packages("plotGoogleMaps")
#library("plotGoogleMaps")


lon <- c(Data$Longitude)
lat <- c(Data$Latitude)

# make your coordinates a data frame 
coords <- as.data.frame(cbind(lon,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

解决方案

1. 修复原有代码的核心问题

你的代码失败的主要原因是数据集列名不匹配:数据框列是Lon和Lat,但代码里一直用Longitude和Latitude,导致无法读取坐标。以下是修正后的代码:

修正maps包的绘图(尝试2修复版)

library(maps)

# 直接调用正确的列名
lon <- Data$Lon
lat <- Data$Lat

map("world", fill=TRUE, col="white", bg="lightblue", ylim=c(-60, 90), mar=c(0,0,0,0))
points(lon, lat, col="red", pch=16, cex=1.5)

修正ggplot2的绘图(尝试1/3修复版)

library(ggplot2)
library(maps)

mapWorld <- borders("world", colour="gray50", fill="white")
mp <- ggplot() + mapWorld + 
  geom_point(data = Data, aes(x = Lon, y = Lat), color="blue", size=3, alpha=0.7) +
  theme_minimal()
mp

2. 无API密钥的卫星图方案

如果需要卫星底图,推荐使用rosm包(无需API,调用OpenStreetMap的免费卫星图层),配合ggspatial实现ggplot2风格绘图:

步骤1:安装并加载依赖包

install.packages(c("rosm", "ggspatial", "ggplot2"))
library(rosm)
library(ggspatial)
library(ggplot2)

步骤2:绘制卫星底图+标记点

# 扩展坐标范围,让所有点都在视图内
x_range <- range(Data$Lon) + c(-2, 2)
y_range <- range(Data$Lat) + c(-2, 2)

# 生成卫星图+标记
ggplot() +
  # 添加卫星底图(可选其他风格,如stamen_watercolor)
  annotation_map_tile(type = "stamen_terrain_bg", zoom = 6) +
  # 标记坐标点
  geom_point(data = Data, aes(x = Lon, y = Lat), color="red", size=4, shape=19) +
  # 可选:添加地点名称标注
  geom_text(data = Data, aes(x = Lon, y = Lat, label = Location), hjust=1.1, vjust=0, color="white") +
  # 设置WGS84坐标系统
  coord_sf(crs = 4326, xlim = x_range, ylim = y_range) +
  theme_minimal()

可通过rosm::osm.types()查看更多无API的底图类型。

3. 替代plotGoogleMaps的无API交互式地图

如果需要交互式地图,推荐使用leaflet包(基于OpenStreetMap,无需API密钥):

install.packages("leaflet")
library(leaflet)

# 创建交互式地图
leaflet(Data) %>%
  # 切换卫星底图可替换为:addProviderTiles(providers$Esri.WorldImagery)
  addTiles() %>%
  addMarkers(lng = ~Lon, lat = ~Lat, popup = ~Location)

内容的提问来源于stack exchange,提问作者SHoekstra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 23:05:23