闪亮仪表板中的Ggplot

时间:2019-01-27 10:31:58

标签: r shiny

尝试通过FYQuarter和动态图创建带有滑块的仪表板。在有光泽的查询下运行

时出错
  

错误:“”中出现意外的“}”:geom_text(aes(label = Freq),position =   position_dodge(0),vjust = -1)+ theme(panel.grid.major =   element_blank(),panel.grid.minor = element_blank()}}}“”

数据文件2

  structure(list(Quater = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
  3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
  8L, 9L, 9L, 9L, 10L, 10L, 10L), .Label = c("Fy17Q1", "Fy17Q2", 
  "Fy17Q3", "Fy17Q4", "Fy18Q1", "Fy18Q2", "Fy18Q3", "Fy18Q4", "Fy19Q1", 
  "Fy19Q2"), class = "factor"), RiskTierDesc = structure(c(1L, 
  2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
  3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Above Normal", 
  "High", "Normal"), class = "factor"), Freq = c(519L, 63L, 1514L, 
  563L, 87L, 1662L, 643L, 81L, 1618L, 562L, 69L, 1524L, 555L, 61L, 
  1609L, 622L, 52L, 2090L, 800L, 86L, 2052L, 681L, 66L, 1811L, 
  622L, 57L, 2317L, 344L, 14L, 1537L)), .Names = c("Quater", "RiskTierDesc", 
  "Freq"), class = "data.frame", row.names = c(NA, -30L))
library(shiny)
library(shinydashboard)
library(ggplot2)
library(shinyWidgets)
ui <- dashboardPage(
 dashboardHeader(title = "Basic Dashboard"),

 dashboardSidebar(
   sidebarMenu(sliderTextInput("Quater","Select Quarter:" ,
                                              choices = File2$Quater,
                                                          selected = File2$Quater, #values which will be selected by default
                                                          animate = FALSE, grid = FALSE,
                                                          hide_min_max = FALSE, from_fixed = FALSE,
                                                          to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
                                                          to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
                                                          post = NULL, dragRange = TRUE))),

 dashboardBody(

   fluidRow( 
     box(
       title = "RiskTier Vs Quater"
       ,status = "primary"
       ,solidHeader = TRUE 
       ,collapsible = TRUE 
       ,plotOutput("k", height = "300px")
     ))))                  




Server<- function(input, output){
 dataset <- reactive({
   File2[(File2$Quater, input$Quater),]
 })
 output$k<- renderPlot({
 ggplot(dataset, aes(x=Quater, y=Freq , group=RiskTierDesc, colour=RiskTierDesc)) + 
 geom_line(aes(size=RiskTierDesc)) +
 geom_point() +
 scale_color_manual(values=c("red","orange","green")) +
 scale_size_manual(values=c(1,1,1)) +
 labs(title ="RiskTier Vs Quater", x = "Quarter", y = "Frequency") +
 geom_text(aes(label = Freq), position = position_dodge(0),vjust = -1) +
 theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())})}


shinyApp(ui, Server)

请帮助提出一些建议

1 个答案:

答案 0 :(得分:0)

我希望这会有所帮助,这就是您要寻找的,如果需要改进,请告诉我。

致谢/ Revanth Nemani

# Making Data Frame------------
File2 <- structure(list(Quater = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
                                             3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
                                             8L, 9L, 9L, 9L, 10L, 10L, 10L), .Label = c("Fy17Q1", "Fy17Q2", 
                                                                                        "Fy17Q3", "Fy17Q4", "Fy18Q1", "Fy18Q2", "Fy18Q3", "Fy18Q4", "Fy19Q1", 
                                                                                        "Fy19Q2"), class = "factor"), RiskTierDesc = structure(c(1L, 
                                                                                                                                                 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
                                                                                                                                                 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Above Normal", 
                                                                                                                                                                                                                 "High", "Normal"), class = "factor"), Freq = c(519L, 63L, 1514L, 
                                                                                                                                                                                                                                                                563L, 87L, 1662L, 643L, 81L, 1618L, 562L, 69L, 1524L, 555L, 61L, 
                                                                                                                                                                                                                                                                1609L, 622L, 52L, 2090L, 800L, 86L, 2052L, 681L, 66L, 1811L, 
                                                                                                                                                                                                                                                                622L, 57L, 2317L, 344L, 14L, 1537L)), .Names = c("Quater", "RiskTierDesc", 
                                                                                                                                                                                                                                                                                                                 "Freq"), class = "data.frame", row.names = c(NA, -30L))

# Needed Libraries-------------------
library(shinydashboard)
library(shiny)
library(shinyWidgets)
library(ggplot2)
library(dplyr)

#UI---------------------------
ui <- dashboardPage(
  #Header
  dashboardHeader(title = "Basic Dashboard"),
  #Sidebar
  dashboardSidebar(
    sidebarMenu(sliderTextInput("Quater","Select Quarter:" ,
                                choices = unique(File2$Quater),#To not repeat values in the slidertextinput if the values are not sorted
                                selected = unique(File2$Quater), #values which will be selected by default
                                animate = FALSE, grid = FALSE,
                                hide_min_max = FALSE, from_fixed = FALSE,
                                to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
                                to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
                                post = NULL, dragRange = TRUE))),
  #Body
  dashboardBody(

    fluidRow( 
      box(
        title = "RiskTier Vs Quater"
        ,status = "primary"
        ,solidHeader = TRUE 
        ,collapsible = TRUE 
        ,plotOutput("k", height = "300px")
      ))))                  

#Server Function------------------
Server<- function(input, output){
  #React to the slider input
  dataset <- reactive({
    File2 <- File2 %>% filter(Quater %in% input$Quater) #use == instead of %in% if you need only a single quater data
    return(File2)
  })
  output$k<- renderPlot({
    ggplot(dataset(), #You forgot to add the "()" next to dataset. Remember all reactive objects are functions
           aes(x=Quater, y=Freq, group=RiskTierDesc, colour=RiskTierDesc)) + 
      geom_line(aes(size=RiskTierDesc)) +
      geom_point() +
      scale_color_manual(values=c("red","orange","green")) +
      scale_size_manual(values=c(1,1,1)) +
      labs(title ="RiskTier Vs Quater", x = "Quarter", y = "Frequency") +
      geom_text(aes(label = Freq), position = position_dodge(0),vjust = -1) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
    })
  }
#App--------------------------------------
shinyApp(ui, Server)