我希望你能帮助我。我用Leaflet创建了一个Choropleth Map。我将我的(数据框与国家和一个随机分数)和一个Shapefile与Polygon数据合并。到目前为止它正在工作,但是如果我在R-Shiny中实现它,地图显示,但没有颜色。也没有错误显示。有人知道为什么R闪亮的传单addPolygons(颜色不显示)
我的代码:
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)
有没有办法让这[reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)? – alistaire
嗨@alistaire,我添加了一些可重复的代码;)。提前致谢。 – Josef
当我运行你的代码时,我看到三种颜色(阿根廷,巴西和澳大利亚) – MLavoie