R/fct_plotly_helpers.R

Defines functions ggplot_config plotly_config

################### Text Parameters for Charts #######################

# Text parameters for charts
chart_title = list(size = 16, color = "rgb(100,100,100)", family = "Verdana")
chart_axis = list(size = 14, color = "rgb(100,100,100)", family = "Verdana")
# chart_axis_bar = list(size = 14, color = "#38839C", family = "Verdana", face = "bold")
# New color - Text parameters for charts. Also replaced #428BCA with #4fb9db
chart_axis_bar = list(size = 14, color = "#4fb9db", family = "Verdana", face = "bold")

################### Colors and Factors #######################
# assigning colors for crash severity and gender for charts/map
# color_map_svr <- c("Fatal"="#D50032", "Injury"="#38839C", "Property Damage"="#4DB848")
# color_map_gender <- c("Female"="#D50032", "Male"="#38839C", "Unknown" = "#F9C218")
# color_map_wisinj <- c("Suspected Minor Injury" ="#4DB848", "Possible Injury" = "#38839C","Suspected Serious Injury" ="#7f42ca", "Fatal Injury" = "#D50032")

fatal_red = "#DB7E65"
inj_blue = "#4AAECF"
pd_green = "#44DBAE"
bg_color = "#f8f8f8"

# New colors
color_map_svr <- c("Fatal"=fatal_red, "Injury"=inj_blue, "Property Damage"= pd_green)
color_map_gender <- c("Female"=fatal_red, "Male"="#4fb9db", "Unknown" = "#dbb039")
color_map_wisinj <- c("Suspected Minor Injury" =inj_blue, "Possible Injury" = "#58CEF5","Suspected Serious Injury" ="#3D8DA8", "Fatal Injury" = "#265869")
color_map_waffle_inj <- c("BicycleInjured" = "#3D8DA8", "BicycleKilled" = fatal_red, "PedestrianInjured" = inj_blue, "PedestrianKilled" = fatal_red)
# Factor levels
wisinj_factor_levels <- c("Possible Injury", "Suspected Minor Injury", "Suspected Serious Injury", "Fatal Injury")

day_factor_levels <- c("Sunday", "Monday", "Tuesday","Wednesday","Thursday","Friday","Saturday") #newtime
newtime_factor_levels = c("12am","1am","2am","3am", "4am","5am", "6am","7am","8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm",
      "5pm","6pm","7pm","8pm","9pm","10pm","11pm")   

age_group_factor_levels <-c("0-4",
"5-9",
"10-14",
"15-19",
"20-24",
"25-29",
"30-34",
"35-39",
"40-44",
"45-49",
"50-54",
"55-59",
"60-64",
"65-69",
"70+")
# assigning colors for crash severity for map
# color_map_svr <- c("Fatal"="#D50032", "Injury"="#428BCA", "Property Damage"="#4DB848")
# New colors
crshsvr_factor_levels <- c("Property Damage", "Injury", "Fatal") # So Fatals will be on top in the map
# wisinj_factor_levels <- c("Possible Injury", "Suspected Minor Injury", "Suspected Serious Injury", "Fatal Injury")

# Import fontawesome icons for waffle chart - yes, it is needed
# extrafont::fonttable() |> dplyr::as_tibble() |> dplyr::filter(grepl("Awesom", FamilyName)) |> dplyr::select(FamilyName, FontName, fontfile)
sysfonts::font_add(family = "FontAwesome5Free-Solid", regular = "inst/app/www/fonts/FontAwesome5Free-Solid.ttf")
sysfonts::font_add(family = "FontAwesome5Free-Regular", regular = "inst/app/www/fonts/FontAwesome5Brands-Regular.ttf")
sysfonts::font_add(family = "FontAwesome5Brands-Regular", regular = "inst/app/www/fonts/fontawesome-webfont.ttf")
showtext::showtext_auto()


plotly_config <- function(p, chart_name) {
  p |>
    plotly::config(
      # toImageButtonOptions = list(
      #   width = 800,
      #   height = 800,
      #   filename = chart_name,
      #   scale = 2),
        # modeBarButtonsToRemove = c("toImage"),
        displayModeBar = FALSE,
        displaylogo = FALSE
    ) |>
    plotly::layout(plot_bgcolor = 'rgba(0,0,0,0)',
                   # make transparent background
                   paper_bgcolor = 'rgba(0,0,0,0)')
}

ggplot_config <- function(base_size) {
  ggplot2::theme_void(base_size = base_size) %+replace%
    ggplot2::theme(
      complete = TRUE,
     # panel.background = ggplot2::element_rect(fill =  bg_color),
      # panel.border = ggplot2::element_rect(color = bg_color, fill=NA, linewidth = 1, linetype = 0),
      # panel.border = ggplot2::element_blank(),
      plot.background = ggplot2::element_rect(fill =  bg_color, color = bg_color),
      axis.title = ggplot2::element_blank(),
      axis.text = ggplot2::element_blank(),
      axis.line = ggplot2::element_blank(),
      axis.ticks = ggplot2::element_blank(),
      # strip.background=element_rect(fill=bg_color, colour="transparent"),
      # plot.margin = margin(10, 10, 10, 10),
      legend.position = "none",
      plot.title = ggtext::element_markdown(size = 16)
    )
}

counties = c(
  "Adams" ,
  "Ashland" ,
  "Barron" ,
  "Bayfield" ,
  "Brown" ,
  "Buffalo" ,
  "Burnett" ,
  "Calumet" ,
  "Chippewa" ,
  "Clark" ,
  "Columbia" ,
  "Crawford" ,
  "Dane" ,
  "Dodge" ,
  "Door" ,
  "Douglas" ,
  "Dunn",
  "Eau Claire",
  "Florence" ,
  "Fond du Lac",
  "Forest",
  "Grant" ,
  "Green" ,
  "Green Lake",
  "Iowa" ,
  "Iron" ,
  "Jackson" ,
  "Jefferson",
  "Juneau" ,
  "Kenosha" ,
  "Kewaunee" ,
  "La Crosse",
  "Lafayette",
  "Langlade" ,
  "Lincoln" ,
  "Manitowoc",
  "Marathon" ,
  "Marinette",
  "Marquette",
  "Menominee",
  "Milwaukee",
  "Monroe" ,
  "Oconto" ,
  "Oneida" ,
  "Outagamie",
  "Ozaukee" ,
  "Pepin" ,
  "Pierce" ,
  "Polk",
  "Portage" ,
  "Price" ,
  "Racine" ,
  "Richland",
  "Rock",
  "Rusk",
  "St. Croix",
  "Sauk",
  "Sawyer",
  "Shawano",
  "Sheboygan",
  "Taylor",
  "Trempealeau",
  "Vernon",
  "Vilas",
  "Walworth",
  "Washburn",
  "Washington",
  "Waukesha",
  "Waupaca",
  "Waushara" ,
  "Winnebago",
  "Wood"
)
jacciz/shiny_wisdot_crash_dashboard documentation built on May 4, 2023, 11:36 a.m.