闪亮的点击/画笔不能使用非笛卡尔坐标? [英] Shiny click/brush not working with non-cartesian coordinates?

查看:197
本文介绍了闪亮的点击/画笔不能使用非笛卡尔坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Shiny应用程序,它可以让用户在由ggplot2生成的世界地图上选择地理数据点(如这个例子)。

如果我使用常规coord_cartesian坐标系(这扭曲了地图),但如果使用更合适的坐标系统,则会失败。看起来点击/笔刷事件在投影后没有收到正确的坐标。有什么方法可以解决这个问题,而无需恢复到笛卡尔坐标?

您可以在下面找到一个工作示例:

  library(shiny)
library(ggplot2)
library(dplyr)

map_world< - function(world ,mapPoints,xlim,ylim){
#函数来生成ggplot世界地图
ggplot()+
geom_polygon(data = world,aes(x = long,y = lat,group = group) ))+
geom_point(data = mapPoints,aes(x = Longitude,y = Latitude),color =red)+
#coord_map弄错笔刷选择
coord_map(xlim = xlim,ylim = ylim)
#coord_cartesian可以工作但扭曲地图
#coord_cartesian(xlim = xlim,ylim = ylim)
}

mapPoints< - data.frame(经度= c(-103,-108,-130,-120),
纬度= c(52,40,45,54))
world < - map_data(world )

