I know that I can get xy mouse click coordinates in shiny with this stacked bar chart produced by ggplot2:
data <- data.frame(pie = c(0.25,0.25,0.25,0.25))
library(ggplot2)
ui <- fluidPage(
plotOutput('pie', click = "plot_click"),
verbatimTextOutput('mouse')
)
server <- function(input, output) {
output$pie <- renderPlot({
ggplot(data, aes(x = factor(1), y = pie, fill = row.names(data)))
+ geom_bar(stat = "identity")
})
output$mouse <- renderPrint({
str(input$plot_click)
})
}
shinyApp(ui=ui, server=server)
Which when I ran and clicked on just now, I get this kind output, specifically a list, which contains the relative xy coordinate that I clicked on in the plot:
List of 7
$ x : num 0.887
$ y : num 0.116
$ mapping:List of 3
..$ x : chr "factor(1)"
..$ y : chr "pie"
..$ fill: chr "row.names(data)"
$ domain :List of 4
..$ left : num 0.5
..$ right : num 1.5
..$ bottom: num -0.05
..$ top : num 1.05
$ range :List of 4
..$ left : num 44.9
..$ right : num 818
..$ bottom: num 365
..$ top : num 5.48
$ log :List of 2
..$ x: NULL
..$ y: NULL
$ .nonce : num 0.797
However, when I convert this to a pie chart instead with the following, I don't get a list with int 0 elements for the x and y. I'm using this code (which simply adds coord_polar(theta = "y") to the ggplot):
data <- data.frame(pie = c(0.25,0.25,0.25,0.25))
library(ggplot2)
ui <- fluidPage(
plotOutput('pie', click = "plot_click"),
verbatimTextOutput('mouse')
)
server <- function(input, output) {
output$pie <- renderPlot({
ggplot(data, aes(x = factor(1), y = pie, fill = row.names(data)))
+ geom_bar(stat = "identity")
+ coord_polar(theta = "y")
})
output$mouse <- renderPrint({
str(input$plot_click)
})
}
shinyApp(ui=ui, server=server)
Which gives me this kind of result upon clicking on the plot:
List of 7
$ x : int 0
$ y : int 0
$ mapping:List of 3
..$ x : chr "factor(1)"
..$ y : chr "pie"
..$ fill: chr "row.names(data)"
$ domain :List of 4
..$ left : NULL
..$ right : NULL
..$ bottom: NULL
..$ top : NULL
$ range :List of 4
..$ left : num 31.9
..$ right : num 818
..$ bottom: num 373
..$ top : num 5.48
$ log :List of 2
..$ x: NULL
..$ y: NULL
$ .nonce : num 0.964
I'm new to using these mouse events for plots. Is there any way to get the mouse click coordinates for a ggplot pie chart? If it's not possible, is there a way to use javascript to covert the pixel coordinates of where I click to plot coordinates? Or, is there a better graphics system that would support this kind of functionality that I could use in shiny?
Thanks in advance!
There doesn't seem to be an easy answer to this question, without resorting to graphics and web programming outside of R.
My solution was to create a legend for the plot out of html, where the user clicks a part of the legend in order to derive which data they selected.