简体   繁体   English

Plot 坐标作为 map 上的点

[英]Plot coordinates as points on a map

I have read similar questions on this site and haven't been able to replicate the code for my particular dataset (ie "https://stackoverflow.com/questions/65083749/why-dont-i-install-package-plotgooglemaps; Plot coordinates on map ). I am trying to plot specific locations (GPS coordinates) as points on a global map. This map would preferably be in the form of a satellite image but any form could also work. I do not, however, have an API key.我在这个网站上阅读过类似的问题,但无法为我的特定数据集复制代码(即“ https://stackoverflow.com/questions/65083749/why-dont-i-install-package-plotgooglemaps;Plot map 上的坐标。我正在尝试将 plot 特定位置(GPS 坐标)作为全球 map 上的点。这个 map 最好采用卫星图像的形式,但任何形式也可以。我没有,但是,有一个API 密钥。

I am working in R and a subset of my dataframe looks like this:我在 R 工作,我的 dataframe 的一个子集如下所示:

Location地点 Lon经度 Lat纬度
Zakynthos扎金索斯 20.75 20.75 37.85 37.85
Zafaraan扎法兰 18.73 18.73 30.39 30.39
Fethiye费特希耶 29.06 29.06 36.67 36.67

Thanks all SHoekstra感谢所有 SHoekstra

I have checked out similar questions:我检查了类似的问题:

Why don't I install package "plotGoogleMaps"? 为什么我不安装 package “plotGoogleMaps”?

Plot coordinates on map Plot 坐标 map

I am struggling to get it to work on my data for some reason.出于某种原因,我正在努力让它处理我的数据。

Attempt 1 # create maps 2尝试 1 # create maps 2

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

Attempt 2尝试 2

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

lon <- c(Data$Longitude) lat <- c(Data$Latitude) df <- as.data.frame(cbind(lon,lat)) 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) #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)

Attempt 3尝试 3

library("ggmap") library(maptools) library(maps) 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 #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 #Now Layer the cities on top mp <- mp+ geom_point(aes(x=Data$Longitude, y=Data$Latitude),color="blue", size=3) mp

Attempt 4 - could not install "plotGoogleMaps" package尝试 4 - 无法安装“plotGoogleMaps” package

# Create maps 5 #install.packages("plotGoogleMaps", repos="http://R-Forge.R-project.org") #install.packages("plotGoogleMaps") #library("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) lon <- c(Data$Longitude) lat <- c(Data$Latitude)

# make your coordinates a data frame coords <- as.data.frame(cbind(lon,lat)) # 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 # 通过在参考系统coordinates(coords) <- ~lat+lon # make it a spatial object by defining its coordinates in a reference

# you also need a reference system, the following should be a fine default proj4string(coords) <- CRS("+init=epsg:4326") # 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 # 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 # then just plot a <- plotGoogleMaps(coords) # here有一个 <- avoids that you get flooded by the html version of what you plot

Here try the folowwing codehttps://datavizpyr.com/how-to-make-world-map-with-ggplot2-in-r/在这里尝试以下代码https://datavizpyr.com/how-to-make-world-map-with-ggplot2-in-r/

library(ggplot2)
library(tidyverse)

world <- map_data("world")

ggplot() +
  geom_map(
    data = world, map = world,
    aes(long, lat, map_id = region),
    color = "black", fill = "lightgray", size = 0.1
  )+
  geom_point(data=data, aes(Lon,Lat), color="red")

在此处输入图像描述

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

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