设置coord_fixed时,ggplot/shiny中的鼠标悬停坐标是错误的

Pat*_*cks 5 r mouseover ggplot2 shiny

我正在使用" 将鼠标悬停在ggplot上时的工具提示 "问题中的答案,将鼠标悬停功能添加到我的闪亮应用程序中的ggplot对象中.与此相反,我需要coord_fixed在ggplot中设置我的应用程序.这是一个最小的工作示例:

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)
Run Code Online (Sandbox Code Playgroud)

现在我的问题是我没有得到正确的x坐标.在下面的屏幕截图中,鼠标指针靠近原点,但返回的x值大约为9:

鼠标指针靠近原点,但x坐标约为9

如果我调整浏览器窗口的大小,使得绘图和窗口边框之间没有空格,则坐标是正确的.在我看来,hoverOpts只是忽略了coord_fixedggplot 的布局.

设置width = "300px"而不是width = "100%"可能是一种解决方法.但这对我来说不是一个真正的选择,因为我一般需要一个可变宽度的图例,该图例位于绘图区域内.

如何获得正确的x值?