[英]Plot coordinates as points on a map
我在這個網站上閱讀過類似的問題,但無法為我的特定數據集復制代碼(即“ https://stackoverflow.com/questions/65083749/why-dont-i-install-package-plotgooglemaps;Plot map 上的坐標。我正在嘗試將 plot 特定位置(GPS 坐標)作為全球 map 上的點。這個 map 最好采用衛星圖像的形式,但任何形式也可以。我沒有,但是,有一個API 密鑰。
我在 R 工作,我的 dataframe 的一個子集如下所示:
地點 | 經度 | 緯度 |
---|---|---|
扎金索斯 | 20.75 | 37.85 |
扎法蘭 | 18.73 | 30.39 |
費特希耶 | 29.06 | 36.67 |
感謝所有 SHoekstra
我檢查了類似的問題:
為什么我不安裝 package “plotGoogleMaps”?
出於某種原因,我正在努力讓它處理我的數據。
嘗試 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
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” package
# 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))
# 通過在參考系統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")
# 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
有一個 <- avoids that you get flooded by the html version of what you plot
在這里嘗試以下代碼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.