【发布时间】:2016-07-29 20:47:55
【问题描述】:
我正在尝试为直方图创建一个闪亮的应用程序,以显示过去 13 个月的数据。我想我对如何在aes() 中通过input$colname 在ggplot() 中感到震惊
# required library
library(shiny)
library(ggplot2)
library(dplyr)
library(scales)
library(lubridate)
# function to calculate first day of last month;
endF <- function(x) {
as.Date(format(x, "%Y-%m-01"))
}
startF <- function(x) {
as.Date(format(x - months(6), "%Y-%m-01"))
}
ui <- fluidPage(
dateRangeInput(
"Range",
"Date range:",
start = as.date(startF(startF(Sys.Date()))),
end = as.date(endF(endF(Sys.Date()) - 1)),
#format = "mm/dd/yyyy",
separator = " - "
),
plotOutput("hist"))
# Read .csv files
TotalIncident <- read.csv(file = "TotalIncidents.csv", head = TRUE, sep = ",")
# create Range as date
TotalIncident$Range <-as.Date((TotalIncident$DateRange), format = "%Y-%m-%d")
# remove unnecessary value
TotalIncident1 <- TotalIncident[c(-1)]
# rolling data for 13 months;
Finaldata <-reactive({TotalIncident1[TotalIncident1$Range >= input$RangeInput[1] &
TotalIncident1$Range <= input$RangeInput[2],]})
# Define a server for the Shiny app
server <- function(input, output) {
output$hist <- renderPlot({
p <- ggplot(data = Finaldata(),aes(x = Range,
y = Number.of.Incidents)) +
geom_bar(stat = "identity", fill = "blue") +
scale_x_date(
date_breaks = "1 month",
labels = date_format("%b %y"),
expand = c(0, 0)) +
ylab("Total Incidents") + xlab("Mon YY") +
geom_text(aes(label = Number.of.Incidents),
size = 3,hjust = 0.5,vjust = 2) +
#theme for backgroud
theme_bw() +
theme(plot.background = element_blank(),
panel.grid.major = element_blank() ,
panel.grid.minor = element_blank()) +
theme(panel.border = element_blank()) +
theme(axis.line = element_line(color = "black", size = "2"))
})
}
shinyApp(ui = ui, server = server)
【问题讨论】:
-
见how to format your question。另外,请确保您确实提出了问题。不清楚你在问什么。
-
很抱歉,我正在尝试格式化。但一不小心就上线了。