变量可能完全彼此独立,例如“总人口”或“平均温度”,但其中一些将具有时间关系,例如三个或更多时间点上的“总人口”。
我正在使用ABS统计区域2之一的主要形状文件。以下是澳大利亚的人口密度(总人口/面积)以及悉尼的放大视图,以更好地传达我感兴趣的详细程度。
澳大利亚
![Australia](https://istack.dev59.com/I6XZo.webp)
![Sydney](https://istack.dev59.com/hrwDD.webp)
就Shiny和leaflet而言,我一直在做以下内容:
Before the
server
object is defined inserver.R
, I build a primary map object with all the desired 'layers'. That is, a leaflet with numerousaddPolygon()
calls to define the colouring of each 'layer' (group).# Create main map primary_map <- leaflet() %>% addProviderTiles( providers$OpenStreetMap.BlackAndWhite, options = providerTileOptions(opacity = 0.60) ) %>% # Layer 0 (blank) addPolygons( data = aus_sa2_areas, group = "blank" ) %>% # Layer 1 addPolygons( data = aus_sa2_areas, fillColor = ~palette_layer_1(aus_sa2_areas$var_1), smoothFactor = 0.5, group = "layer_1" ) %>%
...
# Layer N addPolygons( data = aus_sa2_areas, fillColor = ~palette_layer_n(aus_sa2_areas$var_n), smoothFactor = 0.5, group = "layer_n" ) %>% ...
All bar the first layer is then hidden using
hideGroup()
so that the initial rendering of the map doesn't look silly.hideGroup("layer_1") %>% hideGroup("layer_2") %>% ... hideGroup("layer_n")
In the Shiny app, using radio buttons (
layer_selection
), the user can select the 'layer' they'd like to see. I useobserveEvent(input$layer_selection, {})
to watch the status of the radio button options. To update the plot, I useleafletProxy()
andhideGroup()
to hide all the groups and thenshowGroup()
to unhide the selected layer.
问题:
How can I optimise my code? I am eager to make it more performant and/or easy to work with. I've found using
hideGroup()
's/showGroup()
for each layer selection is far faster than usingaddPolygon()
to a blank map, but this causes the app to take a very significant amount of time to load.Can I change the variable I am colouring the polygons by, without redrawing or adding those polygons again? To clarify, if I have 2 different variables to plot, both using the same shape data, do I have to do 2 distinct
addPolygon()
calls?Is there a more automatic way to sensibly colour the polygons for each layer according to a desired palette (from the viridis package?). Right now I'm finding defining a new palette for each variable, rather cumbersome, eg:
palette_layer_n <- colorNumeric( palette = "viridis", domain = aus_sa2_areas$aus_sa2_areas$var_n )
附加问题
ABS网站上的这个地图是如何工作的?它可以非常详细,同时响应速度也非常快。将Mesh Block的细节与SA2(2310个多边形)进行比较,例如下面的例子: