简体   繁体   English

在ShinyApp中单击ggmap不会返回正确的坐标

[英]Clicking a ggmap in a ShinyApp does not return correct coordinates

If I attach a click event to a ggmap in a ShinyApp, and then click the map, I do not get back the correct coordinate points. 如果将click事件附加到ShinyApp中的ggmap,然后单击该地图,则无法获取正确的坐标点。

The following app plots a map with some points on it, and attaches a click event to the map. 以下应用绘制了一个地图,上面有一些点,并将click事件附加到地图上。 My goal is to have it so when I click the points, their data (lon/lat in this case) should appear in the outputId="coords" verbatimTextOutput. 我的目标是拥有它,因此当我单击这些点时,它们的数据(在本例中为lon / lat)应出现在outputId =“ coords” verbatimTextOutput中。

app.R: 应用程式R:

library(shiny);
library(ggmap);

# load data
points <- data.frame(lon=c(-122,-121.9,-121.8,-121.7),lat=c(37.2,37.2,37.2,37.2));
map <- get_map(location = c(lon = mean(points$lon),lat = mean(points$lat)),maptype="roadmap",scale=2, zoom =11)


ui <- fluidPage (
  plotOutput(outputId="mapOut",
             width="100%", height="500px",
             click = "plot_click"
  ),
  verbatimTextOutput(outputId="info"),
  verbatimTextOutput(outputId="coords")

)


server <- function(input,output) {

  output$mapOut <- renderPlot({
     mapPoints <-  ggmap(map) + geom_point(aes(x = lon, y = lat),size=8,colour="black", data=points);;
     mapPoints
  })

  output$info <- renderPrint({
    ptClicked <- nearPoints(points, coordinfo = input$plot_click, threshold = 10, maxpoints = 1, xvar="lon", yvar="lat");
    data.frame(lon=ptClicked$lon,lat=ptClicked$lat);

  })

  output$coords <- renderText({
    paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
  })
}

shinyApp(server = server, ui = ui)

When I run this app the points are displayed in the correct locations. 当我运行此应用程序时,这些点会显示在正确的位置。 If I click directly on a point, nearPoints() in output$info <- ... does not correctly identify the point. 如果我直接单击一个点,则output $ info <-...中的nearPoints()无法正确识别该点。 This is because the returned coordinate clicked is wrong. 这是因为单击的返回坐标错误。 (As you can see from the "coords" verbatim text output.) (从“ coords”逐字记录文本输出中可以看到。)

If I click to the RIGHT of the point, thereby making the returned click coordinate "on the point" in a data-sense, then the point is returned. 如果我单击该点的右侧,从而使返回的单击坐标在数据检测中“在该点上”,则返回该点。

The problem is similar to getting correct click coords with ggplot map in shiny , except in that case the coordinates were being returned as numbers in [0,1]; 该问题类似于使用闪亮的ggplot映射获得正确的单击坐标 ,除了在这种情况下,坐标以[0,1]中的数字形式返回; in my case, the coordinates look like true coordinates but are a bit wrong.... From that question I also noticed that even after rescaling using lon <- 400*x - 200 and lat <- 200*y - 100 the coordinates were still way off (eg (lon,lat)= (0,0) seems to be mapped to around (8.9,5.4)). 在我的情况下,坐标看起来像是真实坐标,但是有点错误。。。从这个问题中,我还注意到,即使在使用lon <-400 * x-200和lat <-200 * y-100重新缩放后,仍然遥不可及(例如(lon,lat)=(0,0)似乎映射到(8.9,5.4)左右)。 So ggplot seems to face the same problem when it is used to map. 因此,ggplot用于映射时似乎也面临相同的问题。

Is there a way to make a click of a ggmap/ggplot yield the correct coordinate? 有没有办法使ggmap / ggplot的点击产生正确的坐标? This might have something to do with projections. 这可能与预测有关。

If not, is there a different way to fix things so that the points can be correctly clicked? 如果不是,是否有其他方法可以修复问题,以便可以正确单击这些点? (Eg, one workaround might be to somehow capture the "as far as clicking is concerned" (x,y) locations of the points plotted, store them, and use them instead of lon/lat for nearPoints() purposes...). (例如,一种解决方法是以某种方式捕获所绘制点的“就单击而言”(x,y)的位置,进行存储,然后将它们代替lon / lat用作nearPoints()的目的...) 。

ggmap() by default uses the coord_map() projection in ggplot2 , which distorts the coordinate. ggmap()默认使用coord_map()在投影ggplot2 ,扭曲了坐标。 To get the correct clicked coordinate in Shiny, you need to turn it back to Cartesian coordinate. 要在“闪亮”中获得正确的单击坐标,您需要将其重新设置为笛卡尔坐标。 This can be done simply by adding coord_cartesian() . 只需添加coord_cartesian()即可完成。 As coord_map() has been stripped, aspect ratio of the output map can be any. 由于已删除coord_map() ,因此输出地图的纵横比可以是任意的。 So you need to change the width and height in plotOutput() to make the map still a nice map. 因此,您需要在plotOutput()更改宽度和高度,以使地图仍然是一张漂亮的地图。

Below is the code with the small modification mentioned above. 下面是带有上述小的修改的代码。 It produces correct click and nice map. 它产生正确的点击和漂亮的地图。

library(shiny);
library(ggmap);

# load data
points <- data.frame(lon=c(-122,-121.9,-121.8,-121.7),lat=c(37.2,37.2,37.2,37.2))
map <- get_map(location = c(lon = mean(points$lon),lat = mean(points$lat)),
               maptype="roadmap",scale=2, zoom =11)


ui <- fluidPage (
    plotOutput(outputId="mapOut",
               ##########################################################
               # force the ratio of the width and height so that the map
               # appears as square
               width=530, height=500,  
               click = "plot_click"
    ),
    verbatimTextOutput(outputId="info"),
    verbatimTextOutput(outputId="coords")

)


server <- function(input,output) {

    output$mapOut <- renderPlot({
        mapPoints <-  ggmap(map, extent = "normal") + 
            geom_point(aes(x = lon, y = lat),size=8,colour="black", data=points)
        #########################################################
        # add coord_cartesian() to strip coord_map() from ggmap()
        mapPoints + coord_cartesian()
    })

    output$info <- renderPrint({
        ptClicked <- nearPoints(points, coordinfo = input$plot_click, 
                                threshold = 10, maxpoints = 1, 
                                xvar="lon", yvar="lat");
        data.frame(lon=ptClicked$lon,lat=ptClicked$lat);

    })

    output$coords <- renderText({
        paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
    })
}

shinyApp(server = server, ui = ui)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM