Issue using sliders to narrow data selection

Hello!

I am working on a research project right now and neither the professor I work with or myself knows very much about coding in R. We have been trying to figure out how to compare two segments of data we have collected. We want to use sliders to narrow our data set. I have attached the R code and csv file, but I will talk more specifically about our issue and my process.

code
data

  1. I make a summary data set using information from the CSV I attached. This involves separating the data into segments that are a result of our collection method
  2. I use two sliders to select the ranges we want to do analysis with (filtered1.data() and filtered1.data())

#Range 1
sliderMin1 <- reactive ({ input$sliderRange1[1] })
sliderMax1 <- reactive ({ input$sliderRange1[2] })

#Range 2
sliderMin2 <- reactive ({ input$sliderRange2[1] })
sliderMax2 <- reactive ({ input$sliderRange2[2] })

#Filter data
filtered1.data <- reactive({ #reactive object updates every time the user changes the slider
req(input$sliderRange1)
filter(final.data(), seg2()$Number>input$sliderRange1[1] & seg2()$Number<input$sliderRange1[2])
})

filtered2.data <- reactive({ #reactive object updates every ti

me the user changes the slider
req(input$sliderRange2)
filter(final.data(), seg2()$Number>input$sliderRange2[1] & seg2()$Number<input$sliderRange2[2])
})

  1. I use the new data set to plot the specific range but I get this error that I cannot get rid of!

#Code for L1_1 plot
output$L1_1 <- renderPlot({
ggplot(filtered1.data())+
geom_point(aes(x = seg2()$Number, y = seg2()$L1), color = "blue")+
geom_smooth(aes(x = seg2()$Number, y = seg2()$L1), method = "lm", se=FALSE, color="black", formula =y ~ x)+
labs(title = "L1 vs. Number for region 1",
x = "Number",
y = "L1")+
#edit text sizes for title and axes labels
theme(plot.title = element_text(size=24,face="bold"),
axis.title.x = element_text(size=14),
axis.title.y = element_text(size=14))
})

Aesthetics must be either length 1 or the same as the data (603): x and y

Right now I am just trying to get this method to work for one plot. I would really appreciate some guidance on how I could get this to work.

Best :slight_smile:

Welcome to the community @defenestrater! I think I found what is causing the issues. First, with the filtered data sets, the filter is referencing final.data() but filtering based on fields from seg2(). If seg2() is the data you want to filter (which I believe it is because it contains Number), then the following update will allow the range filters to work.

#Filter data
  filtered1.data <- reactive({ #reactive object updates every time the user changes the slider
    req(input$sliderRange1)
    filter(seg2(), Number > input$sliderRange1[1] & Number < input$sliderRange1[2])
  })
  
  filtered2.data <- reactive({ #reactive object updates every time the user changes the slider
    req(input$sliderRange2)
    filter(seg2(), Number > input$sliderRange2[1] & Number < input$sliderRange2[2])
  })

Then, with plot L1_1, a similar thing was happening by referencing filtered1.data() in ggplot() but then specifying seg2() fields in the geoms. This was causing the length error mismatch you were seeing. Therefore, I updated this section to the following:

#Code for L1_1 plot
  output$L1_1 <- renderPlot({
    ggplot(filtered1.data())+
      geom_point(aes(x = Number, y = L1), color = "blue")+
      geom_smooth(aes(x = Number, y = L1), method = "lm", se=FALSE, color="black", formula =y ~ x)+
      labs(title = "L1 vs. Number for region 1",
           x = "Number",
           y = "L1")+
      #edit text sizes for title and axes labels
      theme(plot.title = element_text(size=24,face="bold"),
            axis.title.x = element_text(size=14),
            axis.title.y = element_text(size=14))
  })

Making these updates resulted in the following (filtered to 10 - 502 for range 1). It appears similar updates to these can be done for the other plots as well. Hope this helps.

1 Like

That worked perfectly! Thank you :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.