使用“ key”属性和facet_grid时,R Shiny ggplot2折线图不会显示折线

时间:2019-12-17 17:25:11

标签: r ggplot2 shiny plotly

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

现在,我想单击一条线上的位置并从数据框中检索相应的数据行。

我了解到可以通过event_data("plotly_click")捕获“ plotly_click” click事件来获取click事件。这有效;我得到一个曲线编号,x和y坐标。现在要获得对数据行的引用,我了解到必须对ggplot使用“ key”属性,例如:How can I grab the row of data from a ggplotly in shiny

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

编辑:

我用mtcars示例重现了这种行为:

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)

可能是由于键属性导致折线图在绘制线条时变得混乱了吗?删除facet_grid并包含“ key”属性时出现的行看起来很奇怪。

任何想法如何解决这个问题?除了使用密钥属性,我还可以通过其他方式解决我的问题吗?

感谢BR!

1 个答案:

答案 0 :(得分:0)

我找到了解决此问题的解决方案:通过组合点和线并将关键信息添加到这些点而不是线上-请参见第二幅图:

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)

gglot2 line plot with clickable points for drill down