Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Created April 1, 2020 08:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aagarw30/1a86725add60b9dae42fbb44b8e94817 to your computer and use it in GitHub Desktop.
Save aagarw30/1a86725add60b9dae42fbb44b8e94817 to your computer and use it in GitHub Desktop.
Interactive data point selection with ggplotly/plotly charts
## Load required packages
library(plotly)
library(ggplot2)
library(shiny)
## Defining a key column in mtcars which will be used for event handling in event_data()
mtcars$key <- row.names(mtcars)
### Ui code begins below
ui <- fluidPage(
h1("Demo - Interactive data point selection with ggplotly/plotly charts"),
h4("Subset the dataset using the data points selected from the chart. Drag and select one or multi data points"),
br(),
## Plotly plot display
plotlyOutput("plot"),
## Data point information display post click
verbatimTextOutput("click")
)
## Server side code begins below
server <- function(input, output, session) {
## ggplotly scatter plot
output$plot <- renderPlotly({
myplot <- ggplot(mtcars, aes(x = mpg,
y = wt,
key = key)) + geom_point()
## in above code line, use the argument key inside ggplot which will be used for event handling
ggplotly(myplot) %>%
layout(dragmode = "select")
})
## returns the data related to data points selected by the user
output$click <- renderPrint({
## Click_data will have the keys (row identifiers) corresponding to selected data points
click_data <- event_data("plotly_selected")
## Event mode options. There are many more to experiment
## plotly_click - click on one data point
## Plotly_selected - multi point select
if(is.null(click_data))
"No data points selected on scatter plot..."
else
filter(mtcars, key %in% click_data$key) %>% select(-key)
## Subsetting in above step based on selected data points and removing the key column
})
}
shinyApp(ui, server)
@LicanMartinez
Copy link

Do yo know why the data.frame returned by event_data() only accepts a new "key" column, but no columns with other names? I tried using "id" instead of "key", and struggled a lot to realize that the problem behind the new variable not being added to the data.frame was that column name restriction.

@aagarw30
Copy link
Author

Hello,
event_data return a key argument. That can't be changed, however you should be able to define whatever that key should should be from your data frame. Are you saying you encountered errors when you defined some other column name in the ggplot( .... key = "someothername")?

@LicanMartinez
Copy link

I didn't know key was an argument. I thought it was just a variable you were creating inside aes() that would be fetched by event_data. Similar to adding variables in aes() to be displayed in the tooltip of plotly.
Thanks for clarifying!

@aagarw30
Copy link
Author

Sure, thanks. Happy to help. ty Abi.

@ZeroLi-Bio
Copy link

ZeroLi-Bio commented Jul 7, 2023

Hello,
Thanks for sharing this useful scripts.
Now I can use your scripts to select points and copy the key from the window and paste to variable later. But I've a minor question which is how can i output the key into an R environment variable directly? So that I can use downstream scripts for those selected points.
I tried adding a line below line 54 savekey <- click_data$key ,but after running it the savekey doesn't appear in variables.
Do you have any suggestions? Thanks in advance! @aagarw30

@aagarw30
Copy link
Author

aagarw30 commented Jul 10, 2023 via email

@ZeroLi-Bio
Copy link

Yes, I need to catch the key and use the key to subset points from the whole points.

@aagarw30
Copy link
Author

aagarw30 commented Jul 10, 2023 via email

@ZeroLi-Bio
Copy link

Hello, I figured out a way and forked your gist.
Thank you!

@aagarw30
Copy link
Author

aagarw30 commented Jul 11, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment