# 创建一个示例数据框,包括美国一些大城市的信息
<- data.frame(
data Name = c("New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia", "San Antonio", "San Diego", "Dallas", "San Jose"),
Latitude = c(40.7128, 34.0522, 41.8781, 29.7604, 33.4484, 39.9526, 29.4241, 32.7157, 32.7767, 37.3541),
Longitude = c(-74.0060, -118.2437, -87.6298, -95.3698, -112.0740, -75.1652, -98.4936, -117.1611, -96.7970, -121.9552),
Population = c(8175133, 3792621, 2695598, 2349783, 1680992, 1584064, 1547253, 1423851, 1317929, 1035317)
)
# 将数据保存为CSV文件
write.csv(data, "data/leaflet_us_city.csv", row.names = FALSE)
Shiny地图可视化包leaflet
R语言
Shiny开发
使用leaflet包制作可视化地图
## 案例演示
library(shiny)
library(shinydashboard)
library(leaflet)
# UI定义
<- dashboardPage(
ui dashboardHeader(title = "Leaflet 地图可视化应用"),
dashboardSidebar(
# 侧边栏内容(如果有的话)
),dashboardBody(
fluidRow(
column(12,
box(
title = "Leaflet 地图",
solidHeader = TRUE,
width=12,
status = "primary",
height = 600,
leafletOutput("map"),
::capture(filename = 'shiny',format = 'png',
captureselector = 'body',icon('camera'),'screenshot'
)
)
)
)
)
)
# 服务器逻辑
<- function(input, output) {
server # 读取数据,替换为你自己的数据文件路径
<- read.csv("data/leaflet_us_city.csv")
data
# 创建 Leaflet 地图
$map <- renderLeaflet({
outputleaflet() %>%
addTiles() %>%
addCircleMarkers(data = data,
lat = ~Latitude,
radius = ~Population/1e5,
lng = ~Longitude,
popup = ~Name) %>%
setView(lng = -95.7129, lat = 37.0902, zoom = 3)
})
}
# 启动应用
shinyApp(ui, server)