Created
April 1, 2020 08:00
-
-
Save aagarw30/1a86725add60b9dae42fbb44b8e94817 to your computer and use it in GitHub Desktop.
Interactive data point selection with ggplotly/plotly charts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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) |
Ok, will work on it today and update . Thank you!
…On Mon, 10 Jul 2023 at 10:53 AM, ZYLI ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Yes, I need to catch the key and use the key to subset points from the
whole points.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/aagarw30/1a86725add60b9dae42fbb44b8e94817#gistcomment-4623981>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABXF2W2YCU7RLQX4XIF6MITXPOGURBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAMRRGE3DKMZZU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Hello, I figured out a way and forked your gist.
Thank you!
Good news!
Ty Abi.
…On Tue, 11 Jul 2023 at 1:31 PM, ZYLI ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hello, I figured out a way and forked your gist.
Thank you!
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/aagarw30/1a86725add60b9dae42fbb44b8e94817#gistcomment-4625277>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABXF2W3INNAOBSZ4LBD7R6TXPUB6DBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAMRRGE3DKMZZU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, I need to catch the key and use the key to subset points from the whole points.