Shiny + GGplot - 鼠标点击坐标
Shiny + GGplot - mouse click coordinates
我对绘制 GGPlot 条形图有疑问。
我可以识别鼠标单击的坐标 (x,y),但我需要知道条形图(x 轴)的值才能使用参数刷新图形并模拟向下钻取。
谁能帮帮我?
library(shiny)
library(ggplot2)
ui <- fluidPage(
plotOutput("plot", click = "GGPlot_click")
)
server <- function(input, output, session) {
v <- reactiveValues(
click1 = NULL
)
# Handle clicks on the plot
observeEvent(input$GGPlot_click, {
v$click1 <- input$GGPlot_click
})
observeEvent(input$reset, {
v$click1 <- NULL
})
output$plot <- renderPlot({
pg <- ggplot(cars, aes(speed, dist)) + geom_bar(stat="identity")
print(pg)
if (!is.null(v$click1$x))
print(paste(v$click1$x, v$click1$y, sep = " / "))
#print(v$click1)
})
}
shinyApp(ui, server)
我想办法解决:
图片和代码:https://github.com/faustobranco/StackQuestions
library(shiny)
library(ggplot2)
ui <- fluidPage(
plotOutput("plot", click = "plot_click"),
verbatimTextOutput("info")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
ggplot(cars, aes(speed, dist)) + geom_bar(stat="identity")
})
output$info <- renderText({
xy_str <- function(e) {
if(is.null(e)) return("NULL\n")
paste0("x=", round(e$x, 1), "\n")
}
x_Numeric <- function(e) {
if(is.null(e)) return(0)
round(e$x, 1)
}
paste0(
"click: x = ", xy_str(input$plot_click),
"Nearest x-axis[?]: ", toString(which(abs(as.numeric(cars$speed)-as.numeric(x_Numeric(input$plot_click)))==min(abs(as.numeric(cars$speed)-as.numeric(x_Numeric(input$plot_click))))))
)
})
}
shinyApp(ui, server)
我对绘制 GGPlot 条形图有疑问。 我可以识别鼠标单击的坐标 (x,y),但我需要知道条形图(x 轴)的值才能使用参数刷新图形并模拟向下钻取。 谁能帮帮我?
library(shiny)
library(ggplot2)
ui <- fluidPage(
plotOutput("plot", click = "GGPlot_click")
)
server <- function(input, output, session) {
v <- reactiveValues(
click1 = NULL
)
# Handle clicks on the plot
observeEvent(input$GGPlot_click, {
v$click1 <- input$GGPlot_click
})
observeEvent(input$reset, {
v$click1 <- NULL
})
output$plot <- renderPlot({
pg <- ggplot(cars, aes(speed, dist)) + geom_bar(stat="identity")
print(pg)
if (!is.null(v$click1$x))
print(paste(v$click1$x, v$click1$y, sep = " / "))
#print(v$click1)
})
}
shinyApp(ui, server)
我想办法解决:
图片和代码:https://github.com/faustobranco/StackQuestions
library(shiny)
library(ggplot2)
ui <- fluidPage(
plotOutput("plot", click = "plot_click"),
verbatimTextOutput("info")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
ggplot(cars, aes(speed, dist)) + geom_bar(stat="identity")
})
output$info <- renderText({
xy_str <- function(e) {
if(is.null(e)) return("NULL\n")
paste0("x=", round(e$x, 1), "\n")
}
x_Numeric <- function(e) {
if(is.null(e)) return(0)
round(e$x, 1)
}
paste0(
"click: x = ", xy_str(input$plot_click),
"Nearest x-axis[?]: ", toString(which(abs(as.numeric(cars$speed)-as.numeric(x_Numeric(input$plot_click)))==min(abs(as.numeric(cars$speed)-as.numeric(x_Numeric(input$plot_click))))))
)
})
}
shinyApp(ui, server)