This map is an example of an interactive maps using plotly.
# Set CRAN mirror
options(repos = c(CRAN = "https://cran.rstudio.com"))
# Install and load the plotly library
if (!requireNamespace("plotly", quietly = TRUE)) {
install.packages("plotly")
}
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
# Sample data
locations <- c("New York", "Los Angeles", "Chicago")
values <- c(100, 200, 150)
lon <- c(-74.0060, -118.2437, -87.6298)
lat <- c(40.7128, 34.0522, 41.8781)
# Create a data frame
data <- data.frame(locations, values, lon, lat)
# Create a plotly scatter plot with geo coordinates
plot_ly(data,
type = 'scattergeo',
locationmode = 'USA-states',
lon = ~lon,
lat = ~lat,
text = ~locations,
mode = 'markers',
marker = list(size = ~values,
color = ~values,
colorscale = 'Viridis',
colorbar = list(title = 'Values'))
) %>%
layout(title = 'Interactive Map',
geo = list(scope = 'usa',
projection = list(type = 'albers usa'),
showland = TRUE,
landcolor = toRGB("grey"),
subunitwidth = 1,
countrywidth = 1,
subunitcolor = toRGB("white"),
countrycolor = toRGB("white")
)
)