【问题标题】:getting correct click coords with ggplot map in shiny使用闪亮的ggplot地图获得正确的点击坐标
【发布时间】:2014-02-09 05:49:18
【问题描述】:

我开始使用带有 ggplot 世界地图的闪亮应用程序。我想获得点击图的坐标,以便用户可以使用地图进行操作,但坐标非常奇怪(NULL 或非常小的东西)。反复点击似乎只改变一个坐标:

ui.R:

library(shiny)

# Define UI for application
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("My App"),
  sidebarPanel(
    textOutput("clickcoord")
  ),

  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("myworld", height="600px", clickId="plotclick")
  )
))

服务器.R:

library(shiny)
library(maps)
library(mapdata)
library(ggplot2)
library(rworldmap)

shinyServer(function(input, output) {

  output$myworld <- renderPlot({

     world <- map_data("world")
     worldmap <- ggplot(aes(x = long, y = lat, group = group), data = world) +
       geom_path()

    print(worldmap)
  })

  output$clickcoord <- renderPrint({
          print(input$plotclick)
  })
})

如果我只使用 map() 命令生成非 ggplot 世界地图,我会得到看起来不错的点击坐标纬度/经度值:

server.R(修改):

library(shiny)
library(maps)
library(mapdata)

shinyServer(function(input, output) {

    output$myworld <- renderPlot({
      map("world2Hires")
    })

    output$clickcoord <- renderPrint({
      print(input$plotclick) 
    })

})

【问题讨论】:

  • AFAIK,shiny 不支持 ggplot2(或更一般的网格图形)的绘图定位。
  • 我试过运行这段代码,首先发现 server.R 文件中缺少右花括号和右括号。添加这些后,我现在在 FireFox 网络选项卡中看到 ERROR: argument "metaHandler" is missing, with no default。这些示例在我正在关注的闪亮教程中运行良好,所以我认为我没有缺少基本组件。
  • 很抱歉。修复了缺少的分隔符。我仔细检查了这个例子,它工作正常。你有最新的 Shiny (0.8 iirc) 吗?
  • 在一个示例中,我尝试了不同的设置,x-y 坐标在 0-1 方格中,所以也许可以适当地缩放它们...
  • @user1965813 希望那里也有一些投影信息。

标签: r user-interface ggplot2 click shiny


【解决方案1】:

只需在您的原始代码中将print(worldmap) 替换为worldmap,您就会得到您想要的。 Shiny 与 ggplot2 完美配合。函数print() 似乎生成了一个图形,其 x 和 y 在 (0, 1) 范围内重置。

【讨论】:

    猜你喜欢
    • 2018-05-04
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多