Generate geographic heatmap with 'leaflet' package

I want to generate a geographic heatmap representing the abundance of each species in different geographic points, here is a toy example of my data and code, but I have an error message. Could you help please ?

library(leaflet)
fishes <- data.frame(lat=c(-47.2,-20.4,-20.9),long=c(-57.9,-3.2,-35.2),abudance=c(5,1,17),samples=c("s1","s2","s3"))
fishes
lat  long abudance samples
1 -47.2 -57.9        5      s1
2 -20.4  -3.2        1      s2
3 -20.9 -35.2       17      s3

leaflet(fishes) %>% addTiles() %>%
  +     addCircles(lng = ~long, lat = ~lat, weight = 1,
                   +                radius = ~abudance *20000, popup = ~samples
                   +     ) %>% addTiles() %>% addMarkers(~long, ~lat, popup = ~as.character(abudance), label = ~as.character(abudance))


leaflet(fishes) %>% addTiles() %>%
+   +     addCircles(lng = ~long, lat = ~lat, weight = 1,
+                    +                radius = ~abudance *20000, popup = ~samples
Error: unexpected '=' in:
"  +     addCircles(lng = ~long, lat = ~lat, weight = 1,
                   +                radius ="
>                    +     ) %>% addTiles() %>% addMarkers(~long, ~lat, popup = ~as.character(abudance), label = ~as.character(abudance))
Error: unexpected ')' in "                   +     )"
>

I think your problem is that you are copying that code from console output that includes the new-line markers "+", you have to remove those and the code works.

library(leaflet)

fishes <- data.frame(lat=c(-47.2,-20.4,-20.9),
                     long=c(-57.9,-3.2,-35.2),
                     abudance=c(5,1,17),
                     samples=c("s1","s2","s3")
                     )

leaflet(fishes) %>%
    addTiles() %>%
    addCircles(lng = ~long, lat = ~lat, weight = 1, radius = ~abudance * 20000, popup = ~samples) %>%
    addTiles() %>%
    addMarkers(~long, ~lat, popup = ~as.character(abudance), label = ~as.character(abudance))

1 Like

Thanks a lot , you are right! I am a beginner in R, sorry!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.