代码之家  ›  专栏  ›  技术社区  ›  Mariana Oliveira

闪亮的点击/画笔不适用于非笛卡尔坐标?

  •  2
  • Mariana Oliveira  · 技术社区  · 10 年前

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

    如果我使用常规的coord_cartesian坐标系(会扭曲贴图),这是可行的,但如果我使用更合适的坐标系,则会失败 坐标_映射 坐标系。在投影后,单击/笔刷事件似乎没有收到正确的坐标。我有没有办法在不返回笛卡尔坐标的情况下解决这个问题?

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

    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)
    

    谢谢

    1 回复  |  直到 10 年前
        1
  •  1
  •   donlelek    10 年前

    我尝试了一些解决方案,使用 ggplot 但在缩放方面存在一些问题 plotOutput 作用在帮助页面中( http://shiny.rstudio.com/reference/shiny/latest/plotOutput.html ),请注意以下事项:

    绘图输出 ,如果可能,坐标将按比例发送到数据空间。(目前,基本图形和ggplot2生成的绘图支持此缩放,但晶格和其他图形生成的绘图不支持。)如果无法缩放,将发送原始像素坐标。对于 imageOutput ,坐标将以原始像素坐标发送。

    使用ggplot2图形 renderPlot 应返回ggplot对象;如果相反,代码将打印ggplot2对象,如下所示 print(p) ,则交互图形的坐标将无法正确缩放到数据空间。

    我试着移动 gg图 到脚本的服务器部分,但它不起作用。

    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)