当在Shiny中使用色标时,防止使用ggplot绘制多条曲线

时间:2018-01-02 13:25:34

标签: r ggplot2 shiny

在我的下面的代码中,我有一个复选框,在选中时使用geom_smooth在数据上绘制曲线。但是,我还有一个第二个复选框,在检查时引入了着色美学,并将一些数据的颜色设置为红色和一些黑色。问题是,当检查两个方框时,我得到两条曲线,一条用于黑色数据,一条用于红色,但希望曲线始终保持为“总”曲线。

library(ggplot2)
library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  titlePanel("Transfers Analysis App"),

  sidebarLayout(
    sidebarPanel(
      pickerInput(inputId = "Day", 
                  label = "Days of Week", 
                  choices = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"), selected = data$day[data$day %in% c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')],
                  options = list(`actions-box` = TRUE), 
                  multiple = T),
      pickerInput(inputId = "Month", 
                  label = "Months", 
                  choices = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"), selected = data$month[data$month %in% c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')],
                  options = list(`actions-box` = TRUE), 
                  multiple = T),
      checkboxInput("Outage", "Highlight Days when an Outage Occured", FALSE),
      checkboxInput("Curve", "Curve of Best Fit", FALSE)),

    mainPanel(
      plotOutput("plot1", height = "600px", width = "100%",
                 hover = hoverOpts(id = "plot_hover")),
      verbatimTextOutput("hover_info")

    )))


server <- function(input, output) {

  output$plot1 <- renderPlot({
    Day <- input$Day
    Month <- input$Month
    Outage <- input$Outage
    COBF <- input$Curve
    data <- data[data$day %in% input$Day]
    data <- data[data$month %in% input$Month]

    g <- ggplot(data, aes(Date, NUMBER_OF_TRANSFERS)) + geom_point() 

    if (COBF == TRUE)
      g <- g + geom_smooth()

    if (Outage == TRUE)
      g <- g + aes(colour = Incident) + scale_colour_manual(values=c( "red", "black"))

    #if(Outage == TRUE)
     # ggplot(data, aes(Date, NUMBER_OF_TRANSFERS, colour = Incident)) + geom_point() + scale_colour_manual(values=c( "red", "black")) 

    #else
     # ggplot(data, aes(Date, NUMBER_OF_TRANSFERS)) + geom_point() 
    #points(data$Date[data$Quantity == "1"],data$NUMBER_OF_TRANSFERS[data$Quantity == "1"], col='red')

    plot(g)

  })
}

shinyApp(ui, server)

0 个答案:

没有答案