我开始使用带有 ggplot 世界地图的 Shiny 应用程序。我想获取绘图上点击的坐标,以便用户可以使用 map 进行操作,但坐标非常奇怪(NULL
或非常小的东西)。反复点击似乎只改变一个坐标:
用户界面:
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)
})
})
最佳答案
只需将原始代码中的 print(worldmap)
替换为 worldmap
即可获得所需内容。 Shiny 与 ggplot2 完美配合。函数 print()
似乎生成了一个图形,其 x 和 y 在范围 (0, 1) 内重置。
关于r - 使用 Shiny 的ggplot map 获得正确的点击坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21655830/