Skip to content

Instantly share code, notes, and snippets.

@walkerke
Last active March 25, 2016 00:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save walkerke/da941dac84f730adcdde to your computer and use it in GitHub Desktop.
Save walkerke/da941dac84f730adcdde to your computer and use it in GitHub Desktop.

Shiny app to highlight on a Leaflet map by brushing a Plotly chart. Run for yourself with (provided all of the packages are installed):

shiny::runGist('da941dac84f730adcdde')

To get this to work on another dataset, you're going to need to have a sequential ID variable that matches the pointNumber column in the data frame generated by Plotly's event_data. See the setup.R script for how this was derived.

library(shiny)
library(leaflet)
library(plotly)
library(ggplot2)
library(geojsonio)
tarrxy <- geojson_read('tarrxy.geojson', what = 'sp')
ui <- fluidPage(
titlePanel("Linked brushing with Plotly and Leaflet in Shiny"),
fixedRow(
column(width = 6,
plotlyOutput('scatter'))
),
fixedRow(
column(width = 6,
leafletOutput('map'))
)
)
server <- function(input, output, session) {
output$scatter <- renderPlotly({
g <- ggplot(tarrxy@data, aes(x = pctwhite, y = pctrep)) +
geom_point() +
xlab('Percent non-Hispanic white, 2010') +
ylab('Percent voting Republican, 2012')
ggplotly(g, source = 'source') %>% layout(dragmode = 'lasso')
})
selected_precincts <- reactive({
eventdata <- event_data('plotly_selected', source = 'source')
precincts <- as.numeric(eventdata[['pointNumber']])
sub <- tarrxy[tarrxy$id %in% precincts, ]
return(sub)
})
output$map <- renderLeaflet({
tarrant_map <- leaflet() %>%
addProviderTiles('CartoDB.DarkMatter') %>%
addPolygons(data = tarrxy, weight = 1, smoothFactor = 0.2,
color = 'white', fillColor = 'grey') %>%
addPolygons(data = selected_precincts(), fill = FALSE, color = '#FFFF00',
opacity = 1)
tarrant_map
})
}
shinyApp(ui = ui, server = server)
library(rgdal)
library(rmapshaper)
library(geojsonio)
tarr <- readOGR(dsn = '.', layer = 'tarrant_precincts')
tarr_simp <- ms_simplify(tarr)
tarrxy <- spTransform(tarr_simp, CRS("+proj=longlat +datum=WGS84"))
tarrxy <- tarrxy[tarrxy$pctrep != -1 & tarrxy$pctwhite != -1, ]
l1 <- length(tarrxy) - 1
tarrxy$id <- 0:l1
geojson_write(tarrxy, file = 'tarrxy.geojson')
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@cpsievert
Copy link

Nice work @walkerke! Since some of these packages are not on CRAN, it'd be useful to have code to install them in the README

@cpsievert
Copy link

Also, would it be possible to include code for obtaining the shapefile?

@walkerke
Copy link
Author

Thanks! And thanks for the feedback. I didn't use R to obtain and prep the shapefile but I'll include some pointers.

My ultimate goal is to get the Plotly events talking to leafletProxy so that the entire map doesn't re-load every time, and that the selected tracts are simply removed and then re-added - this isn't working for me just yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment