I have an R shiny app that is displaying a line chart with a facet grid. I therefore use ggplot2 and 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")
. This works; I get a curve number, x- and y- coordinates. 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 .
Now when I add the "key" property to my ggplot suddenly the lines won't show up anymore (it seems the chart stays empty). 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.
EDIT:
I reproduced the behavior with the mtcars exmaple:
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..
Any ideas how to resolve this? Can I solve my issue also in a different way than using the key-property?
Thanks and 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.