我希望你能帮助我。我用 Leaflet 创建了一个等值线地图。我将我的(数据框与国家和随机分数)和 Shapefile 与多边形数据合并。到目前为止它正在工作,但是如果我在 R-Shiny 中实现它,地图会显示,但没有颜色。也没有错误显示。有谁知道为什么?
我的代码:
ui <- fluidPage(
leafletOutput("map")
)
shinyServer(function(input, output) {
output$map <- renderLeaflet({
test_map
})
})
global.R
tmp <- tempdir()
url <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"
file <- basename(url)
download.file(url, file)
unzip(file, exdir = tmp)
world <- readOGR(dsn = tmp, layer = "ne_50m_admin_0_countries", encoding = "UTF-8")
data <- data.frame(Code = c("AR", "AU", "BE", "BR"),
Score = c(0.01, -0.05, 0.15, -0.22))
world <- merge(world, data,
by.x = "iso_a2",
by.y = "Code",
sort = FALSE)
pal <- colorNumeric(
palette = "RdYlGn",
domain = world$Score
)
test_map <- leaflet(data = world) %>%
addTiles() %>%
addPolygons(fillColor = ~pal(Score),
fillOpacity = 0.9,
color = "#BDBDC3",
weight = 1)