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 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) |
Sure, thanks. Happy to help. ty Abi.
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
Hello, so you just need to catch and use the value of key?
Ty Abi
…On Mon, 10 Jul 2023 at 6:59 AM, ZYLI ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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
<https://github.com/aagarw30>
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/aagarw30/1a86725add60b9dae42fbb44b8e94817#gistcomment-4621867>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABXF2WYVXBV2UVB3DE3BFWTXPNLGLBFKMF2HI4TJMJ2XIZLTSGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAMRRGE3DKMZZU52HE2LHM5SXFJTVOBSGC5DF>
.
You are receiving this email because you were mentioned.
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>
.
Yes, I need to catch the key and use the key to subset points from the whole points.
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
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!