简体   繁体   中英

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.

I am working in R and a subset of my dataframe looks like this:

Location Lon Lat
Zakynthos 20.75 37.85
Zafaraan 18.73 30.39
Fethiye 29.06 36.67

Thanks all SHoekstra

I have checked out similar questions:

Why don't I install package "plotGoogleMaps"?

Plot coordinates on map

I am struggling to get it to work on my data for some reason.

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

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

Attempt 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

Attempt 4 - could not install "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))

# 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

Here try the folowwing codehttps://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")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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