Show the code
library(plotly)
Latitude and longitude are coordinates for locating objects on earth.
library(plotly)
set.seed(3846) # random seed
<- 10 # number of points
N
# latitude from -90 to +90
<- runif(N, min = -90, max = 90)
latitude
# longitude from + -180 to + 180
<- runif(N, min = -180, max = 180)
longitude
# 1st point reset to 0, 0
1] <- 0 # equator
latitude[
1] <- 0 # prime meridian
longitude[
2] <- 42 # Ann Arbor-ish
latitude[
2] <- -83.5 # Ann Arbor-ish
longitude[
# label
<- LETTERS[1:N] # label with letters of alphabet
label
# dataframe
<- data.frame(latitude, longitude, label)
mydata
# replay mydata
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
<- list(lonaxis = list(showgrid = T, # geographic parameters
g gridcolor = "lightblue"),
lataxis = list(showgrid = T,
gridcolor = "lightblue"),
showland = TRUE,
landcolor = toRGB("lightgrey"))
<- plot_geo(mydata) %>%
mymap add_markers(x = ~longitude,
y = ~latitude,
color = ~label,
colors = "Spectral",
marker = list(size = 15)) %>%
layout(title = "Randomly Generated Coordinates",
geo = g)
# replay mymap