基于Shiny输入+纬度和经度半径的子集df

时间:2019-10-17 16:02:13

标签: r ggplot2 shiny plotly

我正在尝试构建一个闪亮的应用程序,该应用程序将使用两个不同的时间序列渲染geom_line图,其中之一是我的公寓的价格可以从SelectInput中进行选择,图中的第二行应该是公寓的价格其中lat和lng应该从第一个输入开始在new_latitude和new_longitude之间,并按天汇总,以便最终结果只是一条连续的线。在此图中,x是一年中的天,y是平均价格。输入的内容只能是我的公寓

这是我的df head()

df<-structure(list(unitcode = c("23986", "23986", "23986", "23986", 
"23986", "23986"), lat = c(45.44991, 45.44991, 45.44991, 45.44991, 
45.44991, 45.44991), lng = c(9.17597, 9.17597, 9.17597, 9.17597, 
9.17597, 9.17597), bedrooms = c(1L, 1L, 1L, 1L, 1L, 1L), maxoccupancytotal = c(4L, 
4L, 4L, 4L, 4L, 4L), day_of_year = c(55L, 106L, 107L, 108L, 109L, 
110L), avg_price = c(190L, 187L, 187L, 187L, 187L, 187L), past_future = c("past", 
"past", "past", "past", "future", "future"), new_latitude = c(49.9415829000532, 
49.9415829000532, 49.9415829000532, 49.9415829000532, 49.9415829000532, 
49.9415829000532), new_longitude = c(15.578627910762, 15.578627910762, 
15.578627910762, 15.578627910762, 15.578627910762, 15.578627910762
)), row.names = c(NA, 6L), class = "data.frame")

到目前为止,这是我所得到的,我错过了基于lat和lng进行汇总和过滤的全部内容,因为坦率地说,我对R相对陌生,对于Shiny则完全陌生,甚至不知道在哪里开始

ui <- fluidPage(theme = shinytheme("darkly"),

#Header
h1("Cohort Health Check", align = "center"),

selectInput(
    inputId = "unitcode",
    label = "Select unit",
    choices = unique(df$unitcode),
    selected = "23986"
  ),


plotlyOutput("plot", height = "650px")
)

server <- function(input, output, session) {

  output$plot <- renderPlotly({
    df%>%
      filter(unitcode == input$unitcode) %>%
      ggplot(aes(day_of_year, avg_price)) +
      geom_line(aes(color = past_future))+ 
      scale_y_continuous(breaks = seq(0,2000,25),labels = dollar)+
      scale_x_continuous(breaks = seq(0,365,7))+
      scale_color_manual(values=c( "black", "red"))+
      theme_economist()
  })

}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

您必须在ggplotly之前添加ggplot函数,因为要使用plotlyOutput进行渲染:

ui <- fluidPage(theme = shinytheme("darkly"),

                #Header
                h1("Cohort Health Check", align = "center"),

                selectInput(
                  inputId = "unitcode",
                  label = "Select unit",
                  choices = unique(df$unitcode),
                  selected = "23986"
                ),


                plotlyOutput("plot", height = "650px")
)

server <- function(input, output, session) {

  output$plot <- renderPlotly({
    ggplotly( df%>%
      filter(unitcode == input$unitcode) %>%
      ggplot(aes(day_of_year, avg_price, group = past_future)) +
      geom_line(aes(color = past_future))+ 
      scale_y_continuous(breaks = seq(0,200,1))+
      scale_x_continuous(breaks = seq(0,365,7))+
      scale_color_manual(values=c( "black", "red"))+
      theme_economist() )
  })

}

shinyApp(ui, server)

enter image description here

祝您有光泽! :D