0

I have read similar questions on this site and haven't been able to replicate the code for my particular dataset (i.e. "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

# 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
and-bri
  • 1,563
  • 2
  • 19
  • 34
SHoekstra
  • 11
  • 3
  • Could you post an example of the code you wrote? You say you're struggling to get it to work, but more details would be nice – XaC Jan 13 '23 at 08:34
  • There is another approach discussed here: https://stackoverflow.com/questions/54759432/world-map-plot-with-latitude-and-longitude-points – Paul Stafford Allen Jan 13 '23 at 08:36
  • @PaulStaffordAllen I have tried the following code but still does not generate a plot (I am still new with R so might be a simple mistake): # 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) – SHoekstra Jan 13 '23 at 08:43
  • @XaC I will edit my question and provide example(s) of the code I wrote :) – SHoekstra Jan 13 '23 at 08:45
  • I think you need to change the section `aes(x = Longitude, y = Latitude)` to match the column names in your data frame - possibly `Lon` and `Lat` based on the question? – Paul Stafford Allen Jan 13 '23 at 08:47
  • @PaulStaffordAllen I tried but still no plot :( . I just read about the "mapview" package and this might be able to work for my assignment. – SHoekstra Jan 13 '23 at 09:23
  • see the answer by @beeflight31 below - please comment there if that approach works for your data, and if it does then tick that as an approved answer. If not, comment there. Thanks! – Paul Stafford Allen Jan 13 '23 at 09:24

1 Answers1

1

Here try the folowwing code 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")

enter image description here

Beeflight31
  • 227
  • 7