Adding mini radar plots as markers on leaflet map

274 views Asked by At

I have the following dataset of weather conditions in 5 different sites observed in 15-minute intervals over a year, and am developing a shiny app based on it.

site_id date_time           latitude longitude ambient_air_tem~ relative_humidy barometric_pres~ average_wind_sp~ particulate_den~
   <chr>   <dttm>                 <dbl>     <dbl>            <dbl>           <dbl>            <dbl>            <dbl>            <dbl>
 1 arc1046 2019-11-15 09:15:00    -37.8      145.             14.4            65.4            1007.             7.45              3.9
 2 arc1048 2019-11-15 09:15:00    -37.8      145.             14.0            65.5            1006.             6.95              4.4
 3 arc1045 2019-11-15 09:15:00    -37.8      145.             14.8            60              1007.             4.93              3.9
 4 arc1047 2019-11-15 09:15:00    -37.8      145.             14.4            66.1            1008.             7.85              4.5
 5 arc1050 2019-11-15 09:15:00    -37.8      145.             14.1            64.7            1007.             5.8               3.9
 6 arc1045 2019-11-15 09:30:00    -37.8      145.             15.4            57.1            1007.             4.43              3.8
 7 arc1046 2019-11-15 09:30:00    -37.8      145.             14.8            63.2            1007.             7.6               4.5
 8 arc1047 2019-11-15 09:30:00    -37.8      145.             15.2            62.7            1008              7.13              3.6
 9 arc1048 2019-11-15 09:30:00    -37.8      145.             14.6            62.2            1007.             7.09              4.7
10 arc1050 2019-11-15 09:30:00    -37.8      145.             14.6            62.5            1007              5.94              3.5

I mapped the 5 sites using leaflet.

leaflet(quarter_hour_readings) %>%
      addTiles() %>%
      addCircleMarkers(
        layerId = ~site_id,
        label = ~site_id)

enter image description here

And now want to include radial(spider) plots on each of the markers on the map, upon selecting a single date. For now I have filtered out the data values at a single date, for the following radial plot.

library(fmsb)

dat <- rbind(c(85.00,100.00,2000.00,160.00,999.9,1999.9),
      c(-40.00,0.00,10.00,0.00,0.00,0.00),
      quarter_hour_readings %>%
        filter(date_time == as.POSIXct("2019-11-15 09:15:00",tz="UTC")) %>%
        column_to_rownames(var="site_id") %>%
        select(c("ambient_air_temperature","relative_humidy","barometric_pressure", "average_wind_speed", "particulate_density_2.5", "particulate_density_10")))
  
radarchart(dat)

enter image description here

I am however unsure how to include these raidal plots on the respective markers on the map and if there was an easier way to handle this. Although I found this package to insert minicharts on leaflet maps, I wasn't able to find how to add radar plots on a map.

1

There are 1 answers

0
thothal On

Note. Since you did not provide a reproducible dataset, I take some fake data.

You can follow the approach described here:

m <- leaflet() %>% addTiles()

rand_lng <- function(n = 5) rnorm(n, -93.65, .01)
rand_lat <- function(n = 5) rnorm(n, 42.0285, .01)

rdr_dat <- structure(list(total = c(5, 1, 2.15031008049846, 4.15322054177523, 
                                    2.6359076872468), 
                          phys = c(15, 3, 12.3804132539814, 6.6208886719424, 
                                   12.4789917719968), 
                          psycho = c(3, 0, 0.5, NA, 3), 
                          social = c(5, 1, 2.82645894121379, 
                                     4.82733338139951, 2.81333662476391), 
                          env = c(5, 1, 5, 2.5, 4)), 
                     row.names = c(NA, -5L), class = "data.frame")

makePlotURI <- function(expr, width, height, ...) {
   pngFile <- plotPNG(function() { expr }, width = width, height = height, ...)
   on.exit(unlink(pngFile))
   base64 <- httpuv::rawToBase64(readBin(pngFile, raw(1), file.size(pngFile)))
   paste0("data:image/png;base64,", base64)
}

set.seed(1)
plots <- data.frame(lat = rand_lat(), 
                    lng = rand_lng(),
                    radar = rep(makePlotURI({radarchart(rdr_dat)}, 200, 200, bg = "white"), 5))

m %>% addMarkers(icon = ~ icons(radar), data = plots)

Leaflet with Radarchert