Show the code
library(plotly)
Latitude and longitude are coordinates for locating objects on earth.
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
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