将事件数据信息映射到 Shiny 反应图中的绘图信息
Mapping the event data information to the plotting information in Shiny reactive plots
如何从even_data信息中提取用于绘图的原始数据?
library(plotly)
library(shiny)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("click")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
key <- row.names(mtcars)
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
geom_point()
ggplotly(p) %>% layout(dragmode = "select")
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else d
})
}
shinyApp(ui, server)
点击一个点时,示例输出类似于
curveNumber pointNumber x y key
1 1 3 24.4 3.19 Merc 240D
是否有任何方法可以将这些信息映射到原始数据集mtcars
? curveNumber
、pointNumber
中的信息有何用处,这些字段的含义是什么?
curveNumber
是colour = factor(vs)
变量,pointNumber
是组内的行号+1(vs的0或1)。
因此使用这两个您可以执行以下操作:
library(plotly)
library(shiny)
library(dplyr)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("click")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
key <- row.names(mtcars)
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
geom_point()
ggplotly(p) %>% layout(dragmode = "select")
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else mtcars %>% tibble::rownames_to_column() %>% filter(vs==d$curveNumber) %>% filter(row_number()==d$pointNumber+1)
})
}
shinyApp(ui, server)
或者,第二个选项,您需要从 event_data
中提取 key
和子集 mtcars,如下所示:
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)"
else mtcars[rownames(mtcars) == d$key,]
})
完整的应用程序:
library(plotly)
library(shiny)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("click")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
key <- row.names(mtcars)
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
geom_point()
ggplotly(p) %>% layout(dragmode = "select")
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else mtcars[rownames(mtcars) == d$key,]
})
}
shinyApp(ui, server)
如何从even_data信息中提取用于绘图的原始数据?
library(plotly)
library(shiny)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("click")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
key <- row.names(mtcars)
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
geom_point()
ggplotly(p) %>% layout(dragmode = "select")
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else d
})
}
shinyApp(ui, server)
点击一个点时,示例输出类似于
curveNumber pointNumber x y key
1 1 3 24.4 3.19 Merc 240D
是否有任何方法可以将这些信息映射到原始数据集mtcars
? curveNumber
、pointNumber
中的信息有何用处,这些字段的含义是什么?
curveNumber
是colour = factor(vs)
变量,pointNumber
是组内的行号+1(vs的0或1)。
因此使用这两个您可以执行以下操作:
library(plotly)
library(shiny)
library(dplyr)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("click")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
key <- row.names(mtcars)
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
geom_point()
ggplotly(p) %>% layout(dragmode = "select")
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else mtcars %>% tibble::rownames_to_column() %>% filter(vs==d$curveNumber) %>% filter(row_number()==d$pointNumber+1)
})
}
shinyApp(ui, server)
或者,第二个选项,您需要从 event_data
中提取 key
和子集 mtcars,如下所示:
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)"
else mtcars[rownames(mtcars) == d$key,]
})
完整的应用程序:
library(plotly)
library(shiny)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("click")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
key <- row.names(mtcars)
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
geom_point()
ggplotly(p) %>% layout(dragmode = "select")
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else mtcars[rownames(mtcars) == d$key,]
})
}
shinyApp(ui, server)