cjerzak commited on
Commit
1a23181
·
verified ·
1 Parent(s): 03a0eb2

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +43 -14
app.R CHANGED
@@ -31,6 +31,11 @@
31
  # Clean up out-of-range values
32
  wealth_stack[wealth_stack <= 0 | wealth_stack > 1] <- NA
33
 
 
 
 
 
 
34
  # Load improvement data (change in IWI by state/province)
35
  improvement_data <- read_csv("poverty_improvement_by_state.csv")
36
 
@@ -78,7 +83,7 @@
78
  max = length(time_periods),
79
  value = 1,
80
  step = 1,
81
- animate = animationOptions(interval = 1500, loop = TRUE)
82
  ),
83
  # Show the currently selected year range clearly
84
  strong("Currently Selected: "),
@@ -300,22 +305,46 @@
300
  # (reactive to user-selected palette)
301
  # ----------------------------------
302
  color_pal <- reactive({
 
303
  palette_choice <- switch(
304
  input$color_palette,
305
- "viridis" = "viridis",
306
- "plasma" = "plasma",
307
- "magma" = "magma",
308
- "inferno" = "inferno",
309
- # Fallback to a Brewer palette for "Spectral"
310
  "Spectral" = "Spectral"
311
  )
312
- colorNumeric(
313
- palette = palette_choice,
314
- domain = c(0, 1), # Domain for map: 0 to 1
315
- na.color = "transparent"
 
 
 
316
  )
317
  })
318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
  # ----------------------------------
320
  # Display the currently selected time period (year range)
321
  # ----------------------------------
@@ -335,10 +364,10 @@
335
  setView(lng = 20, lat = 0, zoom = 3) %>% # Center on Africa
336
  addLegend(
337
  position = "bottomright",
338
- colors = color_pal()(legend_values),
339
- labels = sprintf("%.2f", legend_values),
340
- title = "IWI",
341
- opacity = 1
342
  )
343
  })
344
 
 
31
  # Clean up out-of-range values
32
  wealth_stack[wealth_stack <= 0 | wealth_stack > 1] <- NA
33
 
34
+ all_vals <- values(wealth_stack)
35
+ all_vals <- all_vals[!is.na(all_vals)]
36
+ q_breaks_legend <- quantile(all_vals, probs = seq(0, 1, 0.2), na.rm = TRUE)
37
+ q_breaks <- quantile(all_vals, probs = seq(0, 1, 0.1), na.rm = TRUE)
38
+
39
  # Load improvement data (change in IWI by state/province)
40
  improvement_data <- read_csv("poverty_improvement_by_state.csv")
41
 
 
83
  max = length(time_periods),
84
  value = 1,
85
  step = 1,
86
+ animate = animationOptions(interval = 3000, loop = TRUE)
87
  ),
88
  # Show the currently selected year range clearly
89
  strong("Currently Selected: "),
 
305
  # (reactive to user-selected palette)
306
  # ----------------------------------
307
  color_pal <- reactive({
308
+ # Switch the user selection to a palette name
309
  palette_choice <- switch(
310
  input$color_palette,
311
+ "viridis" = "viridis",
312
+ "plasma" = "plasma",
313
+ "magma" = "magma",
314
+ "inferno" = "inferno",
 
315
  "Spectral" = "Spectral"
316
  )
317
+
318
+ # Create a single palette across *all* data (all_vals) using quantiles:
319
+ colorBin(
320
+ palette = palette_choice,
321
+ domain = all_vals,
322
+ bins = q_breaks,
323
+ na.color = "transparent"
324
  )
325
  })
326
 
327
+ color_pal_legend <- reactive({
328
+ # Switch the user selection to a palette name
329
+ palette_choice <- switch(
330
+ input$color_palette,
331
+ "viridis" = "viridis",
332
+ "plasma" = "plasma",
333
+ "magma" = "magma",
334
+ "inferno" = "inferno",
335
+ "Spectral" = "Spectral"
336
+ )
337
+
338
+ # Create a single palette across *all* data (all_vals) using quantiles:
339
+ colorBin(
340
+ palette = palette_choice,
341
+ domain = all_vals,
342
+ bins = q_breaks_legend,
343
+ na.color = "transparent"
344
+ )
345
+ })
346
+
347
+
348
  # ----------------------------------
349
  # Display the currently selected time period (year range)
350
  # ----------------------------------
 
364
  setView(lng = 20, lat = 0, zoom = 3) %>% # Center on Africa
365
  addLegend(
366
  position = "bottomright",
367
+ pal = color_pal_legend(),
368
+ values = all_vals, # the entire distribution for the legend
369
+ title = "IWI",
370
+ opacity = 1
371
  )
372
  })
373