Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot coordinates on map

I am trying to plot my coordinates using R. I have tried already to follow different post (R: Plot grouped coordinates on world map ; Plotting coordinates of multiple points at google map in R) but I am not having much success with my data.

I am trying to achieve a flat map of the world with my gps coordinate as colored dots (each area a specific color):

area         lat    long Agullhas    -38,31  40,96 Polar       -57,59  76,51 Tasmanian   -39,47  108,93  library(RgoogleMaps) lat <- c(-38.31, -35.50) #define our map's ylim lon <- c(40.96,37.50) #define our map's xlim center = c(mean(lat), mean(lon))  #tell what point to center on zoom <- 2 #zoom: 1 = furthest out (entire globe), larger numbers = closer in terrmap <- GetMap(center=center, zoom=zoom, maptype= "satallite", destfile = "satallite.png") 

problem that now I don't know how to add my points and I will like one color for each region.

Could anyone help me going forward with it?

the other option I have tried is :

library(maps) library(mapdata) library(maptools) map(database= "world", ylim=c(-38.31, -35.5), xlim=c(40.96, 37.5), col="grey80", fill=TRUE, projection="gilbert", orientation= c(90,0,225)) lon <- c(-38.31, -35.5)  #fake longitude vector lat <- c(40.96, 37.5)  #fake latitude vector coord <- mapproject(lon, lat, proj="gilbert", orientation=c(90, 0, 225))  #convert points to projected lat/long points(coord, pch=20, cex=1.2, col="red")  #plot converted points 

but the coordinates ends in a wrong position and I am not sure why

Hope someone can help

like image 482
flacchy Avatar asked Apr 17 '14 10:04

flacchy


People also ask

Can you plot coordinates on Google Maps?

Enter coordinates to find a place On your computer, open Google Maps. In the search box, enter your coordinates. Here are examples of formats that work: Decimal degrees (DD): 41.40338, 2.17403.

How do I plot latitude and longitude coordinates on Google Maps?

Go to maps.google.com. 2. Type out the coordinates into the search bar — using either the degrees, minutes, and seconds (DMS) format, the degrees and decimal minutes (DMM) format, or decimal degrees (DD) format — then hit enter or click on the search icon.


2 Answers

As an alternative to RgoogleMaps, you can also use the combination ggplot2 with ggmap.

With this code:

# loading the required packages library(ggplot2) library(ggmap)  # creating a sample data.frame with your lat/lon points lon <- c(-38.31,-35.5) lat <- c(40.96, 37.5) df <- as.data.frame(cbind(lon,lat))  # getting the map mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 4,                       maptype = "satellite", scale = 2)  # plotting the map with some points on it ggmap(mapgilbert) +   geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) +   guides(fill=FALSE, alpha=FALSE, size=FALSE) 

you get this result: enter image description here

like image 103
Jaap Avatar answered Sep 22 '22 23:09

Jaap


Another option is using the leaflet package (as suggested here). Unlike Google Maps it does not require any API key.

install.packages(c("leaflet", "sp")) library(sp) library(leaflet) df <- data.frame(longitude = runif(10, -97.365268, -97.356546),                   latitude = runif(10, 32.706071, 32.712210))  coordinates(df) <- ~longitude+latitude leaflet(df) %>% addMarkers() %>% addTiles() 

Plot 10 random points in vicinity of TCU

like image 38
bonna Avatar answered Sep 18 '22 23:09

bonna