简体   繁体   English

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

[英]R Shiny ggplot2 line chart won't show lines when using "key" property and facet_grid

I have an R shiny app that is displaying a line chart with a facet grid.我有一个 R 闪亮的应用程序,它显示一个带有分面网格的折线图。 I therefore use ggplot2 and plotly.因此我使用 ggplot2 和 plotly。

Now I want to make it possible to click a position on a line and retrieve the corresponding data row from the data frame.现在我想让点击一行上的一个位置并从数据框中检索相应的数据行成为可能。

I learned fetching the click event is possible by catching the "plotly_click" click event via event_data("plotly_click") .我了解到可以通过event_data("plotly_click")捕获“plotly_click”点击事件来event_data("plotly_click")点击事件。 This works;这有效; I get a curve number, x- and y- coordinates.我得到一个曲线编号,x 和 y 坐标。 Now to get a reference to my data row I learned I have to use a "key" property for ggplot like stated here: How can I grab the row of data from a ggplotly in shiny .现在要获得对我的数据行的引用,我了解到我必须使用 ggplot 的“key”属性,如下所述: How can I get the row of data from a ggplotly in Shiny

Now when I add the "key" property to my ggplot suddenly the lines won't show up anymore (it seems the chart stays empty).现在,当我将“key”属性添加到我的 ggplot 时,线条将不再显示(似乎图表保持空白)。 Interestingly, when I remove facet_grid, some lines will show up and clicking it provides the "key"-information with the event like stated in the link above.有趣的是,当我删除 facet_grid 时,会显示一些行,单击它会提供“关键”信息以及上面链接中所述的事件。

EDIT:编辑:

I reproduced the behavior with the mtcars exmaple:我用 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)

May it be that due to the key-property the line chart gets confused as how to draw its lines?可能是由于关键属性,折线图对如何绘制线条感到困惑? The lines appearing when removing the facet_grid and having the "key"-propery included look quite strange..删除 facet_grid 并包含“key”属性时出现的线条看起来很奇怪..

Any ideas how to resolve this?任何想法如何解决这个问题? Can I solve my issue also in a different way than using the key-property?我能否以与使用密钥属性不同的方式解决我的问题?

Thanks and BR!谢谢和BR!

I've found a solution of how to solve this: by combining points and lines and adding the key information to the points instead of the lines - see second plot:我找到了如何解决这个问题的解决方案:通过组合点和线并将关键信息添加到点而不是线 - 参见第二个图:

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 线图,带有可点击的点以便向下钻取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM