Filtering data sets with a slide using plotly in R

Viewed 19

I'm plotting several plots one above the other: first, EEG data (time series); secondly, points at specific peaks of the EEG; thirdly, rectangular areas covering certain portions of the EEG.

Currently, the data used to plot the points and the rectangles is filtered using the alpha and beta arguments in my plotting function; this arguments define sensitivity, so if they are small a lot of points and rectangles appear, and if they are high only few or no points/rectangles make it to the plot.

However, it is slow and annoying to run the function separately at different leves of sensitivity to compare results. So, I desire to have a slide where I can change the alpha and beta arguments live, to get an inmediate view of the resulting change in the plots.

This is my the plotting function (I will highlight with comments the sections relevant to my question):

function(object, channel, alpha = 8, beta = 1) {
        
        # ! Here I filter the @panoms data frame (which
        # will define the points) using the beta parameter.
        points <- object@panoms %>%
            dplyr::filter(variate == channel, strength >= beta)
        point_values <- unlist(object@eeg@data[-1][points$location, channel])
        points <- tibble(A = as_datetime(points$Time), B = point_values)

        # ! Here I filter the @canoms data frame (which
        # will define the rectangles) using the 
        # alpha parameter.
        canoms <- object@canoms %>% dplyr::filter(
            variate == channel,
            mean.change >= alpha
        )

        # Here I create the rectangle areas.
        areas <- tibble(
            xmin = as_datetime(canoms$Time),
            xmax = as_datetime(object@eeg@data$Time[canoms$end]),
            ymin = -300, ymax = 300, alpha = 0.3
        )

        # The plot.channel function plots the EEG (time series).
        eeg <- plot.channel(object@eeg, channel = channel)
        plot <- eeg + # We add the rectangle areas
            geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax,
            frame = ),
                alpha = 0.3, fill = "red",
                data = transform(areas, as.character(1:(nrow(areas)))),
                inherit.aes = FALSE
            ) + # And now we add the points.
            geom_point(
                data = points, aes(A, B),
                inherit.aes = FALSE, color = "red"
            ) +
            xlab("") + ylab(colnames(object@eeg@data[-1][channel]))

        return(plot)
    }

This an example plot resulting from the function. Increasing the value of the arguments would result in smaller (or non-existing) red areas and fewer (or no) red points; decreasing the value of the arguments would result in more or larger red areas, as well as more red points. Again, only as a consequence of the fact that these values are used to filter the databases used to generate the areas and the points.

enter image description here

My question is: how would one go about doing this using Plotly? From what I read, this is the library I should use, but I have zero familiarity with it. Though I found some potentially helpful things about using the frame ggplot aesthetic, this aesthetic plots all values matching some value of the frame, while I want to plot all values higher or equal than certain arguments.

Thanks in advance.

0 Answers
Related