【问题标题】:Undisplayed countries in Plotly shift event_data return (Shiny)Plotly shift event_data 中未显示的国家/地区返回(闪亮)
【发布时间】:2017-02-09 14:57:01
【问题描述】:

我正在尝试确定在闪亮显示的世界地图中点击的国家/地区。

我使用了this example for click eventthis 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)
  • 处理得很好。

标签: r shiny plotly


【解决方案1】:

一种解决方法是使用包countrycode 中包含的国家列表并绘制它,然后删除(当前)不由plotly 处理的每个国家/地区。这给了我要删除的 ISO 代码列表:

CountriesNotInPlotly <- structure(list(
iso3c = c("ALA", "ASM", "AND", "AIA", "ATA", "ATG", 
"ABW", "BHR", "BRB", "BMU", "BES", "BIH", "BVT", "IOT", "CPV", 
"CYM", "CAF", "CXR", "CCK", "COM", "COD", "COG", "COK", "CUW", 
"CSK", "DMA", "FLK", "FRO", "GUF", "PYF", "DDR", "GIB", "GRD", 
"GLP", "GUM", "GGY", "HMD", "VAT", "HKG", "IMN", "JEY", "KIR", 
"LIE", "MAC", "MDV", "MLT", "MHL", "MTQ", "MUS", "MYT", "FSM", 
"MCO", "MSR", "NRU", "ANT", "NIU", "NFK", "MNP", "PLW", "PCN", 
"REU", "BLM", "SHN", "KNA", "LCA", "MAF", "SPM", "VCT", "WSM", 
"SMR", "STP", "SYC", "SGP", "SXM", "SGS", "SJM", "TKL", "TON", 
"TTO", "TCA", "TUV", "UMI", "VGB", "VIR", "WLF", "YMD", "YUG", 
"EAZ")), .Names = "iso3c", row.names = c(NA, -88L), class = "data.frame")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多