I am working on a Shiny app that should let a user select geographical data points on a world map generated by ggplot2 (as in this example).
This works if I use the regular coord_cartesian coordinate system (which distorts the maps) but fails if I use the more appropriate coord_map coordinate system. It seems like the click/brush events do not receive the correct coordinates after the projection. Is there any way I can work around or fix this without reverting back to cartesian coordinates?
You can find a working example below:
library(shiny)
library(ggplot2)
library(dplyr)
map_world <- function(world, mapPoints, xlim, ylim){
# function to generate ggplot world map
ggplot() +
geom_polygon(data=world, aes(x=long, y=lat, group=group)) +
geom_point(data=mapPoints, aes(x=Longitude, y=Latitude), color="red") +
# coord_map messes up the brush select
coord_map(xlim=xlim, ylim=ylim)
# coord_cartesian would work but distort the map
# coord_cartesian(xlim=xlim, ylim=ylim)
}
mapPoints <- data.frame(Longitude=c(-103, -108, -130, -120),
Latitude=c(52, 40, 45, 54))
world <- map_data("world")
ui <- fluidPage(
fluidRow(
column(width = 6,
plotOutput("plot1", height = 300,
click = "plot1_click", brush = "plot1_brush") )
),
fluidRow(
column(width = 6,
h4("Near points"), verbatimTextOutput("click_info") ),
column(width = 6,
h4("Brushed points"), verbatimTextOutput("brush_info") )
)
)
server <- function(input, output) {
# output world map
output$plot1 <- renderPlot({
map_world(world = world, mapPoints = mapPoints,
xlim=c(-180,180), ylim=c(-90,90))
})
# output clicked points
output$click_info <- renderPrint({
nearPoints(mapPoints, xvar="Longitude",
yvar="Latitude", input$plot1_click)
})
# output brushed points
output$brush_info <- renderPrint({
brushedPoints(mapPoints, xvar="Longitude",
yvar="Latitude", input$plot1_brush)
})
}
shinyApp(ui, server)
Thanks!