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?
MWE:
library("tidyverse")
library("shiny")
library("cowplot")
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)) +
geom_point()
})
output$oneplotclicklocation <- renderText({input$oneplotclick$x})
output$bothplots <- renderPlot({
plot1 <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point()
plot2 <- ggplot(mtcars, aes(x = mpg, y = qsec)) +
geom_point()
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.)