【问题标题】:Subset Dataframe and plot with ggplot? [duplicate]使用ggplot子集数据框和绘图? [复制]
【发布时间】:2018-11-24 04:47:38
【问题描述】:

我创建了一个闪亮的应用程序,需要一些关于我的数据子集的帮助。我插入了一个dateRangeInput,客户端可以在其中过滤开始日期和结束日期。将该过滤器包含在我的GGPOT代码中,以便在选择不同的日期时,绘图始终会自动更改。我的问题是它没有根据所选日期进行过滤,partC 的数据。问题是这行代码:geom_line(aes(x = Date, y = OLS.Data[partC]), color="red")partC 是一个连接到选择输入以访问我的数据框的变量。示例:客户端选择input1 = Informedinput2 = FullpartC 使InformedFull(这是我的数据集的一列的名称)等等。所以partC 只是两个输入的连接器,这是我的问题。如果我将这段代码(例如geom_line(aes(x = Date, y = InformedFull), color="red"))放入我的 geom_line,则上面的一切都可以完美运行,但我需要它与 partC。

这是我的 ui.R 代码(仅必要部分):

        box(
          title = "Controls-0", 
          status = "primary", 
          solidHeader = TRUE,
          width = 3,
          height = 142,
          dateRangeInput("daterange", "SELECT DATE:", start = min(OLS.Data$Date), end = max(OLS.Data$Date))
        ), 

            box(
              title = "Investor Control", 
              status = "primary", 
              solidHeader = TRUE,
              width = 3,
              selectInput("investor", label="Select Investor", choices = list("Informed" = "Informed", "Noise" = "Noise"), selected = "Informed")
            ),

            box(
              title = "Category Control", 
              status = "primary", 
              solidHeader = TRUE,
              width = 3,
              selectInput("category", label="Select Category", choices = list("Full" = "Full", "Fact" = "Fact", "Fact Positive" = "Fact.Pos", "Fact Negative" = "Fact.Neg", "Emotions" = "Emotions", "Emotions Fact" = "EmotionsFact"), selected = "Full")
            ),

用 ggplot 更新 server.R:

