I'm trying to use Shiny to create an interactive scatter plot where I can click on any point on the graph and get the corresponding x and y co-ordinates. It works (i.e. is the RStudio example) for base graphics (commented out) gives different and incorrect numbers using ggplot.
library(shiny)
library(ggplot2)
ui <- basicPage(
plotOutput("plot1", click = "plot_click"),
verbatimTextOutput("info")
)
server <- function(input, output) {
output$plot1 <- renderPlot({
# plot(mtcars$wt, mtcars$mpg)
g=ggplot(mtcars,aes(wt,mpg))+geom_point()
g
})
output$info <- renderText({
paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
})
}
shinyApp(ui, server)
Do I need to add anything to my code?