【问题标题】:R Shiny ggplot2 line chart won't show lines when using "key" property and facet_gridR Shiny ggplot2 折线图在使用“key”属性和 facet_grid 时不会显示线条
【发布时间】:2019-12-17 17:25:11
【问题描述】:

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

现在我想让点击一行上的一个位置并从数据框中检索相应的数据行成为可能。

我了解到可以通过event_data("plotly_click") 捕获“plotly_click”点击事件来获取点击事件。这行得通;我得到一个曲线编号、x 和 y 坐标。现在要获得对我的数据行的引用,我了解到我必须使用 ggplot 的“键”属性,如下所述: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!

【问题讨论】:

标签: r ggplot2 shiny plotly


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 2018-10-19
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多