【发布时间】:2017-02-09 14:57:01
【问题描述】:
我正在尝试确定在闪亮显示的世界地图中点击的国家/地区。
我使用了this example for click event 和this one for display,这段代码有效:
library(shiny)
library(plotly)
library(countrycode)
df <- countrycode_data[,c("iso3c", "country.name")]
df <- df[complete.cases(df),]
df$random <- rnorm(dim(df)[1])
shinyApp(
ui = shinyUI(fluidPage(plotlyOutput("plot"), textOutput("text"))),
server = shinyServer(function(input, output) {
output$text <- renderPrint({
d <- event_data("plotly_click")
ifelse( is.null(d),
"No selection",
as.character(df[as.numeric(d[2])+1,"country.name"]))
})
output$plot <- renderPlotly({plot_ly(df, z=~random, locations=~iso3c,
text=~country.name, type="choropleth")})
}))
当我点击数据集的第一个国家阿富汗时,它会正确选择它
但是如果我点击第三个Albania,它会返回第二个Aland Islands,Plotly不显示。
所以我猜测选择是根据我在原始数据集(包括所有国家/地区)上投影的显示条目列表计算的
我试图在 github 上找到 Plotly 使用的国家/地区列表,但未成功,我可以使用它来删除未知国家/地区并修复转变。
【问题讨论】:
-
countrycode 很不错。
-
感谢@alistaire,但它也包含美属萨摩亚(ASM)和安道尔(AND)
-
处理得很好。