r - R Shiny ggplot2 折线图在使用“key”属性和 facet_grid 时不会显示线条
问题描述
我有一个 R 闪亮的应用程序,它显示一个带有分面网格的折线图。因此,我使用 ggplot2 和 plotly。
现在我想让点击一行上的一个位置并从数据框中检索相应的数据行成为可能。
我了解到可以通过捕获“plotly_click”单击事件来获取单击事件event_data("plotly_click")
。这行得通;我得到一个曲线编号、x 和 y 坐标。现在要获得对我的数据行的引用,我了解到我必须使用 ggplot 的“键”属性,如下所述:如何从闪亮的 ggplotly 中获取数据行。
现在,当我将“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)
推荐阅读
- css - jumbotron 灰色的十六进制代码是什么?
- python - rpy2在jupyter实验室将python对象转换为R对象
- python - Python 挂在长度和拆分函数上
- reactjs - 如何在没有 Node.js 服务器的情况下部署 Next.js 应用程序?
- javascript - 如何使用 JavaScript 使图像在点击时播放声音?
- android - Android Paging 3 库:如何使用新参数更改列表?
- java - Discord Java Bot 没有响应
- android - 如何用 Flow 替换 LiveData
- php - 致命错误:未捕获的类型错误:filter_var():参数 #3 ($options) 必须是数组类型
- c - 将 C 数组中的一个字符转换为大写会给出 C4244 警告