Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

On_click - change the point color


How can I change the color of the selected points in scatter plot?

For example if I use this code, and the user chooses multiple points, I want to change the points that the user chose to another color and by another click on point to get the previous color:


ui <- fluidPage(
radioButtons(“plotType”, “Plot Type:”, choices = c(“ggplotly”, “plotly”)),

server <- function(input, output, session) {

output$plot <- renderPlotly({
# use the key aesthetic/argument to help uniquely identify selected observations
key <- row.names(mtcars)
if (identical(input$plotType, “ggplotly”)) {
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
ggplotly§ %>% layout(dragmode = “select”)
} else {
plot_ly(mtcars, x = mpg, y = wt, key = key, mode = “markers”) %>%
layout(dragmode = “select”)

output$hover <- renderPrint({
d <- event_data(“plotly_hover”)
if (is.null(d)) “Hover events appear here (unhover to clear)” else d

output$click <- renderPrint({
d <- event_data(“plotly_click”)
if (is.null(d)) “Click events appear here (double-click to clear)” else d

output$brush <- renderPrint({
d <- event_data(“plotly_selected”)
if (is.null(d)) “Click and drag events (i.e., select/lasso) appear here (double-click to clear)” else d

output$zoom <- renderPrint({
d <- event_data(“plotly_relayout”)
if (is.null(d)) “Relayout (i.e., zoom) events appear here” else d


shinyApp(ui, server, options = list(display.mode = “showcase”))

Thanks a lot!