user9532692
user9532692

Reputation: 706

Creating a heatmap on R using leaflet function add_heatmap() or addHeatmap()

I am trying to create a plot with heatmap using plotly add_heatmap() function, but it generates an error message saying Error: Must supply z attribute I referred to this site for addHeatmap() function provided by leaflet.extras package.

The following code only displays dots from addCircles() and outputs a warning message and a map as shown below:

final_df %>%
  leaflet() %>%
  addTiles() %>%
  addCircles(
    lng = final_df$long,
    lat = final_df$lat,
    popup = final_df$station_name
  ) %>% addHeatmap(lng = final_df$long, lat = final_df$lat, radius=5)

warning message dot map

I have tried a different function add_heatmap() from leaflet which does not display any maps and generates an error.

final_df %>%
  leaflet() %>%
  addTiles() %>%
  addCircles(
    lng = final_df$long,
    lat = final_df$lat,
    popup = final_df$station_name
  ) %>% add_heatmap(lng = final_df$long, lat = final_df$lat)

error message

Anyone faced a similar issue and created a heatmap on R?

Upvotes: 0

Views: 3494

Answers (1)

Oleole
Oleole

Reputation: 411

Remove addCircles part and try:

library(leaflet.extras)

final_df %>%
  leaflet() %>%
  addTiles() %>%
  addHeatmap(lng = final_df$long, lat = final_df$lat, blur = 40, max = 0.05, radius = 15)

Upvotes: 4

Related Questions