How do you animate a choropleth in RShiny over time using leaflet? I would like to animate the color of choropleths over time in Shiny according to their numeric value.
Below is what I have tried, but the choropleths don't change color after hitting the play button on the slider (or changing the date on the slider). I would like for the choropleths to change color with year as the input.
Here is a link to example that I am looking to replicate, but I cannot seem to get it to work on my end: https://towardsdatascience.com/eye-catching-animated-maps-in-r-a-simple-introduction-3559d8c33be1
Here is the shiny app code:
library(shiny)
library(shiny)
ui <- fluidPage(
titlePanel("Chamonix Snow Development"),
sidebarPanel(width = 2,
sliderInput("dateSel", "Date",
min = min(snow_total_year_abs$year),
max = max(snow_total_year_abs$year),
value = min(snow_total_year_abs$year),
step = 1,
timeFormat = "%d %b %y",
animate = animationOptions(interval = 200, loop = FALSE)
)
),
mainPanel(width = 10,
leafletOutput("map", width = "70%", height = "750px")
)
)
server <- function(input, output, session) {
output$map = renderLeaflet({
leaflet(stations %>% filter(year==min(stations$year))) %>%
addTiles() %>% addPolygons( data = vcrop,
weight=2, opacity = 1,
color = stations$color,
fillOpacity = 0.5)
})
observe({
leafletProxy({
leafletProxy("map", data = stations %>% filter(year==input$dateSel))
})
})
}
shinyApp(ui, server)
Here is the code getting the coordinate data into polygon form:
test = ac_ml_join %>% filter(Name=="Admont" | Name =="Fieberbrunn")
stations = st_as_sf(test,coords = c("Longitude","Latitude"))
#create voronoi/thiessen polygons
v <- stations %>%
st_union() %>%
st_voronoi() %>%
st_collection_extract()
box <- st_bbox( stations ) %>% st_as_sfc()
vparts = st_collection_extract(v)
vcrop = st_crop(vparts, box)
leaflet(stations) %>%
addTiles() %>%
#addCircleMarkers( data = stations ) %>%
addPolygons( data = vcrop,fillColor = stations$color,
weight=2, opacity = 1,
color = stations$color,
fillOpacity = 0.5)
'Test' dataset:
year max.snow Name `3yr_roll_avg` snow.vis color Longitude Latitude
1971 4.35 Absdorf 2.93 100 royalblue 16.0 48.4
1972 2.28 Absdorf 3.07 105. royalblue 16.0 48.4
1973 2.58 Absdorf 1.62 55.2 red 16.0 48.4
1974 0 Absdorf 1.16 39.6 darkred 16.0 48.4
1975 0.903 Absdorf 0.508 17.3 darkred 16.0 48.4
1976 0.621 Absdorf 3.31 113. royalblue 16.0 48.4
1977 8.42 Absdorf 5.44 186. purple 16.0 48.4
1978 7.29 Absdorf 8.04 274. purple 16.0 48.4
1979 8.42 Absdorf 7.41 253. purple 16.0 48.4
1980 6.52 Absdorf 6.02 205. purple 16.0 48.4
1971 20.9 Admont 8.77 100 royalblue 14.5 47.6
1972 3.6 Admont 26.4 301. purple 14.5 47.6
1973 54.7 Admont 29.7 339. purple 14.5 47.6
1974 30.9 Admont 42.8 488. darkred 14.5 47.6
1975 42.9 Admont 39.6 451. purple 14.5 47.6
1976 45.0 Admont 43.2 492. purple 14.5 47.6
1977 41.7 Admont 42.5 485. red 14.5 47.6
1978 40.9 Admont 37.3 426. purple 14.5 47.6
1979 29.4 Admont 40.7 464. purple 14.5 47.6
1980 51.8 Admont 56.8 647. purple 14.5 47.6
ac_ml_join
come from?