collapsibletree 01

2021-05-08

setwd("J:/FARS/Data")
aa= read.csv("back16.csv")
dim(aa)
## [1] 144  14
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# all character columns to factor:
aa <- mutate_if(aa, is.character, as.factor)
summary(aa)
##                      HARM_EVNAME
##  Pedestrian                :66  
##  Motor Vehicle In-Transport:49  
##  Rollover/Overturn         : 7  
##  Curb                      : 3  
##  Pedalcyclist              : 3  
##  Fell/Jumped from Vehicle  : 2  
##  (Other)                   :14  
##                                           MAN_COLLNAME
##  Angle                                          :31   
##  Front-to-Front                                 : 2   
##  Front-to-Rear                                  :12   
##  Not a Collision with Motor Vehicle In-Transport:95   
##  Other                                          : 2   
##  Rear-to-Side                                   : 2   
##                                                       
##                                                                                         BODY_TYPNAME
##  Standard pickup (GVWR 4,500 to 10,00 lbs.)(Jeep Pickup, Comanche, Ram Pickup, D100-D350,....):23   
##  Truck-tractor (Cab only, or with any number of trailing unit; any weight)                    :22   
##  4-door sedan, hardtop                                                                        :21   
##  Compact Utility (Utility Vehicle Categories "Small" and "Midsize")                           :21   
##  Large utility (ANSI D16.1 Utility Vehicle Categories and "Full Size" and "Large")            : 8   
##  Single-unit straight truck or Cab-Chassis (GVWR > 26,000 lbs.)                               : 8   
##  (Other)                                                                                      :41   
##                                                     V_CONFIGNAME
##  Not Applicable                                           :101  
##  Single-Unit Truck (2- axle and GVWR more than 10,000 lbs): 10  
##  Single-Unit Truck (3 or more axles)                      :  8  
##  Truck Pulling Trailer(s)                                 :  2  
##  Truck Tractor (Bobtail)                                  :  1  
##  Truck Tractor/Semi-Trailer                               : 21  
##  Unknown                                                  :  1  
##        TRAV_SPNAME            DEFORMEDNAME                          M_HARMNAME
##  Not Reported:75   Disabling Damage :35    Pedestrian                    :66  
##  005 MPH     :25   Functional Damage:28    Motor Vehicle In-Transport    :52  
##  Unknown     :16   Minor Damage     :26    Rollover/Overturn             : 6  
##  010 MPH     : 8   No Damage        :38    Tree (Standing Only)          : 5  
##  015 MPH     : 4   Not Reported     :16    Fire/Explosion                : 3  
##  002 MPH     : 2   Unknown          : 1    Immersion or Partial Immersion: 3  
##  (Other)     :14                           (Other)                       : 9  
##     DR_WGTNAME                          DR_SF1NAME
##  Unknown :68   None                          :56  
##  170 lbs.: 7   Starting or Backing Improperly:49  
##  150 lbs.: 4   Careless Driving              :10  
##  140 lbs.: 3   Failure to Yield Right-of-Way : 8  
##  160 lbs.: 3   Unknown                       : 8  
##  165 lbs.: 3   Aggressive Driving / Road Rage: 2  
##  (Other) :56   (Other)                       :11  
##                                                     VTRAFWAYNAME
##  Two-Way, Not Divided                                     :93   
##  Non-Trafficway or Driveway Access                        :21   
##  Two-Way,  Divided, Positive  Median Barrier              : 9   
##  Two-Way,  Divided, Unprotected (Painted > 4 Feet ) Median: 9   
##  One-Way Trafficway                                       : 7   
##  Unknown                                                  : 2   
##  (Other)                                                  : 3   
##                             VNUM_LANNAME
##  Five lanes                       : 4   
##  Four lanes                       : 7   
##  Non-Trafficway or Driveway Access:21   
##  Not Reported                     : 1   
##  One lane                         : 7   
##  Three lanes                      : 5   
##  Two lanes                        :99   
##                                                VSPD_LIMNAME
##  25 MPH                                              :24   
##  No Statutory Limit/Non-Trafficway or Driveway Access:22   
##  55 MPH                                              :21   
##  Not Reported                                        :11   
##  30 MPH                                              :10   
##  35 MPH                                              : 8   
##  (Other)                                             :48   
##                              VALIGNNAME 
##  Curve - Unknown Direction        :  3  
##  Curve Left                       :  4  
##  Curve Right                      :  2  
##  Non-Trafficway or Driveway Access: 21  
##  Not Reported                     :  3  
##  Straight                         :111  
##                                         
##                             VPROFILENAME
##  Downhill                         : 4   
##  Grade, Unknown Slope             :10   
##  Hillcrest                        : 1   
##  Level                            :95   
##  Non-Trafficway or Driveway Access:21   
##  Not Reported                     : 6   
##  Uphill                           : 7
library(collapsibleTree)
library(data.tree)

collapsibleTree(
  aa,
  hierarchy = c("MAN_COLLNAME", "VPROFILENAME", "VSPD_LIMNAME"),
   width = 1000, height=800, fontSize = 16
)
collapsibleTreeSummary(
  aa,attribute = "leafCount", 
  hierarchy = c("MAN_COLLNAME", "VPROFILENAME", "VSPD_LIMNAME"),
  collapsed = FALSE, percentOfParent = TRUE, width = 1000, height=800, fontSize = 16
)
aa %>%
  group_by(MAN_COLLNAME, VPROFILENAME, VSPD_LIMNAME) %>%
  summarize(`Count` = n(), `Percentage`=`Count`/nrow(aa)*100) %>%
  collapsibleTreeSummary(
    hierarchy = c("MAN_COLLNAME", "VPROFILENAME", "VSPD_LIMNAME"),
    attribute = "Percentage", width = 1000, height=800, 
    zoomable = TRUE, fontSize = 16
  )
## `summarise()` regrouping output by 'MAN_COLLNAME', 'VPROFILENAME' (override with `.groups` argument)