2015-09-26 31 views
0

如果我在ShinyApp中將單擊事件附加到ggmap中,然後單擊地圖,我不會找回正確的座標點。單擊ShinyApp中的ggmap不會返回正確的座標

下面的應用程序繪製一個地圖上有一些點,並將一個點擊事件附加到地圖上。我的目標是當我點擊這些點時,它們的數據(在這種情況下爲lon/lat)應該出現在outputId =「coords」verbatimTextOutput中。

app.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) 

當我運行這個應用程序中的點都顯示在正確的位置。如果我直接點擊某個點,輸出$ info <中的nearPoints()不能正確識別該點。這是因爲點擊返回的座標是錯誤的。 (正如你可以從「coords」逐字文本輸出中看到的那樣)。

如果我點擊該點的右側,從而使數據意義上的返回點擊座標爲「在點上」,那麼點回。

該問題類似於getting correct click coords with ggplot map in shiny,除非在這種情況下座標被返回爲[0,1]中的數字;在我的情況下,座標看起來像真正的座標,但有點錯誤....從這個問題我也注意到,即使在使用lon < - 400 * x - 200和lat < - 200 * y - 100重新縮放後,座標是仍然離開(例如(lon,lat)=(0,0)似乎映射到(8.9,5.4))左右。所以ggplot在用於映射時似乎也面臨同樣的問題。

有沒有辦法讓ggmap/ggplot的點擊產生正確的座標?這可能與預測有關。

如果不是,有沒有不同的方法來解決事情,使點可以正確點擊? (例如,一種解決方法可能是以某種方式捕獲繪圖點上的「就點擊而言」(x,y)位置,將它們存儲起來,並將它們用於nearPoints()目的,而不是lon/lat) 。

回答

2

ggmap()默認情況下使用coord_map()投影ggplot2,這會扭曲座標。要在Shiny中獲得正確的單擊座標,您需要將其重新轉換爲笛卡爾座標。這可以簡單地通過添加coord_cartesian()來完成。由於coord_map()已被剝離,輸出映射的高寬比可以是任意值。因此,您需要更改plotOutput()中的寬度和高度,以使地圖仍然是一張不錯的地圖。

下面是上面提到的小修改的代碼。它產生正確的點擊和漂亮的地圖。

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) 
+0

偉大的解決方案.. [如果需要可以通過設置繪圖的寬度和高度設置閃亮的用戶界面的高寬比] – IndranilGayen

+0

好主意!我試圖用'coord_fixed'來代替,另外使用'xlim'和'ylim'參數放大/縮小。這似乎失敗了。我嘗試了去除高度和寬度的變化,但它們都看起來失敗了。任何想法爲什麼? – TheComeOnMan

+0

我也問過這裏 - http://stackoverflow.com/questions/40724059/clicking-a-ggmap-in-a-shinyapp-does-not-return-correct-coordinates-2。 – TheComeOnMan