The mouse coordinates in ggplot / shiny coordinates are incorrect when ord_fixed is set

I use the answer in the question " ToolTip when you hover over ggplot with a shiny one " to add mouseover functionality to the ggplot object in my shiny application. In contrast, I need to set coord_fixed to ggplot for my application. Here is a minimal working example:

 library(shiny) library(ggplot2) ui <- fluidPage( plotOutput("plot1", height = "300px", width = "100%", hover = hoverOpts(id = "plot_hover")), verbatimTextOutput("hover_info") ) server <- function(input, output) { output$plot1 <- renderPlot({ ggplot(mtcars, aes(x=mpg,y=qsec)) + geom_point() + coord_fixed(xlim=c(0,30),ylim=c(0,30)) }) output$hover_info <- renderPrint({ if(!is.null(input$plot_hover)) paste0(input$plot_hover$x, " ", input$plot_hover$y) }) } shinyApp(ui, server) 

Now my problem is that I am not getting the correct x coordinate. In the following screenshot, the mouse pointer is near the start, but the return value of x is around 9:

The mouse pointer is close to the origin, but the x coordinate is about 9

If I resized the browser window so that there was no space between the graph and the border of the window, the coordinates are correct. It seems to me that hoverOpts just ignores the coord_fixed ggplot layout.

Setting width = "300px" instead of width = "100%" can be a workaround. But this is not a real option for me, since I generally need a legend of variable width directly to the plot in the graph area.

How can I get the correct x value?

+5
source share

Source: https://habr.com/ru/post/1243962/


All Articles