在闪亮的热图上显示不同的类别
Show different categories on a heatmap in shiny
我创建了一个闪亮的应用程序,用户可以在复选框中选择一些值并设置日期范围。
我已经创建了 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 函数放在哪里。
您的代码有几个问题:
你用的是req(input$type)
,应该是req(input$offense)
。这就是为什么当 运行 您的代码
时没有显示任何情节(或者更确切地说没有错误消息)的原因
第二个问题是数据的过滤。 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)
我创建了一个闪亮的应用程序,用户可以在复选框中选择一些值并设置日期范围。 我已经创建了 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 函数放在哪里。
您的代码有几个问题:
你用的是
时没有显示任何情节(或者更确切地说没有错误消息)的原因req(input$type)
,应该是req(input$offense)
。这就是为什么当 运行 您的代码第二个问题是数据的过滤。 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)