ui< - fluidPage(
fluidRow(
column(width = 6,
plotOutput(plot1,高度= 300,
click =plot1_click,brush =plot1_brush))
),
fluidRow(
column(width = 6,
h4( (刷点),verbatimTextOutput(brush_info)),
)$ b $($) b)

服务器< - 函数(输入,输出){
#输出世界地图
输出$ plot1 < - renderPlot({
map_world(world =世界,mapPoints = mapPoints,
xlim = c(-180,180),ylim = c(-90,90))
})
#输出点击点
输出$ click_info< ; - renderPrint({
nearPoints(mapPoints,xvar =Longitude,
yvar =Latitude,输入$ plot1_click)
})
#输出刷点数
输出$ brush_info< - renderPrint({
brushedPoints(mapPoints,xvar =Longitude,
yvar =Latitude,input $ plot1_brush)
})
}

shinyApp(ui,serve r)

谢谢!

解决方案

我尝试了一些使用 ggplot 的解决方案,但是没有被 plotOutput拾取的缩放问题函数。在帮助页面( http://shiny.rstudio.com/reference/shiny/最新/ plotOutput.html ),请注意以下几点:


对于 plotOutput imageOutput ,坐标将以原始像素坐标进行发送。


lockquote>

使用ggplot2图形, renderPlot 中的代码应该返回一个ggplot对象;如果相反,代码打印ggplot2对象时,如 print(p),那么交互式图形的坐标将不会正确缩放到数据空间。


我尝试将 ggplot 移动到脚本的服务器部分,但它不起作用。

我建议的解决方案,使用base graphics:

  library闪亮)
图书馆(dplyr)
图书馆(地图)
图书馆(地图数据)


mapPoints< - data.frame(Longitude = c( -103,-108,-130,-120),
Latitude = c(52,40,45,54))

ui< - fluidPage(
fluidRow
column(width = 6,
plotOutput(plot1,height = 300,
click =plot1_click,brush =plot1_brush))
),$ b $ (b $)列(宽度= 6,
h4(近点),verbatimTextOutput(click_info)),
列(宽度= 6,
h4(磨毛),verbatimTextOutput(brush_info))



服务器< - 函数(输入,输出){
#输出世界地图
输出$ plot1 < - renderPlot({
map('worldHires')
points(mapPoints $ Longitude,mapPoints $ Latitude,
col =red,pch = 16)
})
#输出点击点
输出$ click_info< - renderPrint({
nearPoints(mapPoints,xvar =Longitude,
yvar =Latitude,输入$ plot1_click)
})
#输出刷点
输出$ brush_info< - renderPrint({
brushedPoints(mapPoints,xvar =Longitude,
yvar =Latitude,输入$ plot1_brush)
})
}

shinyApp(ui,server)


I am working on a Shiny app that should let a user select geographical data points on a world map generated by ggplot2 (as in this example).

This works if I use the regular coord_cartesian coordinate system (which distorts the maps) but fails if I use the more appropriate coord_map coordinate system. It seems like the click/brush events do not receive the correct coordinates after the projection. Is there any way I can work around or fix this without reverting back to cartesian coordinates?

You can find a working example below:

library(shiny)
library(ggplot2)
library(dplyr)

map_world <- function(world, mapPoints, xlim, ylim){
  # function to generate ggplot world map
  ggplot() + 
    geom_polygon(data=world, aes(x=long, y=lat, group=group)) +
    geom_point(data=mapPoints, aes(x=Longitude, y=Latitude), color="red") +
    # coord_map messes up the brush select
     coord_map(xlim=xlim, ylim=ylim)
    # coord_cartesian would work but distort the map
    # coord_cartesian(xlim=xlim, ylim=ylim)
}

mapPoints <- data.frame(Longitude=c(-103, -108, -130, -120),
                      Latitude=c(52, 40, 45, 54))
world <- map_data("world")

ui <- fluidPage(
  fluidRow(
    column(width = 6, 
           plotOutput("plot1", height = 300, 
                      click = "plot1_click", brush = "plot1_brush") )
  ),
  fluidRow(
    column(width = 6, 
           h4("Near points"), verbatimTextOutput("click_info") ),
    column(width = 6, 
           h4("Brushed points"), verbatimTextOutput("brush_info") )
  )
)

server <- function(input, output) {
  # output world map
  output$plot1 <- renderPlot({
    map_world(world = world, mapPoints = mapPoints, 
              xlim=c(-180,180), ylim=c(-90,90))
  })
  # output clicked points 
  output$click_info <- renderPrint({
    nearPoints(mapPoints, xvar="Longitude", 
                  yvar="Latitude", input$plot1_click)
  })
  # output brushed points 
  output$brush_info <- renderPrint({
    brushedPoints(mapPoints, xvar="Longitude", 
                  yvar="Latitude", input$plot1_brush)
  })
}

shinyApp(ui, server)

Thanks!

解决方案

I tried some solutions using ggplot but there is some issue with the scaling that is not being picked up by the plotOutput function. In the help page (http://shiny.rstudio.com/reference/shiny/latest/plotOutput.html), note the following:

For plotOutput, the coordinates will be sent scaled to the data space, if possible. (At the moment, plots generated by base graphics and ggplot2 support this scaling, although plots generated by lattice and others do not.) If scaling is not possible, the raw pixel coordinates will be sent. For imageOutput, the coordinates will be sent in raw pixel coordinates.

also,

With ggplot2 graphics, the code in renderPlot should return a ggplot object; if instead the code prints the ggplot2 object with something like print(p), then the coordinates for interactive graphics will not be properly scaled to the data space.

I tried moving the ggplot to the server portion of the script, but it didn't work.

My proposed solution, use base graphics:

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


mapPoints <- data.frame(Longitude=c(-103, -108, -130, -120),
                        Latitude=c(52, 40, 45, 54))

ui <- fluidPage(
  fluidRow(
    column(width = 6, 
           plotOutput("plot1", height = 300, 
                      click = "plot1_click", brush = "plot1_brush") )
  ),
  fluidRow(
    column(width = 6, 
           h4("Near points"), verbatimTextOutput("click_info") ),
    column(width = 6, 
           h4("Brushed points"), verbatimTextOutput("brush_info") )
  )
)

server <- function(input, output) {
  # output world map
  output$plot1 <- renderPlot({
    map('worldHires')
    points(mapPoints$Longitude, mapPoints$Latitude, 
           col = "red", pch = 16)
  })
  # output clicked points 
  output$click_info <- renderPrint({
    nearPoints(mapPoints, xvar="Longitude", 
               yvar="Latitude", input$plot1_click)
  })
  # output brushed points 
  output$brush_info <- renderPrint({
    brushedPoints(mapPoints, xvar="Longitude", 
                  yvar="Latitude", input$plot1_brush)
  })
}

shinyApp(ui, server)

这篇关于闪亮的点击/画笔不能使用非笛卡尔坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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