# Load package
library(dplyr)
library(ggplot2)

# Import data
data <- read.csv("/resources/rstudio/BusinessStatistics/data/NH.csv") # 1st row is the description of variables

# Convert to factors: some are imported as integers
data <- 
  data %>%
  mutate_if(is.integer, factor)

# Basic descriptive statistics of data
summary(data)
##      County                   DoNo        CaseType      FileDate  
##  BCDD   : 58   211-2015-CV-00001:  1   2      :113   42492  :  8  
##  Belknap:351   211-2015-CV-00003:  1   37     : 38   42060  :  7  
##                211-2015-CV-00004:  1   38     : 37   42510  :  6  
##                211-2015-CV-00012:  1   4      : 29   42138  :  5  
##                211-2015-CV-00013:  1   35     : 24   42620  :  5  
##                211-2015-CV-00014:  1   47     : 22   42639  :  5  
##                (Other)          :403   (Other):146   (Other):373  
##                                       PNames   
##  Discover Bank                           : 14  
##  Paugus Bay Plaza Condominium Association:  8  
##  Barcklay Bank Delaware                  :  7  
##  American Express Centurion Bank         :  6  
##  Concord Hospital                        :  6  
##  Bank of New Hampshire                   :  5  
##  (Other)                                 :363  
##                               DNames          ProSe    
##  Town of Gilford                 :  6   Both     : 23  
##  Barry K. Meyers; Brenda M. Stowe:  2   Defendant:131  
##  Barry Myers                     :  2   Neither  :216  
##  Brazilian Resources, Inc.       :  2   Plaintiff: 19  
##  City of Laconia                 :  2   Some DF  : 20  
##  LRG Healthcare                  :  2                  
##  (Other)                         :393                  
##                   PLaw                                PFirm    
##  9                  : 42   9                             : 42  
##  Robert L. O'Brien  : 24   Schlee & Stillman, LLC        : 24  
##  Michael J. Fontaine:  9   Welts, White, & Fontaine, P.C.: 16  
##  Benjamin R. Roberge:  8   Zwicker & Associates, P.C.    : 16  
##  Edward D. Philpot  :  8   Donais Law Offices, PLLC      :  8  
##  Harvey J. Garod    :  6   Martin, Lord, & Osman, P.A.   :  8  
##  (Other)            :312   (Other)                       :295  
##      EqReq         MonReq             LoC                        DLaw    
##         :  1   Min.   :      0   0      :332   9                   :156  
##  9      :  2   1st Qu.:      1   15     :  7   Joseph D. Becher    :  5  
##  No     : 55   Median :      1   3      :  6   Daniel J. Orroth    :  4  
##  Unknown:  4   Mean   :  11819   8      :  6   Gary M. Burt        :  4  
##  Yes    :346   3rd Qu.:   1670   10     :  6   Laura Spector-Morgan:  4  
##  NA's   :  1   Max.   :1118000   11     :  6   (Other)             :235  
##                                  (Other): 46   NA's                :  1  
##                                         DFirm    
##  9                                         :156  
##  Getman, Schulthess, Steere, & Poulin, P.A.: 11  
##  Mitchell Municipal Group, P.A.            :  8  
##  Primmer, Piper, Eggleston, & Cramer, P.C. :  8  
##  Wescott Law, P.A.                         :  8  
##  (Other)                                   :217  
##  NA's                                      :  1  
##                             X3d                         X3dLaw   
##  0                            :392   0                     :397  
##  Andrew Howe; Martina Howe    :  4   Ethan G. Wood         :  4  
##  Donald A. Kennedy            :  3   Donald A. Kennedy     :  3  
##  Angela Pearl                 :  2   David W. Johnston     :  1  
##  Belknap County Superior Court:  1   Frank P. Spinella, Jr.:  1  
##  Brenda Stowe                 :  1   Gregg M. Charest      :  1  
##  (Other)                      :  6   (Other)               :  2  
##                             X3dFirm    CtrCl   CrsCl  
##  0                              :397   0:369   0:404  
##  Patrick Wood Law Office, PLLC  :  4   1: 39   1:  4  
##  Law Office of Donald A. Kennedy:  2   2:  1   2:  1  
##  Devine, Millimet & Branch, P.A.:  1                  
##  Frasca & Frasca Law Offices    :  1                  
##  Law Office of Gregg M. Charest :  1                  
##  (Other)                        :  3                  
##                   Rec     
##  Ignatius           :  4  
##  MacLeod; Borenstein:  1  
##  Na                 :  1  
##  O'Neill            :  5  
##  NA's               :398  
##                           
##                           
##                                                                                               Rem     
##  HRC                                                                                            :  1  
##  Made the appeal and then withdrew, so NA (thought you'd like to know… the guy sued NH and won!):  1  
##  Removed                                                                                        : 13  
##  Transfer                                                                                       :  4  
##  Transferred                                                                                    :  2  
##  NA's                                                                                           :388  
##                                                                                                       
##                 Judge                                    OthMot   
##  O'Neill           :260   None                              :125  
##  McNamara          : 32   Motion for Entry of Final Judgment: 24  
##  O'Neill; Fauver   :  5   Motion for Voluntary Nonsuit      : 16  
##  Anderson; McNamara:  4   Assented to Motion to Continue    : 10  
##  Colburn; McNamara :  4   Motion to Strike                  :  6  
##  (Other)           : 52   Motion for Entry of Final Decree  :  5  
##  NA's              : 52   (Other)                           :223  
##   MTime       MTA           MTD        DSJ       PSJ         DMIL    
##  0   :284   0   :353   0      :342   0   :378   0  :384   0    :400  
##  1   : 18   1   : 51   1      : 22   1   :  4   1  : 13   1; 10:  1  
##  1; 2:  5   2   :  4   3      : 16   10  : 17   1;3:  1   1; 3 :  1  
##  1;2 :  1   NA's:  1   10     : 11   2   :  1   10 :  4   10   :  4  
##  2   :101              2      :  6   2; 4:  1   3  :  6   2; 4 :  1  
##                        1; 3   :  5   3   :  7   6  :  1   3; 10:  1  
##                        (Other):  7   4   :  1             4    :  1  
##  PMIL     Stp       Arb      TrialType      DISP         JMent    
##  0 :403   0:236   No  : 66   0: 14     3      :184   1      : 93  
##  6 :  1   1:131   Yes : 55   1:  1     8      : 74   2      : 64  
##  10:  5   2: 42   NA's:288   2:  4     7      : 42   9      : 15  
##                              8:  1     1      : 38   0      :  3  
##                              9:389     6      : 37   1; NA  :  1  
##                                        (Other): 33   (Other):  2  
##                                        NA's   :  1   NA's   :231  
##    NOJ          Award             Eq         TermDate   Enforce   
##  0   :260   Min.   :      0   0    : 73   Open   : 19   0   :377  
##  1   : 73   1st Qu.:      0   1    :  3   42093  :  5   1   : 11  
##  1; 0:  1   Median :      1   2    :  2   42263  :  5   1; 2:  3  
##  2   :  3   Mean   :  11170   3    :  6   42619  :  5   1; 3:  1  
##  3   :  8   3rd Qu.:      1   9    :324   42579  :  4   2   :  4  
##  9   : 62   Max.   :1700000   34000:  1   (Other):370   3   : 13  
##  NA's:  2                                 NA's   :  1
# Sturecture of data
str(data)
## 'data.frame':    409 obs. of  40 variables:
##  $ County   : Factor w/ 2 levels "BCDD","Belknap": 2 2 2 2 2 2 2 2 2 2 ...
##  $ DoNo     : Factor w/ 409 levels "211-2015-CV-00001",..: 136 137 138 139 140 141 142 143 144 145 ...
##  $ CaseType : Factor w/ 41 levels "1","10","10; 26",..: 9 13 28 28 22 11 11 11 11 11 ...
##  $ FileDate : Factor w/ 269 levels "2015-07031","41073",..: 133 133 134 134 135 136 136 137 138 138 ...
##  $ PNames   : Factor w/ 344 levels "A.O. Phaneuf & Son Funeral Home and Crematorium, Inc.; Cremation Society of New Hampshire, Inc.; Arthur Phaneuf",..: 21 236 259 259 15 71 72 157 207 12 ...
##  $ DNames   : Factor w/ 398 levels "223 D.W. Highway, LLC",..: 347 374 208 239 123 358 149 285 1 66 ...
##  $ ProSe    : Factor w/ 5 levels "Both","Defendant",..: 3 3 2 2 2 3 2 3 3 3 ...
##  $ PLaw     : Factor w/ 228 levels "9","A. Gerard O'Neil, Jr.",..: 209 118 16 16 84 151 151 62 202 190 ...
##  $ PFirm    : Factor w/ 171 levels "9","Abrahamsen Ratchford, P.C.",..: 156 94 45 45 121 168 168 123 83 136 ...
##  $ EqReq    : Factor w/ 5 levels "","9","No","Unknown",..: 5 5 5 5 5 5 5 5 5 5 ...
##  $ MonReq   : num  1 1 1436 2159 4928 ...
##  $ LoC      : Factor w/ 25 levels "0","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ DLaw     : Factor w/ 212 levels "9","Allison M. Ambrose",..: 107 110 1 1 1 172 1 156 1 1 ...
##  $ DFirm    : Factor w/ 162 levels "9","Azarian Law Office, PLLC",..: 132 99 1 1 1 81 1 148 1 1 ...
##  $ X3d      : Factor w/ 12 levels "0","Andrew Howe; Martina Howe",..: 1 2 1 1 1 1 1 1 1 1 ...
##  $ X3dLaw   : Factor w/ 8 levels "0","David W. Johnston",..: 1 4 1 1 1 1 1 1 1 1 ...
##  $ X3dFirm  : Factor w/ 9 levels "0","Devine, Millimet & Branch, P.A.",..: 1 7 1 1 1 1 1 1 1 1 ...
##  $ CtrCl    : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
##  $ CrsCl    : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Rec      : Factor w/ 4 levels "Ignatius","MacLeod; Borenstein",..: NA NA NA NA NA NA NA NA NA NA ...
##  $ Rem      : Factor w/ 5 levels "HRC","Made the appeal and then withdrew, so NA (thought you'd like to know… the guy sued NH and won!)",..: NA NA NA NA NA NA NA NA NA NA ...
##  $ Judge    : Factor w/ 37 levels "Abramson; Brown; McNamara",..: NA 26 NA 26 26 26 36 26 26 NA ...
##  $ OthMot   : Factor w/ 216 levels "Assented Motion to Continue",..: 216 172 216 216 13 43 44 139 216 216 ...
##  $ MTime    : Factor w/ 5 levels "0","1","1; 2",..: 1 1 1 2 1 1 1 5 1 1 ...
##  $ MTA      : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 2 2 1 2 1 ...
##  $ MTD      : Factor w/ 11 levels "0","1","1; 3",..: 1 2 1 1 2 1 1 2 1 1 ...
##  $ DSJ      : Factor w/ 7 levels "0","1","10","2",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ PSJ      : Factor w/ 6 levels "0","1","1;3",..: 1 1 1 1 1 2 1 1 1 1 ...
##  $ DMIL     : Factor w/ 7 levels "0","1; 10","1; 3",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ PMIL     : Factor w/ 3 levels "0","6","10": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Stp      : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Arb      : Factor w/ 2 levels "No","Yes": NA NA NA NA NA NA NA NA NA NA ...
##  $ TrialType: Factor w/ 5 levels "0","1","2","8",..: 5 5 5 5 3 5 5 5 5 5 ...
##  $ DISP     : Factor w/ 12 levels "1","10","2","2; 3",..: 5 1 10 10 1 2 9 1 10 10 ...
##  $ JMent    : Factor w/ 7 levels "0","1","1; NA",..: NA 4 NA NA NA NA 2 4 NA NA ...
##  $ NOJ      : Factor w/ 6 levels "0","1","1; 0",..: 6 6 6 6 6 6 4 6 6 6 ...
##  $ Award    : num  1 0 0 0 0 0 34000 0 0 0 ...
##  $ Eq       : Factor w/ 6 levels "0","1","2","3",..: 5 5 5 5 5 5 6 5 5 5 ...
##  $ TermDate : Factor w/ 296 levels "41186","41402",..: 122 122 103 111 138 296 296 151 130 105 ...
##  $ Enforce  : Factor w/ 6 levels "0","1","1; 2",..: 1 1 1 1 1 1 2 1 1 1 ...

