Skip to content

Adding Sliders as Interactive elements in the ggplot2 Graphs in R-instat #10048

@anastasia-mbithe

Description

@anastasia-mbithe

As the suggestion from @rdstern
"Consider adding sliders and other interactive elements to R-Instat ggplot2 graphs. This could be examined generally, as a low-hanging fruit. I note that one method involves using plotly. I suggest we start with that, because we already include plotly in R-Instat. Ideally the work would include a slider for the time series graphs used in e-picsa, so R-Instat can produce something similar."

I looked into this and also found this example that works so well in R Studio,

library(ggplot2)
library(plotly)

# Create a ggplot2 plot with 'cyl' mapped to the frame aesthetic
p <- mtcars |> 
  ggplot(aes(x = wt, y = mpg, frame = cyl)) + 
  geom_point() + 
  labs(title = "Example with mtcars")

# Convert the ggplot2 plot to an interactive plotly object with a slider
ggplotly(p)

So it includes the frame parameter (which should be a variable that can be used as a frame) and passes the object into the ggplotly function
I have tested this in R-instat,

# Dialog: Scatter Plot
survey <- data_book$get_data_frame(data_name="survey")
last_graph <- ggplot2::ggplot(data=survey, mapping=ggplot2::aes(x=field, y=yield, frame= fert)) + ggplot2::geom_point() + theme_grey()
last_graph <- plotly::ggplotly(last_graph)
data_book$add_object(data_name="survey", object_name="last_graph", object_type_label="graph", object_format="image", object=instatExtras::check_graph(graph_object=last_graph))
data_book$get_object_data(data_name="survey", object_name="last_graph", as_file=TRUE)
rm(list=c("last_graph", "survey"))

I've randomly tested in Scatter plot, boxplot, histogram and line plot.
it works for all except histogram which I get this error

Image

The only downside is that the output gets printed/viewed in the browser currently.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions