Advertisement
Guest User

Untitled

a guest
Jun 26th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. ## data
  2. ra_ft = structure(list(jurisdiction_id = c(258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L), hazard_name = c("Pandemic Influenza", "Hurricane/Tropical Storm", "Extreme Heat", "Nuclear Attack", "Drought ", "Flood", "Windstorm ", "Biological Disease Outbreak", "Power Failure ", "Biological Terrorism - Communicable (including A - B - C agents)", "Mass Casualty Incidents", "Biological Terrorism - Non-Communicable (including A - B - C agents)", "Water Supply Contamination - environmental", "Food Borne Disease", "Extreme Cold"), hazard_risk_index = c(152.17, 139.72, 82.09, 55.84, 56.81, 61.47, 58.19, 60.4, 51.72, 49.37, 53.08, 45.57, 47.89, 50.29, 47.83), residual_risk_index = c(11.22, 9.62, 6.08, 5.1, 4.76, 4.53, 4.52, 4.41, 4.41, 4.08, 3.93, 3.82, 3.82, 3.59, 3.49), probability_score = c(3, 3.22, 2.65, 1, 2.5, 1.87, 2.62, 1.33, 2.29, 1, 1.72, 1, 1.18, 1.3, 2.53)), row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"))
  3.  
  4. library(tidyverse)
  5. prob_factor = with(ra_ft, max(hazard_risk_index) / max(probability_score))
  6.  
  7. long_dat = ra_ft |>
  8. mutate(hazard_name = reorder(hazard_name, -residual_risk_index)) |>
  9. pivot_longer(cols = c("hazard_risk_index", "residual_risk_index", "probability_score"))
  10.  
  11. ggplot(
  12. data = long_dat |> filter(name != "probability_score"),
  13. aes(x = hazard_name, y = value, fill = name)
  14. ) +
  15. geom_col(
  16. #aes(fill = name),
  17. position = position_dodge()
  18. ) +
  19. geom_point(
  20. data = long_dat |> filter(name == "probability_score"),
  21. aes(y = value * prob_factor),
  22. #color = "red4"
  23. shape = 21
  24. ) +
  25. scale_fill_manual(
  26. values = c(
  27. hazard_risk_index = "cadetblue2",
  28. residual_risk_index = "yellow3",
  29. probability_score = "red4"
  30. ),
  31. limits = c(
  32. "hazard_risk_index",
  33. "residual_risk_index",
  34. "probability_score"
  35. ),
  36. labels = \(x) {x |> str_replace_all("_", " ") |> str_to_title()}
  37. ) +
  38. scale_y_continuous(
  39. name = "Risk Indices",
  40. sec.axis = sec_axis(~ . / prob_factor, name = "Probability Score"),
  41. expand = expansion(mult = c(0, 0.1))
  42. ) +
  43. scale_x_discrete(
  44. labels = \(x) str_replace(x, pattern = fixed("("), replacement = "\n(")
  45. ) +
  46. labs(
  47. x = NULL,
  48. fill = NULL,
  49. title = "Hazard Probability, Residual Risk, and Probability Score"
  50. ) +
  51. ## putting the legend at the bottom this way requires
  52. ## ggplot2 version 3.5.0 or higher
  53. guides(fill = guide_legend(
  54. position = "bottom",
  55. label.position = "bottom"
  56. )) +
  57. theme_bw() +
  58. theme(
  59. panel.grid.major.x = element_blank(),
  60. panel.border = element_blank(),
  61. axis.text.x = element_text(angle = 45, hjust = 1),
  62. axis.ticks = element_line(color = "gray85"),
  63. plot.margin = margin(t = 5.5, r = 5.5, b = 10, l = 20, unit = "pt")
  64. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement