Вы можете использовать географическую карту json с функцией readOGR , вы можете найти карту на inte rnet и связать с ней свой набор данных.
rgdal::readOGR(dsn = "GeoJSON map",
layer = "OGRGeoJSON",
stringsAsFactors = FALSE)
Ниже - это пример, основанный на этой записи stackoverflow для создания интерактивной карты на R Shiny. Выбранные страны находятся в click.list $ ids , см. вывод $ selected_var ниже.
Ваши данные
## packages ##
packages <- c("leaflet", "shiny", "shinydashboard")
newPackages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(newPackages)) install.packages(newPackages)
lapply(packages, library, character.only = TRUE)
remove(packages, newPackages)
## map & data ##
Europe <- rgdal::readOGR(dsn = "https://data.opendatasoft.com/explore/dataset/european-union-countries@public/download/?format=geojson&timezone=Europe/Berlin",
layer = "OGRGeoJSON",
stringsAsFactors = FALSE)
data <- data.frame("name" = c("Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czechia", "Denmark", "Estonia",
"Finland", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Italy", "Latvia",
"Liechtenstein", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Norway", "Poland", "Portugal",
"Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", "Turkey", "United Kingdom"),
"polcapita" = c(0.0087928207, 0.0100464969, 0.0075375536, 0.0049040898, 0.0097860082, 0.0119440135, 0.0087740252,
0.0080851042, 0.0063462331, 0.0064707328, 0.0107846429, 0.0085740997, 0.0059612600, 0.0409884683,
0.0138830047, 0.0067616652, 0.0049423915, 0.0053782730, 0.0053461017, 0.0165884166, 0.0046052235,
0.0116079951, 0.0052533159, 0.0100049243, 0.0075509189, 0.0047028415, 0.0067531703, 0.0077087169,
0.0064795469, 0.0008881585, 0.0053907055, 0.0053085690, 0.0069728195))
# example if you need to change the name of a country
Europe@data[Europe@data$name == "Czech Rep.", ]$name <- "Czechia"
# example if you want to add datas to the map
Europe@data$polcapita <- merge(x = Europe@data, y = data, sort = FALSE)$polcapita
pal <- colorNumeric(c("Green", "Red"), Europe$polcapita)
Пользовательская часть
## create the UI ##
ui <- fluidPage(
# place the contents inside a box
shinydashboard::box(
width = 12,
title = "Click on the map!",
# separate the box by a column
column(width = 2,
shiny::actionButton(inputId = "clearHighlight",
icon = icon( name = "eraser"),
label = "Clear the Map",
style = "color: #fff; background-color: #D75453; border-color: #C73232")),
# separate the box by a column
column(width = 10,
leaflet::leafletOutput(outputId = "myMap", height = 850)),
column(width = 5,
textOutput("selected_var"))
)
)
Серверная часть
## create the server ##
server <- function( input, output, session ){
# create foundational map
foundational.map <- shiny::reactive({
leaflet(Europe) %>%
fitBounds(-20, 65, 20, 39) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(data = Europe,
layerId = Europe$name,
color = ~pal(polcapita),
group = "click.list",
weight = 2,
fillOpacity = 0.3,
opacity = 1,
smoothFactor = 0.2,
stroke = FALSE)
})
output$myMap <- renderLeaflet({
foundational.map()
})
# store the list of clicked polygons in a vector
click.list <- shiny::reactiveValues(ids = vector())
# add countries to selection
shiny::observeEvent(input$myMap_shape_click, {
click <- input$myMap_shape_click
if(click$id %in% click.list$ids){
click.list$ids <- click.list$ids[!click.list$ids%in%click$id]
leaflet::leafletProxy(mapId = "myMap") %>%
clearGroup("lin")
} else{
click.list$ids <- c(click.list$ids, click$id)
}
lines.of.interest <- Europe[ which(Europe$name %in% click.list$ids), ]
leaflet::leafletProxy(mapId = "myMap") %>%
addPolylines(data = lines.of.interest,
layerId = lines.of.interest$ids,
color = "#6cb5bc",
weight = 2,
opacity = 1,
group = "lin")
})
# Clear the map button
shiny::observeEvent( input$clearHighlight, {
output$myMap <- leaflet::renderLeaflet({
click.list$ids <- NULL
foundational.map()
})
})
# selected countries
output$selected_var <- renderText({
paste("You have selected", click.list$ids)
})
}
Для запуска сервера
## run shinyApp ##
shiny::shinyApp(ui, server)
Надеюсь, он ответит на ваш вопрос!