4  Latitude and Longitude

4.1 Introduction

Latitude and longitude are coordinates for locating objects on earth.

  • Latitude represents distance from the equator. 0 latitude is at the equator. +90 latitude, or 90N, is at the North Pole. -90 latitude, or 90S, is at the South Pole.
  • Longitude represents distance from the prime meridian. 0 longitude is at the prime meridian. +180 and -180, or 180E and 180W, meet at the other side of the world.

4.2 Call the Libraries

Code
library(plotly)

4.3 Generate Some Random Coordinates

Code
set.seed(3846) # random seed

N <- 10 # number of points

# latitude from -90 to +90

latitude <- runif(N, min = -90, max = 90) 

# longitude from + -180 to + 180

longitude <- runif(N, min = -180, max = 180) 

# 1st point reset to 0, 0

latitude[1] <- 0 # equator

longitude[1] <- 0 # prime meridian

latitude[2] <- 42 # Ann Arbor-ish

longitude[2] <- -83.5 # Ann Arbor-ish

# label

label <- LETTERS[1:N] # label with letters of alphabet

# dataframe

mydata <- data.frame(latitude, longitude, label)

mydata # replay
     latitude  longitude label
1    0.000000    0.00000     A
2   42.000000  -83.50000     B
3    7.023357   73.49444     C
4  -38.775631  159.53317     D
5  -80.547165 -129.12572     E
6  -17.472905   25.95547     F
7  -69.826706  -54.36372     G
8   10.498416   14.20623     H
9  -72.440487  -27.65036     I
10 -46.753424  130.83572     J

4.4 Map The Coordinates

Code
g <- list(lonaxis = list(showgrid = T, # geographic parameters
                         gridcolor = "lightblue"), 
          lataxis = list(showgrid = T, 
                         gridcolor = "lightblue"),
          showland = TRUE,
          landcolor = toRGB("lightgrey"))

mymap <- plot_geo(mydata) %>%
  add_markers(x = ~longitude,
              y = ~latitude,
              color = ~label,
              colors = "Spectral",
              marker = list(size = 15)) %>%
  layout(title = "Randomly Generated Coordinates",
         geo = g)

mymap # replay