簡體   English   中英

R Shiny ggplot2 折線圖在使用“key”屬性和 facet_grid 時不會顯示線條

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

我有一個 R 閃亮的應用程序,它顯示一個帶有分面網格的折線圖。 因此我使用 ggplot2 和 plotly。

現在我想讓點擊一行上的一個位置並從數據框中檢索相應的數據行成為可能。

我了解到可以通過event_data("plotly_click")捕獲“plotly_click”點擊事件來event_data("plotly_click")點擊事件。 這有效; 我得到一個曲線編號,x 和 y 坐標。 現在要獲得對我的數據行的引用,我了解到我必須使用 ggplot 的“key”屬性,如下所述: How can I get 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!

我找到了如何解決這個問題的解決方案:通過組合點和線並將關鍵信息添加到點而不是線 - 參見第二個圖:

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