HeatMaps in Shiny

671 views
Skip to first unread message

Abdur Rehman Raja

unread,
Nov 10, 2016, 1:23:12 PM11/10/16
to Shiny - Web Framework for R
Hi Everyone,

I'm trying to create a heatmap in R shiny using this format: 
output$map <- renderLeaflet({leaflet() %>% addTiles() %>%...})
  
Is there an addHeatMap function that I can use? Or which other method can I use to make the heatmap. I don't want to use the functions from the rMaps package.

Any help is appreciated.

Thanks,
Abdur 

jeremiah rounds

unread,
Nov 10, 2016, 3:31:19 PM11/10/16
to Abdur Rehman Raja, Shiny - Web Framework for R
https://rstudio.github.io/leaflet/json.html
Using addGeoJSON is how I approach that problem.


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e456142c-bb21-4130-9c1a-e9620dd5a459%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dawg

unread,
Nov 14, 2016, 1:19:01 PM11/14/16
to Shiny - Web Framework for R
I had the same problem and use the following solution to be more responsive:

1) make the heatmap with the raster package (variable is in xyz below, example with US coordinates)
2) display the raster image in leaflet

1)
    make.heatmap <- reactive({
        heatm <- cbind(xyz()$LON,xyz()$LAT,xyz()$count)
        colnames(heatm) <- c('X', 'Y', 'Z')
        heatm.extent <- extent(-125,-60,20,50)
        heatm.raster <- raster(heatm.extent,ncol=650,300)
        heat.map <- rasterize(heatm[,1:2],heatm.raster,heatm[,3],fun=sum)
        heat.map[] <- heat.map[] / max(values(heat.map),na.rm=TRUE)
        crs(heat.map) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" 
        return(heat.map)
    })

2)
addRasterImage(make.heatmap(), colors = pal, opacity = 0.4, group = "Heatmap")

Hope that helps,
D.

Dawg

unread,
Nov 14, 2016, 3:10:54 PM11/14/16
to Shiny - Web Framework for R
sorry, this doesn't work. It gives projection errors from the raster image: http://stackoverflow.com/questions/33742107/raster-image-seems-to-be-shifted-using-leaflet-for-r?rq=1

Joe Cheng

unread,
Nov 14, 2016, 4:43:36 PM11/14/16
to Dawg, Shiny - Web Framework for R
You might try the new webgl-heatmap support Bhaskar has added in the leaflet.extras package:

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/0b6cc14b-8c17-4dce-bf1e-0e64cfde30fa%40googlegroups.com.

Dawg

unread,
Nov 14, 2016, 4:57:07 PM11/14/16
to Shiny - Web Framework for R, dag.l...@katrisk.com
Tried webgl-heatmap first before using raster. It's good for a limited amount of data. Once you have much more than 100k points it is slow.

Abdur Rehman Raja

unread,
Nov 18, 2016, 8:16:49 AM11/18/16
to Shiny - Web Framework for R, dag.l...@katrisk.com
Hi Joe,

Thank you so much for your response. I tried the webgl method and it works beautifully! 

Regards,
Abdur

Abdur Rehman Raja

unread,
Nov 18, 2016, 8:18:36 AM11/18/16
to Shiny - Web Framework for R, arraj...@gmail.com
Hi Jerimiah,

Thanks for your response, I wasn't quite sure how to add the heatmap using geojsons, but I got it to work by using the addWebGlHeatMap method. 

Cheers,
Abdur


On Thursday, November 10, 2016 at 3:31:19 PM UTC-5, jeremiah rounds wrote:
https://rstudio.github.io/leaflet/json.html
Using addGeoJSON is how I approach that problem.

On Thu, Nov 10, 2016 at 10:23 AM, Abdur Rehman Raja <arraj...@gmail.com> wrote:
Hi Everyone,

I'm trying to create a heatmap in R shiny using this format: 
output$map <- renderLeaflet({leaflet() %>% addTiles() %>%...})
  
Is there an addHeatMap function that I can use? Or which other method can I use to make the heatmap. I don't want to use the functions from the rMaps package.

Any help is appreciated.

Thanks,
Abdur 

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages