r语言 - 是否有一种方法来确定哪个轴标签用户点击在闪亮的情节?



我知道如何使用标准方法来获得绘图点击的x/y坐标,但它只在绘图的实际数据区域内工作。是否有任何方法还包括数据区域之外的边界,以便我还可以确定是否已单击轴标签?我使用ggplot

我只能想到一个近似的解

  • 可以通过shinyjs
  • 跟踪点击
  • 你会收到一个像素值,它必须转换成x轴上的值。这一步必须考虑到x轴上的比例,情节的边界,当前会话中的情节大小,并且必须与应用/情节的大小调整保持不变。我的计算示例是在以下代码。

可再生的例子:

library(shiny)
library(shinyjs)
xlim <- c(1, 3)
offset_left <- 75
offset_right <- 15
ui <- basicPage(
useShinyjs(),
plotOutput(outputId = "plot1", click = "plot_click"),
p(id = "date", "The x value you clicked on the axis:"),
verbatimTextOutput("info")
)
server <- function(input, output, session){
global <- reactiveValues(x = NULL)

onclick("plot1", function(event){
plot_width <- session$clientData$output_plot1_width
global$x <- xlim[1] + (xlim[2] - xlim[1])/0.926*(event$pageX - offset_left) / 
(plot_width - offset_left - offset_right) - 0.08
})
output$plot1 <- renderPlot({
plot(mtcars$wt, mtcars$mpg, xlim = xlim)
})
output$info <- renderText({
global$x
})
}
shinyApp(ui, server)

最新更新