For our term project we took a look at if the equity requested was granted for the county the trial took place. Our first three graphs show only if the equity was granted, we then filter counties which makes our data more complex and gives us a better story for how equity was granted or not for each county. Lastly, we add the case type which gives us an even better story as to which cases in the given county were more likely to have equity granted.

ggplot(data, aes(x = EqReq)) +
  geom_bar()

library(forcats)

# Plot one variable
data %>%
  ggplot(aes(x =  EqReq)) +
  geom_bar() +
  coord_flip()


# Filter for Yes and No
data %>%
  # Filter out NA
  filter(EqReq %in% c("Yes", "No")) %>%
  ggplot(aes(x =  EqReq)) +
  geom_bar() +
  coord_flip()


# Add 2nd variable to color
data %>%
  # Filter out NA
  filter(EqReq %in% c("Yes", "No")) %>%
  ggplot(aes(x =  EqReq, fill = County)) +
  geom_bar() +
  coord_flip()


# Plot proportion
data %>%
  # Filter out NA
  filter(EqReq %in% c("Yes", "No")) %>%
  ggplot(aes(x =  EqReq, fill = County)) +
  geom_bar(position = "fill") + #position = "fill", to have a stacked barchart
  coord_flip()


# Add 3rd variable
data %>%
  # Filter out NA
  filter(EqReq %in% c("Yes", "No")) %>%
  ggplot(aes(x =  EqReq, fill = County)) +
  geom_bar(position = "fill") + #position = "fill", to have a stacked barchart
  coord_flip() +
  facet_wrap(~ CaseType)


# Narrow down to top few categories
data %>%
         # Lump together least common factor levels into "other"
  mutate(CaseType = fct_lump(CaseType, 3),
         # Sort case type in frequency
         CaseType = fct_infreq(CaseType)) %>%
  # Filter out NA
  filter(EqReq %in% c("Yes", "No")) %>%
  ggplot(aes(x =  EqReq, fill = County)) +
  geom_bar(position = "fill") + #position = "fill", to have a stacked barchart
  coord_flip() +
  facet_wrap(~ CaseType)