获得正确的点击协调与ggplot地图在闪亮 [英] getting correct click coords with ggplot map in shiny

查看:378
本文介绍了获得正确的点击协调与ggplot地图在闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个闪亮的应用程序与ggplot世界地图的开始。我想获得点击坐标的绘图,所以用户可以做的事情与地图,但坐标是非常奇怪的( NULL 或一些非常小)。重复点击似乎改变了一个坐标:

I have the start of a shiny app with a ggplot map of the world. I would like to get the coordinates of the click on the plot so users can do things with the map, but the coordinates are very strange (either NULL or something very small). Clicking repeatedly only seems to change one coordinate:

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")
  )
))

server.R :

server.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世界地图,我得到看起来好的lat / long值的点击协调:

If I just use the map() command to generate a non-ggplot world map, I get what looks like good lat/long values for the click coords:

server.R(修改):

server.R (modified):

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

shinyServer(function(input, output) {

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

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

})


推荐答案

只需将 print(worldmap)替换为 worldmap 在您的原始代码中,你会得到你想要的。闪亮的作品完美地与ggplot2。函数 print()似乎生成一个图形,其x和y在范围(0,1)中复位。

Simply replace print(worldmap) with worldmap in your original code you will get what you want. Shiny works perfectly fine with ggplot2. The function print() seems generate a figure whose x and y are reset in the range (0, 1).

这篇关于获得正确的点击协调与ggplot地图在闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