Ich versuche, eine glänzende App für Histogramm erstellen, um die letzten 13 Monate der Daten zu zeigen. Ich glaube, ich bin geschlagen, wie input$colname
in aes()
passiert in ggplot()
Erstellen Shiny App für das Rolling 13 Monate
# 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)
See [wie Sie Ihre Frage zu formatieren] (http://stackoverflow.com/help/formatting). Stellen Sie außerdem sicher, dass Sie tatsächlich eine Frage stellen. Es ist nicht klar, was Sie fragen. – MrFlick
Es tut mir leid, ich versuche zu formatieren. aber versehentlich ging es online. – user2612244