R 2折线图赢得';使用“时不显示行”;“关键”;平面网格的性质及应用

R 2折线图赢得';使用“时不显示行”;“关键”;平面网格的性质及应用,r,ggplot2,shiny,plotly,R,Ggplot2,Shiny,Plotly,我有一个R闪亮的应用程序,它显示一个带有刻面网格的折线图。因此,我使用ggplot2和plotly 现在,我希望能够单击一行上的某个位置,并从数据框中检索相应的数据行 我了解到,通过事件数据(“plotly\u click”)捕捉“plotly\u click”点击事件,可以获取点击事件。这是有效的;我得到一个曲线数,x坐标和y坐标。现在,为了获得对我的数据行的引用,我学会了对ggplot使用“key”属性,如下所述: 现在,当我将“key”属性添加到我的ggplot中时,这些线突然不再显示(图

我有一个R闪亮的应用程序,它显示一个带有刻面网格的折线图。因此,我使用ggplot2和plotly

现在,我希望能够单击一行上的某个位置,并从数据框中检索相应的数据行

我了解到,通过
事件数据(“plotly\u click”)
捕捉“plotly\u click”点击事件,可以获取点击事件。这是有效的;我得到一个曲线数,x坐标和y坐标。现在,为了获得对我的数据行的引用,我学会了对ggplot使用“key”属性,如下所述:

现在,当我将“key”属性添加到我的ggplot中时,这些线突然不再显示(图表似乎保持为空)。有趣的是,当我删除facet_grid时,会显示一些行,单击它会提供“关键”信息,以及上面链接中所述的事件

编辑:

我用MTV复制了这种行为:

library(shiny)
library(plotly)
library(tidyr)

mtcars$key <- row.names(mtcars)

ui <- fluidPage(
    plotlyOutput("originalLinePlot"),
    plotlyOutput("keyLinePlot"),
    verbatimTextOutput("click"),
)

server <- function(input, output) {

    output$originalLinePlot <- renderPlotly({
        # here I want to add click event with data row selection - click doesn't return key info

        data_long <- gather(mtcars, condition, measurement, c(drat, wt), factor_key=TRUE)

        g <- ggplot(data_long, aes(x=mpg))
        # won't work
        # g <- ggplot(data_long, aes(x=mpg, key=key))

        g <- g + facet_grid(rows = vars(condition), scales="free_y")
        g <- g + geom_line(aes(y=measurement))
        g
    })


    output$keyLinePlot <- renderPlotly({
        data_long <- gather(mtcars, condition, measurement, c(drat, wt), factor_key=TRUE)

        g <- ggplot(data_long, aes(x=mpg, key=key))

        # won't work
        # g <- g + facet_grid(rows = vars(condition), scales="free_y")
        g <- g + geom_line(aes(y=measurement))
        g
    })

    output$click <- renderPrint({
        d <- event_data("plotly_click")
        if (is.null(d)) "Click events appear here (double-click to clear)" else data.frame(d) 
    })
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(绘本)
图书馆(tidyr)

mtcars$key我找到了一个解决方案:通过组合点和线,并将关键信息添加到点而不是线中-参见第二个图:

library(shiny)
library(plotly)
library(tidyr)

mtcars$key <- row.names(mtcars)

ui <- fluidPage(
    plotlyOutput("originalLinePlot"),
    plotlyOutput("keyLinePlot"),
    verbatimTextOutput("click"),
)

server <- function(input, output) {

    output$originalLinePlot <- renderPlotly({
        # here I want to add click event with data row selection - click doesn't return key info

        data_long <- gather(mtcars, condition, measurement, c(drat, wt), factor_key=TRUE)

        g <- ggplot(data_long, aes(x=mpg))
        # won't work
        # g <- ggplot(data_long, aes(x=mpg, key=key))

        g <- g + facet_grid(rows = vars(condition), scales="free_y")
        g <- g + geom_line(aes(y=measurement))
        g
    })


    output$keyLinePlot <- renderPlotly({
        data_long <- gather(mtcars, condition, measurement, c(drat, wt), factor_key=TRUE)

        g <- ggplot(data_long, aes(x=mpg))

        g <- g + facet_grid(rows = vars(condition), scales="free_y")
        g <- g + geom_line(aes(y=measurement))
        g <- g + geom_point(aes(y=measurement, key=key))
        g
    })

    output$click <- renderPrint({
        d <- event_data("plotly_click")
        if (is.null(d)) "Click events appear here (double-click to clear)" else data.frame(d) 
    })
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(绘本)
图书馆(tidyr)

mtcars$key我找到了一个解决方案:通过组合点和线,并将关键信息添加到点而不是线中-参见第二个图:

library(shiny)
library(plotly)
library(tidyr)

mtcars$key <- row.names(mtcars)

ui <- fluidPage(
    plotlyOutput("originalLinePlot"),
    plotlyOutput("keyLinePlot"),
    verbatimTextOutput("click"),
)

server <- function(input, output) {

    output$originalLinePlot <- renderPlotly({
        # here I want to add click event with data row selection - click doesn't return key info

        data_long <- gather(mtcars, condition, measurement, c(drat, wt), factor_key=TRUE)

        g <- ggplot(data_long, aes(x=mpg))
        # won't work
        # g <- ggplot(data_long, aes(x=mpg, key=key))

        g <- g + facet_grid(rows = vars(condition), scales="free_y")
        g <- g + geom_line(aes(y=measurement))
        g
    })


    output$keyLinePlot <- renderPlotly({
        data_long <- gather(mtcars, condition, measurement, c(drat, wt), factor_key=TRUE)

        g <- ggplot(data_long, aes(x=mpg))

        g <- g + facet_grid(rows = vars(condition), scales="free_y")
        g <- g + geom_line(aes(y=measurement))
        g <- g + geom_point(aes(y=measurement, key=key))
        g
    })

    output$click <- renderPrint({
        d <- event_data("plotly_click")
        if (is.null(d)) "Click events appear here (double-click to clear)" else data.frame(d) 
    })
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(绘本)
图书馆(tidyr)

mtcars$key谢谢@phalteman,我添加了一个可复制的示例谢谢@phalteman,我添加了一个可复制的示例