This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

library(waffle)
library(ggplot2)
library(extrafont)
library(magrittr)
# emojifont
pool <- pool::dbPool(RSQLite::SQLite(), dbname = "inst/app/www/crash_db.db")
person <-DBI::dbReadTable(pool, "person2017")

role <- person %>% dplyr::filter(ROLE %in% c("Bicyclist", "Pedestrian"), CNTYCODE == "13")
role_table <- table(role$ROLE)

w = waffle(role_table, rows = get_number_of_rows(role_table), legend_pos = "none", colors = c("blue", "green", "white"))


get_number_of_rows <- function(role_table) {
  if (is.na(role_table["Bicyclist"] | is.na(role_table["Pedestrian"]))){
    rows = 1
  }
  else if (role_table["Bicyclist"] == 1 |
      role_table["Pedestrian"] == 1) {
    rows = 1
  }
  else if
    (role_table["Bicyclist"] == 2 |
       role_table["Pedestrian"] == 2){
    rows = 2
    }
  else if
    (role_table["Bicyclist"] < 20 |
       role_table["Pedestrian"] <20){
    rows = 5
    }
  else if
    (role_table["Bicyclist"] >= 2 |
       role_table["Pedestrian"] >= 2){
    rows = 10
  }
  rows
}

bikeped_title <- sprintf(
  # not found font-family:Verdana
  "<span style='font-size:12pt;color:#666666'>%s pedestrians were <span style='color:#Db7e65;'><b>killed,</b></span>
          %s <span style='color:#4AAECF;'><b>injured</b><br></span>
          %s bicyclists were <span style='color:#Db7e65;'><b>killed,</b></span>
          %s <span style='color:#3D8DA8;'><b>injured</b></span>  </span>",
  2,
  3,
  4,
  5
)

w +   ggplot2::theme(
                  plot.title = ggtext::element_markdown()) + ggplot2::labs(title = bikeped_title)

# spec <- table(iris$Species)
# waffle(spec)
# fa_grep("spoon")
# shiny::icon("users")
role <- person %>% dplyr::filter(ROLE %in% c("Bicyclist", "Pedestrian"), countyname == "Dane")
role_table <- table(role$ROLE)

waffle(role_table, rows = get_number_of_rows(role_table), legend_pos = "none", colors = c("blue", "green", "white"))


get_number_of_rows <- function(role) {
  # if (is.na(role)){
  #   rows = 1
  # }
  if (role == 1) {
    rows = 1
  }
  else if
    (role== 2){
    rows = 2
    }
  else if
    (role< 20){
    rows = 5
    }
  else if
    (role >= 20){
    rows = 10
  }
  rows
}


bike <- person %>% filter(ROLE %in% c("Bicyclist"), CNTYCODE == "1", WISINJ !="No Apparent Injury")
bike_table <- table(bike$ROLE)
waffle(bike_table, get_number_of_rows(bike_table["Bicyclist"]), legend_pos = "none", use_glyph = "bicycle", colors = c("#4fb9db", "white"), glyph_size = 6)

ped <- person %>% dplyr::filter(ROLE %in% c("Pedestrian"), countyname == "Dane")
ped_table <- table(ped$ROLE)
waffle(ped_table, get_number_of_rows(ped_table["Pedestrian"]), legend_pos = "none", use_glyph = "walking", colors = c("#44dbae", "white"), glyph_size = 5)
# library(extrafontdb)
# extrafont::font_import (path="C:\\Users\\dotjaz\\Downloads\\", pattern = "fa-", prompt = FALSE)
# load.fontawesome(font = "C:/Users/dotjaz/Downloads/web/fa-regular-400.ttf")
# extrafont::loadfonts(device = "win") 
extrafont::fonttable() %>% dplyr::as_tibble() %>% dplyr::filter(grepl("Awesom", FamilyName)) %>% dplyr::select(FamilyName, FontName, fontfile)
# waffle(c(50, 30, 15, 5), rows = 5, use_glyph = "bolt", glyph_size = 4)

# library(showtext)
sysfonts::font_add(family = "FontAwesome5Free-Solid", regular = "C:\\Users\\dotjaz\\Downloads\\FontAwesome5Free-Solid.ttf")
# sysfonts::font_add(family = "FontAwesome5Free-Regular", regular = "C:\\Users\\dotjaz\\Downloads\\arcive\\FontAwesome5Brands-Regular.ttf")
# sysfonts::font_add(family = "FontAwesome5Brands-Regular", regular = "C:\\Users\\dotjaz\\Downloads\\fontawesome-webfont.ttf")

showtext::showtext_auto()
library(magrittr)
library(waffle)
role_table = person %>%
      dplyr::filter(.data$ROLE %in% c("Bicyclist", "Pedestrian"), .data$WISINJ != "No Apparent Injury") %>%
      dplyr::mutate(inj = ifelse(.data$WISINJ == "Fatal Injury", "Killed","Injured"),
             inj = factor(.data$inj, levels =c("Injured","Killed")),
             ROLE = factor(.data$ROLE, levels =c("Bicyclist","Pedestrian"))) %>% 
      dplyr::count(.data$ROLE, .data$inj, .drop = FALSE) %>% dplyr::mutate(for_colors = paste0(.data$ROLE, .data$inj)) %>%
      data.table::as.data.table()


 role_table %>% 
        ggplot2::ggplot(ggplot2::aes(label = .data$ROLE, values = .data$n, color = .data$for_colors)) +
        geom_pictogram(
                               n_rows = 12, # sqr root so we can make a square
                               size = 4,
                               # size = 4,
                               flip = TRUE,
                               # size = ".4vw",
                               show.legend = FALSE) +
   scale_label_pictogram()
role_table %>% 
  ggplot2::ggplot(ggplot2::aes(label = ROLE, values = n)) +
  waffle::geom_pictogram(n_rows = 10, ggplot2::aes(colour = ROLE), flip = TRUE, make_proportional = TRUE) +
  ggplot2::scale_color_manual(
    name = NULL,
    values = c("#a40000", "#c68958", "#ae6056"),
    labels = c("Fruit", "Sammich", "Pizza")
  ) +
  waffle::scale_label_pictogram(
    name = NULL,
    values = c("apple-alt", "bread-slice", "pizza-slice"),
    labels = c("Fruit", "Sammich", "Pizza")
  ) +
  # ggplot2::coord_equal() +
  # theme_ipsum_rc(grid="") +
  # waffle::theme_enhance_waffle() +
  ggplot2::theme(legend.key.height = unit(2.25, "line")) +
  ggplot2::theme(legend.text = element_text(size = 10, hjust = 0, vjust = 0.75))
role_table
bikeped_icons = c("Bicyclist" = "bicycle", "Pedestrian" = "walking")
role_table |> 
          ggplot2::ggplot() +
          waffle::geom_pictogram(ggplot2::aes(label = .data[["ROLE"]], values = .data[["n"]], color = .data[["for_colors"]]),
                                 n_rows = round(sqrt(sum(role_table[["n"]])), 0), # sqr root so we can make a square
                                 size = 5,
                                 # family = "FontAwesome5Free-Solid",
                                 # size = 4,
                                 flip = TRUE,
                                 # size = ".4vw",
                                 show.legend = FALSE) +
          # scale_label_pictogram()
          waffle::scale_label_pictogram(
            name = NULL,
            values = bikeped_icons[role_table[["ROLE"]]]
            # values = c("bicycle", "walking")
            # labels = c("Bicyclists", "Pedestrians")
          ) 


jacciz/shiny_wisdot_crash_dashboard documentation built on May 4, 2023, 11:36 a.m.