Click location from multiple aligned plots in Shiny

I'm trying to combine two plots in Shiny using cowplot/plot_grid, and I'd like to be able to get the x coordinate for clicks.

When I have a single plot, not using plot_grid, I get an x value in the scale of the x axis. When I have two plots, though, I get a decimal percentage of the width of the plot.

Is it possible to either get the x value for clicks on one of the plots in a group of multiple aligned plots directly, or to figure it out from the reported decimal percentage?



ui <- basicPage(
    plotOutput("oneplot", click = "oneplotclick"),
    verbatimTextOutput("oneplotclicklocation"), #Prints x value

    plotOutput("bothplots", click = "bothplotsclick"),
    verbatimTextOutput("bothplotsclicklocation") #Prints percent of plot width

# Server logic ----
server <- function(input, output) {

    output$oneplot <- renderPlot({
        ggplot(mtcars, aes(x = mpg, y = hp)) +

    output$oneplotclicklocation <- renderText({input$oneplotclick$x})

    output$bothplots <- renderPlot({
        plot1 <- ggplot(mtcars, aes(x = mpg, y = hp)) +
        plot2 <- ggplot(mtcars, aes(x = mpg, y = qsec)) +
        plot_grid(plot1, plot2, nrow = 2)

    output$bothplotsclicklocation <- renderText({input$bothplotsclick$x})


# Run app ----
shinyApp(ui, server)

(In this MWE it's not crucial that the plots have aligned x axes, but in my actual application that's necessary.)

I couldn't help but notice that you recently posted a very similar question on and on a different website. The current way you've done this is generally considered not-OK both here an on SO. FAQ: Is it OK if I cross-post?

Thanks for the heads-up. I don't seem to have permission to delete this topic, but the admins can feel free to do so. I'll give the post over on SO a few days to marinate before coming back here with an update if I find one.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.