【问题标题】:Plot points (latitude & Longitude) into a ggmap将点(纬度和经度)绘制到ggmap中
【发布时间】:2023-04-02 11:30:02
【问题描述】:

我正在尝试制作一个闪亮的应用程序,它的一部分是一张应该将纬度和经度点打印到地图中的地图。我一直在尝试这样做,但我收到一条错误消息,告诉我它找不到我的对象 d。

如果我只是把地图效果很好,没有积分但这是一个步骤。

我的 server.R 代码是:

#Reactive Map
  output$MapPr <- renderPlot({
    d <- switch(input$chDatabase,
                "BPD 2013 Baltimore" = read.csv("./Data/BPD_13_Bal.csv", 
                                                header=TRUE, sep=",", dec="."),
                "BPD 2014 Baltimore" = read.csv("./Data/BPD_14_Bal.csv", 
                                                header=TRUE, sep=",", dec=".")
    )
    library(ggmap)
    map <- get_map(location = 'Baltimore', zoom = 12)
    ggmap(map)
    ggmap(map) +
      geom_point(aes(as.numeric(d$Longitude), as.numeric(d$Latitude)), data = d, alpha =.5, color = "darkred")
  }, width = 800, height = 700)

在 ui.R 我有:

################################
#2nd tabpanel for Reactive Map
tabPanel("Reactive Map", 

  #SideBarLayout for sidebar Panel for the options of the map       
  sidebarLayout(

    #SideBar Panel with options to adjust the map
    sidebarPanel(

      #Databases selection
      selectInput("chDatabaseMap","Choose DataBase:", 
          choices = c("BPD 2013 Baltimore", "BPD 2014 Baltimore"))
    ),
    ###################################       
    #Main panel to put plots  
    mainPanel(
      plotOutput("MapPr")
    )
  )
)

顺便说一句,我已经看到这是 csv 文件的加载问题,或者至少我认为是这样,但是我一直在使用之前的图(直方图、饼图、箱线图等)相同的系统,它们可以工作。

我不知道该如何继续。

纬度和经度列都是数字。

【问题讨论】:

    标签: r shiny coordinates ggmap


    【解决方案1】:

    将 server.R 更改为以下是否有效?

    library(ggmap)
    
    d <- reactive({
        switch(input$chDatabase,
               "BPD 2013 Baltimore" = read.csv("./Data/BPD_13_Bal.csv", 
                                               header=TRUE, sep=",", dec="."),
               "BPD 2014 Baltimore" = read.csv("./Data/BPD_14_Bal.csv", 
                                               header=TRUE, sep=",", dec="."))
    })
    
    
    
    output$MapPr <- renderPlot({
        df <- d()
        map <- get_map(location = 'Baltimore', zoom = 12)
        ggmap(map) +
            geom_point(aes(as.numeric(Longitude), 
                           as.numeric(Latitude)), 
                       data = df, alpha =.5, color = "darkred")
    }, width = 800, height = 700)
    

    【讨论】:

    • 现在我又来了一个;警告:$ 中的错误:“闭包”类型的对象不是子集
    • 我的错。你现在可以检查吗?不应该在aes 中使用$
    • 是的!它现在可以工作了:D 非常感谢@Sumedh!我还意识到我的 csv 文件中存在另一个错误。
    猜你喜欢
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 2015-02-27
    • 1970-01-01
    相关资源
    最近更新 更多