USAID-OHA-SI/glitr

Function around an achievement legend

Opened this issue · 0 comments

What is the most optimal way to include a legend into an achievement plot? We would want:

  • to create some sort of function that is a wrapper around scale_[fill/color]_identity and guides,
  • possibly user input to have a legend that returns the achv_desc (eg On target), achv_label (eg 90-110%), or combination (eg On target (90-110%)).
  • include all levels in the legend, possibly use scale_[fill/color]_manual(values = x, drop = FALSE) instead
  • figure out if there is a way to order the values correctly without having to make the user order anything themselves (the below code requires the user add a more complicated fill param, fill = factor(achv_color, achv_color_map$achv_color)))
library(tidyverse)
library(glitr)

df_t <- tibble::tribble(
            ~snu1, ~achievement,     ~achv_desc, ~achv_label, ~achv_color,
        "Midwest",         0.99,    "On Target",   "90-110%",   "#5BB5D5",
      "Northwest",         1.22, "Above Target",      "+110%",  "#697EBC",
  "Pacific Coast",         0.62,    "Concerned",      "<75%",   "#F8A27E"
  )


df_t %>% 
  ggplot(aes(achievement, snu1, 
             fill = factor(achv_color, achv_color_map$achv_color))) +
  geom_col() +
  scale_fill_identity(guide = "legend",
                      labels = achv_color_map$achv_desc,
                      breaks = achv_color_map$achv_color) +
  guides(fill = guide_legend(title = "Achievement")) +
  si_style()