server <- function(input, output) {

  partC = NULL

  makeReactiveBinding("partC")


  observeEvent(input$investor, { 
    partA<<-input$investor
    partA<<-as.character(partA)
  })

  observeEvent(input$category, { 
    partB<<-input$category
    partB<<-as.character(partB)
  })

  OLS.Data$InformedEmotionsFact <- as.numeric(as.character(OLS.Data$InformedEmotionsFact))
  OLS.Data$NoiseEmotionsFact <- as.numeric(as.character(OLS.Data$NoiseEmotionsFact))

  output$myPlotVisu <- renderPlot({
    partC<-as.character(paste(partA,partB,sep=""))

    OLS.Data %>%
      select(partC, NYSE,Date,Sector) %>%
      filter(Date >= input$daterange[1], Date <= input$daterange[2]) %>%
      ggplot(aes(x = Date, y = NYSE)) +
      geom_line() +
      ggtitle(paste(input$investor,input$category,sep = "")) +
      theme(plot.title = element_text(hjust = 0.5,face="bold")) +
      labs(x="Time",y="Return S&P500") +
      geom_line(aes(x = Date, y = OLS.Data[partC]), color="red")
  })

【问题讨论】:

    标签: r ggplot2 shiny subset


    【解决方案1】:

    我不知道你为什么将partA/partB分配给全局环境,甚至两次。你不需要这样做。我创建了一个 reactiveValues 对象,用于存储值(partA、partB 和 partC)。然后,您可以在应用程序中的任何位置使用它们。

    也许下面的例子会对你的代码有所帮助。我为它创建了一些虚拟数据。

    library(shiny)
    library(shinydashboard)
    library(ggplot2)
    
    ## DATA #######################
    DateSeq = seq(as.Date("1910/1/1"), as.Date("1911/1/1"), "days")
    
    OLS.Data = data.frame(
      ID = 1:length(DateSeq),
      Date = DateSeq,
      NoiseEmotionsFact = sample(1:100,length(DateSeq), T),
      InformedEmotionsFact = sample(100:1000,length(DateSeq), T),
      InformedFull = sample(10:1000,length(DateSeq), T),
      NoiseFull = sample(50:5000,length(DateSeq), T),
      NoiseFact = sample(1:15,length(DateSeq), T),  
      NoiseFact.Pos = sample(100:110,length(DateSeq), T),
      NoiseFact.Pos = sample(10:200,length(DateSeq), T)
    )
    
    
    ## UI #######################
    ui <- {dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody(
        plotOutput("myPlot"),
        box(
          title = "Controls-0", 
          status = "primary", 
          solidHeader = TRUE,
          width = 3,
          height = 142,
          dateRangeInput("daterange", "SELECT DATE:", start = min(OLS.Data$Date), end = max(OLS.Data$Date))
        ),
        box(
          title = "Alpha",
          sliderInput("alphaVisu", label = "Alpha :", min = 0, max = 1, value = 0.4, step = 0.1)
        ),
    
        box(
          title = "Investor Control", 
          status = "primary", 
          solidHeader = TRUE,
          width = 3,
          selectInput("investor", label="Select Investor", 
                      choices = list("Informed" = "Informed", "Noise" = "Noise"), selected = "Informed")
        ),
    
        box(
          title = "Category Control", 
          status = "primary", 
          solidHeader = TRUE,
          width = 3,
          selectInput("category", label="Select Category", 
                      choices = list("Full" = "Full", "Fact" = "Fact", "Fact Positive" = "Fact.Pos", 
                                     "Fact Negative" = "Fact.Neg", "Emotions" = "Emotions", 
                                     "Emotions Fact" = "EmotionsFact"), selected = "Full")
        )
      )
    )}
    
    ## SERVER #######################
    server <- function(input, output) {
    
      ## Reactive Values ############
      parts <- reactiveValues(partA=NULL, partB=NULL, partC=NULL)
    
      ## Observe Events ############
      observeEvent(input$investor, { 
        parts$partA <- as.character(input$investor)
      })
      observeEvent(input$category, { 
        parts$partB <- as.character(input$category)
      })
    
      ## Plot ############
      output$myPlot <- renderPlot({
    
        parts$partC <- as.character(paste(parts$partA, parts$partB,sep=""))
    
        OLS.Data.filtered <-  OLS.Data %>%
          filter(Date >= input$daterange[1], Date <= input$daterange[2])
    
        req(OLS.Data.filtered)
    
        OLS.Data.filtered %>% 
          ggplot(aes(x = Date, y = ID)) +
          geom_line() +
          ggtitle(paste("input$investor","input$category",sep = "")) +
          theme(plot.title = element_text(hjust = 0.5,face="bold")) +
          labs(x="Time",y="Return S&P500") +
    
          geom_line(aes(x = Date, y = OLS.Data.filtered[parts$partC]), color="red", 
                    alpha = rep(as.numeric(input$alphaVisu), nrow(OLS.Data.filtered[parts$partC])))
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 世嘉人,你真是个天才!兄弟非常感谢您的帮助!很完美,我在 3 天里一直在解决这个问题。非常感谢!!!
    • 另一个快速问题。我正在尝试在我的 ggplot 中添加一个 alpha,客户可以在其中使用线条的透明度。如果我把这个+ geom_line(alpha = input$alphaVisu) 我得到这个错误:Error in : Aesthetics must be either length 1 or the same as the data (73986): alpha
    • 世嘉有什么想法吗??这是我需要修复的最后一个想法!
    • 我编辑了我的答案。由于 sliderInput 只为 alphaVisu 创建了 1 个值,所以我只是将 geom_line 中的值重复为 OLS.Data.filtered[parts$partC] 中的行数。
    • 世嘉上帝保佑你兄弟!简直完美!!!!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2014-11-21
    • 2021-01-22
    相关资源
    最近更新 更多