首页 > 解决方案 > 在闪亮的热图上显示不同的类别

问题描述

我创建了一个闪亮的应用程序,用户可以在复选框中选择一些值并设置日期范围。我已经创建了 ui,但绘图在主函数中不起作用。

我闪亮的应用程序:

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

bbox = c(left=-95.8, bottom=29.4, right=-95.0, top=30.0)
map <- get_stamenmap(bbox, zoom = 10, source="stamen")

ui <- fluidPage(
  titlePanel("Location"),
  sidebarPanel('Choose the type of offense to show in plot:',
               checkboxGroupInput("offense", label = "Type of offenses...",
                                  choices = list("Murder" = 'murder',
                                                 "Robbery" = 'robbery',
                                                 "Aggravated assault" = 'aggravated assault',
                                                 "Burglary" = 'burglary',
                                                 "Rape" = 'rape'),
                                  selected = 'Murder'),
               dateRangeInput("date", "Date range:",
                              start  = "2010-01-01",
                              end    = "2010-12-08",
                              min    = "2010-01-01",
                              max    = "2010-12-08",
                              format = "yyyy/mm/dd",
                              separator = " - ")
  ),
  mainPanel('Performance Plot',
            plotOutput('myplot')),
  position = 'left')



server <- function(input, output){
  output$myplot <- renderPlot(
    {
      req(input$type)
      data <- data_4 %>% filter(offense %in% input$offense)
      date <- data_4 %>% filter(offense %in% input$date)
      ggmap(map) + stat_density2d(data = subset(data,offense == "offense" & date == "date"),
                                  aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), geom = 'polygon') +
        scale_fill_gradient(low = "green", high = "red") +
        scale_alpha_continuous(range = c(0, 0.8)) +
        geom_point(data = subset(data, offense == input$offense),
                   aes(x = lon, y = lat), size = 0.5) + 
        guides(fill = FALSE, alpha = FALSE) +
        ggtitle('Crime ')
      
    }
  )
  
}
shinyApp(ui = ui, server = server)

我不确定哪里出了问题,但我想是服务器功能内的 ggplot 。我能够在闪亮之外创建情节。但不确定如何整理日期范围和复选框选项。任何人都可以帮忙吗?我想在主面板中创建多个绘图,因为用户可以从复选框中选择多个值。但我不知道应该把 grid.arrange 函数放在哪里。

标签: rggplot2shinyggmap

解决方案


您的代码有几个问题:

  1. 你使用req(input$type)which should be req(input$offense)。这就是为什么在运行代码时没有显示图(或者更确切地说没有错误消息)的原因

  2. 第二个问题是过滤数据。Noz 确定您的想法……但是要过滤您可以使用的数据subset(data_4, offense %in% input$offense & date >= input$date[[1]] & date <= input$date[[2]])

完整的可重现示例

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

bbox = c(left=-95.8, bottom=29.4, right=-95.0, top=30.0)
map <- get_stamenmap(bbox, zoom = 10, source="stamen")

ui <- fluidPage(
  titlePanel("Crime Location"),
  sidebarPanel('Choose the type of offense to show in plot:',
               checkboxGroupInput("offense", label = "Type of offenses...",
                                  choices = list("Murder" = 'murder',
                                                 "Robbery" = 'robbery',
                                                 "Aggravated assault" = 'aggravated assault',
                                                 "Burglary" = 'burglary',
                                                 "Rape" = 'rape'),
                                  selected = 'Murder'),
               dateRangeInput("date", "Date range:",
                              start  = "2010-01-01",
                              end    = "2010-12-08",
                              min    = "2010-01-01",
                              max    = "2010-12-08",
                              format = "yyyy/mm/dd",
                              separator = " - ")
  ),
  mainPanel('Performance Plot',
            plotOutput('myplot')),
  position = 'left'
)


server <- function(input, output){
  output$myplot <- renderPlot(
    {
      req(input$offense)

      ggmap(map) + stat_density2d(data = subset(data_4, offense %in% input$offense & date >= input$date[[1]] & date <= input$date[[2]]),
                                  aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), geom = 'polygon') +
        scale_fill_gradient(low = "green", high = "red") +
        scale_alpha_continuous(range = c(0, 0.8)) +
        geom_point(data = subset(data_4, offense %in% input$offense),
                   aes(x = lon, y = lat), size = 0.5) + 
        guides(fill = FALSE, alpha = FALSE) +
        ggtitle('Crime in Houston TX')
      
    }
  )
  
}
shinyApp(ui = ui, server = server)

在此处输入图像描述


推荐阅读