1. Read and set up Data Loyalty for SEM1

# Read Data directly
t = "F:/NGHIEN CUU SINH/NCS - PHUONG ANH/Part 2-Satisfaction and Loyalty/So lieu/So lieu - PA xu ly-for SEM/So lieu - PA xu ly-for SEM-27.2.2022/863-Loyaltyofbuspassenger_NonBus_Mising-outliersSEM1.csv"
SEM1 = read.csv(t, header = T)
head(SEM1)
##   ID MCCar MCMotor MCBus AGE CITY FRE TripPurpose Departure TimeUseonBus
## 1  3     0       0     1   1    2   1           2         0            4
## 2  4     0       0     1   1    2   2           4         0            4
## 3  5     0       0     1   1    2   1           2         1            4
## 4  6     0       0     1   1    2   1           2         1            1
## 5  7     0       0     1   1    2   1           2         1            4
## 6  8     0       1     1   1    2   1           2         1            6
##   TravelTime PSW1 PSW2 PSW3 PSW4 PSW5 PSW6 PSW7 PSS1 PSS2 PSS3 PSS4 PSS5 PSS6
## 1       3.00    4    5    6    4    6    4    5    6    6    6    6    4    6
## 2       2.00    2    5    3    2    4    5    3    2    3    4    2    2    2
## 3       0.17    4    6    6    4    1    4    2    2    6    6    4    2    2
## 4       4.00    5    5    5    2    6    4    2    6    6    4    1    4    4
## 5       2.00    3    2    4    4    2    6    5    3    3    5    2    1    2
## 6       2.00    2    5    6    5    4    4    6    3    4    6    2    1    1
##   PSS7 PSB1 PSB2 PSB3 PSB4 PSB5 PSB6 PSB7 PSB8 PSQ1 PSQ2 PSQ3 PSQ4 PSQ5 PSQ6
## 1    4    6    6    4    6    5    6    6    6    6    6    4    4    6    4
## 2    2    3    3    4    4    6    6    6    6    4    6    5    4    4    5
## 3    2    2    6    6    4    6    6    4    2    2    2    2    2    2    4
## 4    6    5    6    6    5    5    6    7    6    6    5    4    5    5    5
## 5    2    2    2    5    5    5    6    4    3    6    3    6    3    6    6
## 6    2    2    5    4    4    4    6    4    2    4    4    5    4    5    5
##   PSQ7 PSQ8 PSQ9 PSQ10 PSQ11 PSQ12 PSQ13 PSQ14 PSQ15 PSQ16 PSQ17 PSQ18 SAT1
## 1    4    4    6     6     6     5     6     4     4     6     4     2    6
## 2    6    4    6     6     4     4     5     4     3     4     3     6    5
## 3    1    2    1     1     1     6     6     2     6     2     5     2    2
## 4    5    4    4     5     5     5     5     4     5     5     5     5    5
## 5    4    3    6     4     4     4     5     5     3     5     3     5    4
## 6    4    3    3     4     4     4     5     3     3     3     3     4    4
##   SAT2 SAT3 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 IMA2 IMA3 IMA4 IMA5 PHB1
## 1    6    6    4    6    5    7    6    6    6    6    6    6    4    6    6
## 2    5    4    4    5    4    4    4    4    5    4    5    6    5    4    3
## 3    2    2    2    6    6    4    4    2    2    4    2    5    2    2    5
## 4    5    5    5    5    5    5    5    5    5    5    5    5    5    5    5
## 5    4    4    4    5    5    4    4    4    6    5    4    5    4    5    5
## 6    5    5    4    3    2    5    5    6    6    4    3    2    2    4    4
##   PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4 ATM1 ATM2 ATM3 ATM4 ATM5 ATM6 ATM7
## 1    7    6    7    7    4    4    4    4    3    5    6    4    2    4    6
## 2    4    3    4    6    5    6    6    4    3    5    4    4    4    4    5
## 3    2    6    6    6    6    5    6    6    2    6    4    2    2    4    2
## 4    5    5    5    5    5    5    5    5    5    5    5    5    5    5    5
## 5    6    6    6    5    6    6    6    6    3    4    2    2    5    4    3
## 6    6    7    6    4    7    7    5    2    1    4    5    2    1    4    2
##   PPI1 PPI2 PPI3 SIM1 SIM2 SIM3 SIM4 PPA1 PPA2 PPA3 PPA4 SBE1 SBE2 SBE3 SBE4
## 1    6    6    6    4    4    4    2    4    6    4    4    4    4    4    4
## 2    4    4    4    4    4    4    4    4    4    4    4    5    4    5    5
## 3    4    2    2    4    4    4    2    4    6    6    2    6    6    6    6
## 4    5    5    5    5    5    5    5    5    5    5    5    5    5    5    5
## 5    2    3    4    6    6    5    5    5    5    5    5    5    5    5    5
## 6    5    2    2    2    2    2    2    2    3    3    2    2    2    2    2
##   EXB1 EXB2 EXB3 PVA1 PVA2 PVA3 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1    4    4    3    6    6    6       0       1      0      1      0
## 2    4    5    3    5    5    3       0       0      1      0      0
## 3    4    6    2    6    2    4       0       0      0      0      1
## 4    5    5    5    5    5    5       0       1      0      1      1
## 5    5    5    4    6    2    3       0       1      0      1      1
## 6    5    5    4    6    5    5       0       1      0      1      0
##   MarriedStatus Occupation Education Income
## 1             1          1         2      1
## 2             1          1         2      1
## 3             1          1         2      1
## 4             1          1         2      1
## 5             1          1         2      1
## 6             1          1         3      1
names(SEM1)
##   [1] "ID"            "MCCar"         "MCMotor"       "MCBus"        
##   [5] "AGE"           "CITY"          "FRE"           "TripPurpose"  
##   [9] "Departure"     "TimeUseonBus"  "TravelTime"    "PSW1"         
##  [13] "PSW2"          "PSW3"          "PSW4"          "PSW5"         
##  [17] "PSW6"          "PSW7"          "PSS1"          "PSS2"         
##  [21] "PSS3"          "PSS4"          "PSS5"          "PSS6"         
##  [25] "PSS7"          "PSB1"          "PSB2"          "PSB3"         
##  [29] "PSB4"          "PSB5"          "PSB6"          "PSB7"         
##  [33] "PSB8"          "PSQ1"          "PSQ2"          "PSQ3"         
##  [37] "PSQ4"          "PSQ5"          "PSQ6"          "PSQ7"         
##  [41] "PSQ8"          "PSQ9"          "PSQ10"         "PSQ11"        
##  [45] "PSQ12"         "PSQ13"         "PSQ14"         "PSQ15"        
##  [49] "PSQ16"         "PSQ17"         "PSQ18"         "SAT1"         
##  [53] "SAT2"          "SAT3"          "LOY1"          "LOY2"         
##  [57] "LOY3"          "LOY4"          "LOY5"          "LOY6"         
##  [61] "LOY7"          "IMA1"          "IMA2"          "IMA3"         
##  [65] "IMA4"          "IMA5"          "PHB1"          "PHB2"         
##  [69] "PHB3"          "PHB4"          "PHB5"          "PEV1"         
##  [73] "PEV2"          "PEV3"          "PEV4"          "ATM1"         
##  [77] "ATM2"          "ATM3"          "ATM4"          "ATM5"         
##  [81] "ATM6"          "ATM7"          "PPI1"          "PPI2"         
##  [85] "PPI3"          "SIM1"          "SIM2"          "SIM3"         
##  [89] "SIM4"          "PPA1"          "PPA2"          "PPA3"         
##  [93] "PPA4"          "SBE1"          "SBE2"          "SBE3"         
##  [97] "SBE4"          "EXB1"          "EXB2"          "EXB3"         
## [101] "PVA1"          "PVA2"          "PVA3"          "EC_Stop"      
## [105] "WC_Stop"       "EC_Bus"        "WC_Bus"        "Gender"       
## [109] "MarriedStatus" "Occupation"    "Education"     "Income"
dim(SEM1)
## [1] 863 112

2. Desscriptive statistic

# 2.1. Subset Data SEM1 (6 constructs and 28 items)
SEM1 <- SEM1[, c(2, 3, 5, 6, 7, 8, 9, 10, 11, 26, 27, 28, 29, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112)]
head(SEM1)
##   MCCar MCMotor AGE CITY FRE TripPurpose Departure TimeUseonBus TravelTime PSB1
## 1     0       0   1    2   1           2         0            4       3.00    6
## 2     0       0   1    2   2           4         0            4       2.00    3
## 3     0       0   1    2   1           2         1            4       0.17    2
## 4     0       0   1    2   1           2         1            1       4.00    5
## 5     0       0   1    2   1           2         1            4       2.00    2
## 6     0       1   1    2   1           2         1            6       2.00    2
##   PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 IMA2 IMA3 IMA4 IMA5
## 1    6    4    6    4    6    5    7    6    6    6    6    6    6    4    6
## 2    3    4    4    4    5    4    4    4    4    5    4    5    6    5    4
## 3    6    6    4    2    6    6    4    4    2    2    4    2    5    2    2
## 4    6    6    5    5    5    5    5    5    5    5    5    5    5    5    5
## 5    2    5    5    4    5    5    4    4    4    6    5    4    5    4    5
## 6    5    4    4    4    3    2    5    5    6    6    4    3    2    2    4
##   PHB1 PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4 PVA1 PVA2 PVA3 EC_Stop WC_Stop
## 1    6    7    6    7    7    4    4    4    4    6    6    6       0       1
## 2    3    4    3    4    6    5    6    6    4    5    5    3       0       0
## 3    5    2    6    6    6    6    5    6    6    6    2    4       0       0
## 4    5    5    5    5    5    5    5    5    5    5    5    5       0       1
## 5    5    6    6    6    5    6    6    6    6    6    2    3       0       1
## 6    4    6    7    6    4    7    7    5    2    6    5    5       0       1
##   EC_Bus WC_Bus Gender MarriedStatus Occupation Education Income
## 1      0      1      0             1          1         2      1
## 2      1      0      0             1          1         2      1
## 3      0      0      1             1          1         2      1
## 4      0      1      1             1          1         2      1
## 5      0      1      1             1          1         2      1
## 6      0      1      0             1          1         3      1
names(SEM1)
##  [1] "MCCar"         "MCMotor"       "AGE"           "CITY"         
##  [5] "FRE"           "TripPurpose"   "Departure"     "TimeUseonBus" 
##  [9] "TravelTime"    "PSB1"          "PSB2"          "PSB3"         
## [13] "PSB4"          "LOY1"          "LOY2"          "LOY3"         
## [17] "LOY4"          "LOY5"          "LOY6"          "LOY7"         
## [21] "IMA1"          "IMA2"          "IMA3"          "IMA4"         
## [25] "IMA5"          "PHB1"          "PHB2"          "PHB3"         
## [29] "PHB4"          "PHB5"          "PEV1"          "PEV2"         
## [33] "PEV3"          "PEV4"          "PVA1"          "PVA2"         
## [37] "PVA3"          "EC_Stop"       "WC_Stop"       "EC_Bus"       
## [41] "WC_Bus"        "Gender"        "MarriedStatus" "Occupation"   
## [45] "Education"     "Income"
dim(SEM1)
## [1] 863  46
# Data coding
str(SEM1)
## 'data.frame':    863 obs. of  46 variables:
##  $ MCCar        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ MCMotor      : int  0 0 0 0 0 1 1 1 1 1 ...
##  $ AGE          : int  1 1 1 1 1 1 1 4 1 1 ...
##  $ CITY         : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ FRE          : int  1 2 1 1 1 1 4 1 1 1 ...
##  $ TripPurpose  : int  2 4 2 2 2 2 4 1 2 2 ...
##  $ Departure    : int  0 0 1 1 1 1 1 1 1 1 ...
##  $ TimeUseonBus : int  4 4 4 1 4 6 1 4 4 3 ...
##  $ TravelTime   : num  3 2 0.17 4 2 2 1 2 2.5 1.5 ...
##  $ PSB1         : int  6 3 2 5 2 2 1 6 2 4 ...
##  $ PSB2         : int  6 3 6 6 2 5 1 6 4 4 ...
##  $ PSB3         : int  4 4 6 6 5 4 5 6 4 7 ...
##  $ PSB4         : int  6 4 4 5 5 4 1 6 4 4 ...
##  $ LOY1         : int  4 4 2 5 4 4 2 6 6 6 ...
##  $ LOY2         : int  6 5 6 5 5 3 1 6 5 6 ...
##  $ LOY3         : int  5 4 6 5 5 2 1 6 6 6 ...
##  $ LOY4         : int  7 4 4 5 4 5 2 7 5 6 ...
##  $ LOY5         : int  6 4 4 5 4 5 2 7 5 4 ...
##  $ LOY6         : int  6 4 2 5 4 6 1 7 5 6 ...
##  $ LOY7         : int  6 5 2 5 6 6 2 7 5 7 ...
##  $ IMA1         : int  6 4 4 5 5 4 2 6 5 6 ...
##  $ IMA2         : int  6 5 2 5 4 3 3 6 6 7 ...
##  $ IMA3         : int  6 6 5 5 5 2 2 5 6 7 ...
##  $ IMA4         : int  4 5 2 5 4 2 2 6 6 7 ...
##  $ IMA5         : int  6 4 2 5 5 4 2 6 6 7 ...
##  $ PHB1         : int  6 3 5 5 5 4 3 6 6 7 ...
##  $ PHB2         : int  7 4 2 5 6 6 3 7 5 7 ...
##  $ PHB3         : int  6 3 6 5 6 7 1 4 3 7 ...
##  $ PHB4         : int  7 4 6 5 6 6 4 6 7 7 ...
##  $ PHB5         : int  7 6 6 5 5 4 4 6 7 7 ...
##  $ PEV1         : int  4 5 6 5 6 7 4 6 7 7 ...
##  $ PEV2         : int  4 6 5 5 6 7 2 7 7 7 ...
##  $ PEV3         : int  4 6 6 5 6 5 2 6 6 7 ...
##  $ PEV4         : int  4 4 6 5 6 2 2 6 6 7 ...
##  $ PVA1         : int  6 5 6 5 6 6 6 6 6 6 ...
##  $ PVA2         : int  6 5 2 5 2 5 4 5 4 6 ...
##  $ PVA3         : int  6 3 4 5 3 5 5 6 5 6 ...
##  $ EC_Stop      : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ WC_Stop      : int  1 0 0 1 1 1 0 0 1 0 ...
##  $ EC_Bus       : int  0 1 0 0 0 0 0 0 0 0 ...
##  $ WC_Bus       : int  1 0 0 1 1 1 0 0 1 0 ...
##  $ Gender       : int  0 0 1 1 1 0 0 0 0 0 ...
##  $ MarriedStatus: int  1 1 1 1 1 1 1 0 1 1 ...
##  $ Occupation   : int  1 1 1 1 1 1 1 2 1 1 ...
##  $ Education    : int  2 2 2 2 2 3 5 1 2 3 ...
##  $ Income       : int  1 1 1 1 1 1 1 1 1 1 ...
attach(SEM1)
SEM1 = within(SEM1, {
  MCCar = factor(MCCar,labels = c("No", "Yes"))
  MCMotor = factor(MCMotor,labels = c("No", "Yes"))
  AGE = factor(AGE, labels = c("16-25", "26-35", "36-45", "46-55", ">55"))
  CITY = factor(CITY,labels = c("DaNang", "HoChiMinh"))
  FRE = factor(FRE, labels = c(">=3 days/week", "2days/month-2days/week", "2days/year-1day/month", "<2 days/year"))
  TripPurpose = factor(TripPurpose, labels = c("Working", "Studying", "Shopping", "Entertaining", "Others"))
  Departure = factor(Departure, labels = c("Normal", "Peak-Hour"))
  TimeUseonBus = factor(TimeUseonBus, labels = c("Using.telephone", "Reading", "Listening", "Nothing", "Talking", "Others"))
  EC_Stop = factor(EC_Stop, labels = c("Never", "Ever"))
  WC_Stop = factor(WC_Stop, labels = c("Never", "Ever"))
  EC_Bus = factor(EC_Bus, labels = c("Never", "Ever"))
  WC_Bus = factor(WC_Bus, labels = c("Never", "Ever"))
  Gender = factor(Gender, labels = c("Female", "Male"))
  MarriedStatus = factor(MarriedStatus, labels = c("Married", "Single"))
  Occupation = factor(Occupation, labels = c("Students/Pupils", "Full.time.job", "Part.time.job", "Retirement", "No.job", "Housewife", "Others"))
  Education = factor(Education, labels = c("Secondary.school", "Undergraduate", "High.school", "Postgraduate", "Others"))
  Income = factor(Income, labels = c("<5millions", "5-10millions", "10-15millions", ">15millions"))
    } )
str(SEM1)
## 'data.frame':    863 obs. of  46 variables:
##  $ MCCar        : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
##  $ MCMotor      : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 2 2 2 2 2 ...
##  $ AGE          : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY         : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ FRE          : Factor w/ 4 levels ">=3 days/week",..: 1 2 1 1 1 1 4 1 1 1 ...
##  $ TripPurpose  : Factor w/ 5 levels "Working","Studying",..: 2 4 2 2 2 2 4 1 2 2 ...
##  $ Departure    : Factor w/ 2 levels "Normal","Peak-Hour": 1 1 2 2 2 2 2 2 2 2 ...
##  $ TimeUseonBus : Factor w/ 6 levels "Using.telephone",..: 4 4 4 1 4 6 1 4 4 3 ...
##  $ TravelTime   : num  3 2 0.17 4 2 2 1 2 2.5 1.5 ...
##  $ PSB1         : int  6 3 2 5 2 2 1 6 2 4 ...
##  $ PSB2         : int  6 3 6 6 2 5 1 6 4 4 ...
##  $ PSB3         : int  4 4 6 6 5 4 5 6 4 7 ...
##  $ PSB4         : int  6 4 4 5 5 4 1 6 4 4 ...
##  $ LOY1         : int  4 4 2 5 4 4 2 6 6 6 ...
##  $ LOY2         : int  6 5 6 5 5 3 1 6 5 6 ...
##  $ LOY3         : int  5 4 6 5 5 2 1 6 6 6 ...
##  $ LOY4         : int  7 4 4 5 4 5 2 7 5 6 ...
##  $ LOY5         : int  6 4 4 5 4 5 2 7 5 4 ...
##  $ LOY6         : int  6 4 2 5 4 6 1 7 5 6 ...
##  $ LOY7         : int  6 5 2 5 6 6 2 7 5 7 ...
##  $ IMA1         : int  6 4 4 5 5 4 2 6 5 6 ...
##  $ IMA2         : int  6 5 2 5 4 3 3 6 6 7 ...
##  $ IMA3         : int  6 6 5 5 5 2 2 5 6 7 ...
##  $ IMA4         : int  4 5 2 5 4 2 2 6 6 7 ...
##  $ IMA5         : int  6 4 2 5 5 4 2 6 6 7 ...
##  $ PHB1         : int  6 3 5 5 5 4 3 6 6 7 ...
##  $ PHB2         : int  7 4 2 5 6 6 3 7 5 7 ...
##  $ PHB3         : int  6 3 6 5 6 7 1 4 3 7 ...
##  $ PHB4         : int  7 4 6 5 6 6 4 6 7 7 ...
##  $ PHB5         : int  7 6 6 5 5 4 4 6 7 7 ...
##  $ PEV1         : int  4 5 6 5 6 7 4 6 7 7 ...
##  $ PEV2         : int  4 6 5 5 6 7 2 7 7 7 ...
##  $ PEV3         : int  4 6 6 5 6 5 2 6 6 7 ...
##  $ PEV4         : int  4 4 6 5 6 2 2 6 6 7 ...
##  $ PVA1         : int  6 5 6 5 6 6 6 6 6 6 ...
##  $ PVA2         : int  6 5 2 5 2 5 4 5 4 6 ...
##  $ PVA3         : int  6 3 4 5 3 5 5 6 5 6 ...
##  $ EC_Stop      : Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop      : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus       : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus       : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender       : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ MarriedStatus: Factor w/ 2 levels "Married","Single": 2 2 2 2 2 2 2 1 2 2 ...
##  $ Occupation   : Factor w/ 7 levels "Students/Pupils",..: 1 1 1 1 1 1 1 2 1 1 ...
##  $ Education    : Factor w/ 5 levels "Secondary.school",..: 2 2 2 2 2 3 5 1 2 3 ...
##  $ Income       : Factor w/ 4 levels "<5millions","5-10millions",..: 1 1 1 1 1 1 1 1 1 1 ...
dim(SEM1)
## [1] 863  46
# 2.2. Descritive Table
library(tableone)
require(tableone)
library(magrittr)
summary(SEM1)
##  MCCar     MCMotor      AGE             CITY                         FRE     
##  No :843   No :701   16-25:421   DaNang   :415   >=3 days/week         :500  
##  Yes: 20   Yes:162   26-35:169   HoChiMinh:448   2days/month-2days/week:166  
##                      36-45:102                   2days/year-1day/month : 98  
##                      46-55: 77                   <2 days/year          : 99  
##                      >55  : 94                                               
##                                                                              
##                                                                              
##        TripPurpose      Departure            TimeUseonBus   TravelTime    
##  Working     :305   Normal   :219   Using.telephone:201   Min.   : 0.000  
##  Studying    :297   Peak-Hour:644   Reading        : 53   1st Qu.: 0.500  
##  Shopping    : 59                   Listening      :137   Median : 1.000  
##  Entertaining: 99                   Nothing        :419   Mean   : 1.272  
##  Others      :103                   Talking        : 33   3rd Qu.: 2.000  
##                                     Others         : 20   Max.   :20.000  
##                                                                           
##       PSB1           PSB2            PSB3            PSB4           LOY1      
##  Min.   :1.00   Min.   :1.000   Min.   :1.000   Min.   :1.00   Min.   :1.000  
##  1st Qu.:3.00   1st Qu.:4.000   1st Qu.:5.000   1st Qu.:4.00   1st Qu.:5.000  
##  Median :5.00   Median :6.000   Median :6.000   Median :5.00   Median :6.000  
##  Mean   :4.64   Mean   :5.067   Mean   :5.484   Mean   :4.81   Mean   :5.488  
##  3rd Qu.:6.00   3rd Qu.:6.000   3rd Qu.:6.000   3rd Qu.:6.00   3rd Qu.:6.000  
##  Max.   :7.00   Max.   :7.000   Max.   :7.000   Max.   :7.00   Max.   :7.000  
##                                                                               
##       LOY2            LOY3            LOY4           LOY5            LOY6      
##  Min.   :1.000   Min.   :1.000   Min.   :1.00   Min.   :1.000   Min.   :1.000  
##  1st Qu.:5.000   1st Qu.:5.000   1st Qu.:5.00   1st Qu.:5.000   1st Qu.:4.000  
##  Median :6.000   Median :6.000   Median :6.00   Median :6.000   Median :5.000  
##  Mean   :5.513   Mean   :5.596   Mean   :5.32   Mean   :5.373   Mean   :5.075  
##  3rd Qu.:6.000   3rd Qu.:6.000   3rd Qu.:6.00   3rd Qu.:6.000   3rd Qu.:6.000  
##  Max.   :7.000   Max.   :7.000   Max.   :7.00   Max.   :7.000   Max.   :7.000  
##                                                                                
##       LOY7            IMA1            IMA2            IMA3      
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:5.000   1st Qu.:5.000   1st Qu.:4.500   1st Qu.:5.000  
##  Median :6.000   Median :5.000   Median :6.000   Median :6.000  
##  Mean   :5.626   Mean   :5.209   Mean   :5.217   Mean   :5.364  
##  3rd Qu.:6.000   3rd Qu.:6.000   3rd Qu.:6.000   3rd Qu.:6.000  
##  Max.   :7.000   Max.   :7.000   Max.   :7.000   Max.   :7.000  
##                                                                 
##       IMA4            IMA5            PHB1            PHB2            PHB3     
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.00  
##  1st Qu.:5.000   1st Qu.:5.000   1st Qu.:5.000   1st Qu.:5.000   1st Qu.:5.00  
##  Median :6.000   Median :6.000   Median :6.000   Median :6.000   Median :6.00  
##  Mean   :5.366   Mean   :5.409   Mean   :5.292   Mean   :5.601   Mean   :5.44  
##  3rd Qu.:6.000   3rd Qu.:6.000   3rd Qu.:6.000   3rd Qu.:6.000   3rd Qu.:6.00  
##  Max.   :7.000   Max.   :7.000   Max.   :7.000   Max.   :7.000   Max.   :7.00  
##                                                                                
##       PHB4            PHB5            PEV1            PEV2      
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:5.000   1st Qu.:5.500   1st Qu.:5.000   1st Qu.:5.000  
##  Median :6.000   Median :6.000   Median :6.000   Median :6.000  
##  Mean   :5.868   Mean   :5.798   Mean   :5.757   Mean   :5.578  
##  3rd Qu.:7.000   3rd Qu.:7.000   3rd Qu.:7.000   3rd Qu.:6.000  
##  Max.   :7.000   Max.   :7.000   Max.   :7.000   Max.   :7.000  
##                                                                 
##       PEV3            PEV4            PVA1            PVA2      
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:5.000   1st Qu.:5.000   1st Qu.:5.000   1st Qu.:5.000  
##  Median :6.000   Median :6.000   Median :6.000   Median :6.000  
##  Mean   :5.524   Mean   :5.479   Mean   :5.855   Mean   :5.436  
##  3rd Qu.:6.000   3rd Qu.:6.000   3rd Qu.:7.000   3rd Qu.:6.000  
##  Max.   :7.000   Max.   :7.000   Max.   :7.000   Max.   :7.000  
##                                                                 
##       PVA3        EC_Stop     WC_Stop      EC_Bus      WC_Bus       Gender   
##  Min.   :1.000   Never:827   Never:742   Never:823   Never:748   Female:503  
##  1st Qu.:5.000   Ever : 36   Ever :121   Ever : 40   Ever :115   Male  :360  
##  Median :6.000                                                               
##  Mean   :5.716                                                               
##  3rd Qu.:6.000                                                               
##  Max.   :7.000                                                               
##                                                                              
##  MarriedStatus           Occupation             Education             Income   
##  Married:334   Students/Pupils:362   Secondary.school: 59   <5millions   :460  
##  Single :529   Full.time.job  :304   Undergraduate   :279   5-10millions :248  
##                Part.time.job  : 68   High.school     :363   10-15millions:118  
##                Retirement     : 46   Postgraduate    :105   >15millions  : 37  
##                No.job         :  4   Others          : 57                      
##                Housewife      : 52                                             
##                Others         : 27
library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
Tab1_SEM1 <- table1(~ PSB1 + PSB2 + PSB3 + PSB4 + LOY1 + LOY2 + LOY3 + LOY4 + LOY5 + LOY6 + LOY7 + IMA1 + IMA2 + IMA3 + IMA4 + IMA5 + PHB1 + PHB2 + PHB3 + PHB4 + PHB5 + PEV1 + PEV2 + PEV3 + PEV4 + PVA1 + PVA2 + PVA3 + EC_Stop + WC_Stop + EC_Bus + WC_Bus + Gender + MarriedStatus + Occupation + Education + Income + MCCar + MCMotor + AGE + CITY + FRE + TripPurpose + Departure + TimeUseonBus + TravelTime| CITY , data = SEM1)
Tab1_SEM1

DaNang
(N=415)
HoChiMinh
(N=448)
Overall
(N=863)
PSB1
Mean (SD) 5.56 (1.27) 3.79 (1.90) 4.64 (1.85)
Median [Min, Max] 6.00 [1.00, 7.00] 4.00 [1.00, 7.00] 5.00 [1.00, 7.00]
PSB2
Mean (SD) 5.59 (1.23) 4.58 (1.77) 5.07 (1.61)
Median [Min, Max] 6.00 [1.00, 7.00] 5.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PSB3
Mean (SD) 5.74 (1.05) 5.25 (1.39) 5.48 (1.26)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PSB4
Mean (SD) 5.38 (1.24) 4.28 (1.70) 4.81 (1.59)
Median [Min, Max] 6.00 [1.00, 7.00] 4.00 [1.00, 7.00] 5.00 [1.00, 7.00]
LOY1
Mean (SD) 5.74 (0.896) 5.26 (1.24) 5.49 (1.11)
Median [Min, Max] 6.00 [2.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
LOY2
Mean (SD) 5.78 (0.872) 5.26 (1.31) 5.51 (1.15)
Median [Min, Max] 6.00 [2.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
LOY3
Mean (SD) 5.82 (0.887) 5.39 (1.21) 5.60 (1.09)
Median [Min, Max] 6.00 [2.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
LOY4
Mean (SD) 5.53 (1.06) 5.13 (1.38) 5.32 (1.25)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
LOY5
Mean (SD) 5.62 (1.05) 5.14 (1.39) 5.37 (1.26)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
LOY6
Mean (SD) 5.33 (1.34) 4.83 (1.59) 5.08 (1.50)
Median [Min, Max] 6.00 [1.00, 7.00] 5.00 [1.00, 7.00] 5.00 [1.00, 7.00]
LOY7
Mean (SD) 5.90 (0.915) 5.37 (1.38) 5.63 (1.21)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
IMA1
Mean (SD) 5.36 (0.977) 5.07 (1.27) 5.21 (1.15)
Median [Min, Max] 5.00 [1.00, 7.00] 5.00 [1.00, 7.00] 5.00 [1.00, 7.00]
IMA2
Mean (SD) 5.51 (0.892) 4.95 (1.34) 5.22 (1.18)
Median [Min, Max] 6.00 [2.00, 7.00] 5.00 [1.00, 7.00] 6.00 [1.00, 7.00]
IMA3
Mean (SD) 5.61 (0.858) 5.14 (1.28) 5.36 (1.12)
Median [Min, Max] 6.00 [2.00, 7.00] 5.00 [1.00, 7.00] 6.00 [1.00, 7.00]
IMA4
Mean (SD) 5.68 (0.874) 5.07 (1.34) 5.37 (1.18)
Median [Min, Max] 6.00 [1.00, 7.00] 5.00 [1.00, 7.00] 6.00 [1.00, 7.00]
IMA5
Mean (SD) 5.72 (0.832) 5.13 (1.27) 5.41 (1.12)
Median [Min, Max] 6.00 [2.00, 7.00] 5.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PHB1
Mean (SD) 5.40 (1.09) 5.19 (1.53) 5.29 (1.34)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PHB2
Mean (SD) 5.82 (1.01) 5.40 (1.45) 5.60 (1.27)
Median [Min, Max] 6.00 [2.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PHB3
Mean (SD) 5.46 (1.19) 5.42 (1.38) 5.44 (1.29)
Median [Min, Max] 6.00 [2.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PHB4
Mean (SD) 6.06 (0.853) 5.69 (1.33) 5.87 (1.14)
Median [Min, Max] 6.00 [3.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PHB5
Mean (SD) 6.03 (0.861) 5.59 (1.46) 5.80 (1.23)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PEV1
Mean (SD) 5.95 (0.890) 5.58 (1.55) 5.76 (1.29)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PEV2
Mean (SD) 5.77 (1.04) 5.40 (1.60) 5.58 (1.37)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PEV3
Mean (SD) 5.70 (1.10) 5.36 (1.59) 5.52 (1.39)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PEV4
Mean (SD) 5.68 (1.07) 5.29 (1.57) 5.48 (1.37)
Median [Min, Max] 6.00 [2.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PVA1
Mean (SD) 5.95 (0.909) 5.76 (1.19) 5.86 (1.07)
Median [Min, Max] 6.00 [2.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PVA2
Mean (SD) 5.53 (1.19) 5.35 (1.29) 5.44 (1.25)
Median [Min, Max] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
PVA3
Mean (SD) 5.90 (0.815) 5.54 (1.25) 5.72 (1.08)
Median [Min, Max] 6.00 [2.00, 7.00] 6.00 [1.00, 7.00] 6.00 [1.00, 7.00]
EC_Stop
Never 411 (99.0%) 416 (92.9%) 827 (95.8%)
Ever 4 (1.0%) 32 (7.1%) 36 (4.2%)
WC_Stop
Never 393 (94.7%) 349 (77.9%) 742 (86.0%)
Ever 22 (5.3%) 99 (22.1%) 121 (14.0%)
EC_Bus
Never 412 (99.3%) 411 (91.7%) 823 (95.4%)
Ever 3 (0.7%) 37 (8.3%) 40 (4.6%)
WC_Bus
Never 398 (95.9%) 350 (78.1%) 748 (86.7%)
Ever 17 (4.1%) 98 (21.9%) 115 (13.3%)
Gender
Female 231 (55.7%) 272 (60.7%) 503 (58.3%)
Male 184 (44.3%) 176 (39.3%) 360 (41.7%)
MarriedStatus
Married 150 (36.1%) 184 (41.1%) 334 (38.7%)
Single 265 (63.9%) 264 (58.9%) 529 (61.3%)
Occupation
Students/Pupils 202 (48.7%) 160 (35.7%) 362 (41.9%)
Full.time.job 115 (27.7%) 189 (42.2%) 304 (35.2%)
Part.time.job 28 (6.7%) 40 (8.9%) 68 (7.9%)
Retirement 33 (8.0%) 13 (2.9%) 46 (5.3%)
No.job 3 (0.7%) 1 (0.2%) 4 (0.5%)
Housewife 27 (6.5%) 25 (5.6%) 52 (6.0%)
Others 7 (1.7%) 20 (4.5%) 27 (3.1%)
Education
Secondary.school 30 (7.2%) 29 (6.5%) 59 (6.8%)
Undergraduate 131 (31.6%) 148 (33.0%) 279 (32.3%)
High.school 171 (41.2%) 192 (42.9%) 363 (42.1%)
Postgraduate 63 (15.2%) 42 (9.4%) 105 (12.2%)
Others 20 (4.8%) 37 (8.3%) 57 (6.6%)
Income
<5millions 272 (65.5%) 188 (42.0%) 460 (53.3%)
5-10millions 83 (20.0%) 165 (36.8%) 248 (28.7%)
10-15millions 51 (12.3%) 67 (15.0%) 118 (13.7%)
>15millions 9 (2.2%) 28 (6.2%) 37 (4.3%)
MCCar
No 415 (100%) 428 (95.5%) 843 (97.7%)
Yes 0 (0%) 20 (4.5%) 20 (2.3%)
MCMotor
No 414 (99.8%) 287 (64.1%) 701 (81.2%)
Yes 1 (0.2%) 161 (35.9%) 162 (18.8%)
AGE
16-25 221 (53.3%) 200 (44.6%) 421 (48.8%)
26-35 76 (18.3%) 93 (20.8%) 169 (19.6%)
36-45 36 (8.7%) 66 (14.7%) 102 (11.8%)
46-55 35 (8.4%) 42 (9.4%) 77 (8.9%)
>55 47 (11.3%) 47 (10.5%) 94 (10.9%)
CITY
DaNang 415 (100%) 0 (0%) 415 (48.1%)
HoChiMinh 0 (0%) 448 (100%) 448 (51.9%)
FRE
>=3 days/week 263 (63.4%) 237 (52.9%) 500 (57.9%)
2days/month-2days/week 86 (20.7%) 80 (17.9%) 166 (19.2%)
2days/year-1day/month 28 (6.7%) 70 (15.6%) 98 (11.4%)
<2 days/year 38 (9.2%) 61 (13.6%) 99 (11.5%)
TripPurpose
Working 116 (28.0%) 189 (42.2%) 305 (35.3%)
Studying 163 (39.3%) 134 (29.9%) 297 (34.4%)
Shopping 46 (11.1%) 13 (2.9%) 59 (6.8%)
Entertaining 52 (12.5%) 47 (10.5%) 99 (11.5%)
Others 38 (9.2%) 65 (14.5%) 103 (11.9%)
Departure
Normal 147 (35.4%) 72 (16.1%) 219 (25.4%)
Peak-Hour 268 (64.6%) 376 (83.9%) 644 (74.6%)
TimeUseonBus
Using.telephone 107 (25.8%) 94 (21.0%) 201 (23.3%)
Reading 22 (5.3%) 31 (6.9%) 53 (6.1%)
Listening 54 (13.0%) 83 (18.5%) 137 (15.9%)
Nothing 203 (48.9%) 216 (48.2%) 419 (48.6%)
Talking 24 (5.8%) 9 (2.0%) 33 (3.8%)
Others 5 (1.2%) 15 (3.3%) 20 (2.3%)
TravelTime
Mean (SD) 1.12 (0.831) 1.41 (1.81) 1.27 (1.43)
Median [Min, Max] 1.00 [0, 6.00] 1.00 [0, 20.0] 1.00 [0, 20.0]
3. Perception of bus user 3.1. Tạo dữ liệu biểu đồ likert

# Create data for Likert graph by BS (remove ID and TM =7-Bus) 
head(SEM1)
##   MCCar MCMotor   AGE      CITY                    FRE  TripPurpose Departure
## 1    No      No 16-25 HoChiMinh          >=3 days/week     Studying    Normal
## 2    No      No 16-25 HoChiMinh 2days/month-2days/week Entertaining    Normal
## 3    No      No 16-25 HoChiMinh          >=3 days/week     Studying Peak-Hour
## 4    No      No 16-25 HoChiMinh          >=3 days/week     Studying Peak-Hour
## 5    No      No 16-25 HoChiMinh          >=3 days/week     Studying Peak-Hour
## 6    No     Yes 16-25 HoChiMinh          >=3 days/week     Studying Peak-Hour
##      TimeUseonBus TravelTime PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6
## 1         Nothing       3.00    6    6    4    6    4    6    5    7    6    6
## 2         Nothing       2.00    3    3    4    4    4    5    4    4    4    4
## 3         Nothing       0.17    2    6    6    4    2    6    6    4    4    2
## 4 Using.telephone       4.00    5    6    6    5    5    5    5    5    5    5
## 5         Nothing       2.00    2    2    5    5    4    5    5    4    4    4
## 6          Others       2.00    2    5    4    4    4    3    2    5    5    6
##   LOY7 IMA1 IMA2 IMA3 IMA4 IMA5 PHB1 PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4
## 1    6    6    6    6    4    6    6    7    6    7    7    4    4    4    4
## 2    5    4    5    6    5    4    3    4    3    4    6    5    6    6    4
## 3    2    4    2    5    2    2    5    2    6    6    6    6    5    6    6
## 4    5    5    5    5    5    5    5    5    5    5    5    5    5    5    5
## 5    6    5    4    5    4    5    5    6    6    6    5    6    6    6    6
## 6    6    4    3    2    2    4    4    6    7    6    4    7    7    5    2
##   PVA1 PVA2 PVA3 EC_Stop WC_Stop EC_Bus WC_Bus Gender MarriedStatus
## 1    6    6    6   Never    Ever  Never   Ever Female        Single
## 2    5    5    3   Never   Never   Ever  Never Female        Single
## 3    6    2    4   Never   Never  Never  Never   Male        Single
## 4    5    5    5   Never    Ever  Never   Ever   Male        Single
## 5    6    2    3   Never    Ever  Never   Ever   Male        Single
## 6    6    5    5   Never    Ever  Never   Ever Female        Single
##        Occupation     Education     Income
## 1 Students/Pupils Undergraduate <5millions
## 2 Students/Pupils Undergraduate <5millions
## 3 Students/Pupils Undergraduate <5millions
## 4 Students/Pupils Undergraduate <5millions
## 5 Students/Pupils Undergraduate <5millions
## 6 Students/Pupils   High.school <5millions
str(SEM1)
## 'data.frame':    863 obs. of  46 variables:
##  $ MCCar        : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
##  $ MCMotor      : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 2 2 2 2 2 ...
##  $ AGE          : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY         : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ FRE          : Factor w/ 4 levels ">=3 days/week",..: 1 2 1 1 1 1 4 1 1 1 ...
##  $ TripPurpose  : Factor w/ 5 levels "Working","Studying",..: 2 4 2 2 2 2 4 1 2 2 ...
##  $ Departure    : Factor w/ 2 levels "Normal","Peak-Hour": 1 1 2 2 2 2 2 2 2 2 ...
##  $ TimeUseonBus : Factor w/ 6 levels "Using.telephone",..: 4 4 4 1 4 6 1 4 4 3 ...
##  $ TravelTime   : num  3 2 0.17 4 2 2 1 2 2.5 1.5 ...
##  $ PSB1         : int  6 3 2 5 2 2 1 6 2 4 ...
##  $ PSB2         : int  6 3 6 6 2 5 1 6 4 4 ...
##  $ PSB3         : int  4 4 6 6 5 4 5 6 4 7 ...
##  $ PSB4         : int  6 4 4 5 5 4 1 6 4 4 ...
##  $ LOY1         : int  4 4 2 5 4 4 2 6 6 6 ...
##  $ LOY2         : int  6 5 6 5 5 3 1 6 5 6 ...
##  $ LOY3         : int  5 4 6 5 5 2 1 6 6 6 ...
##  $ LOY4         : int  7 4 4 5 4 5 2 7 5 6 ...
##  $ LOY5         : int  6 4 4 5 4 5 2 7 5 4 ...
##  $ LOY6         : int  6 4 2 5 4 6 1 7 5 6 ...
##  $ LOY7         : int  6 5 2 5 6 6 2 7 5 7 ...
##  $ IMA1         : int  6 4 4 5 5 4 2 6 5 6 ...
##  $ IMA2         : int  6 5 2 5 4 3 3 6 6 7 ...
##  $ IMA3         : int  6 6 5 5 5 2 2 5 6 7 ...
##  $ IMA4         : int  4 5 2 5 4 2 2 6 6 7 ...
##  $ IMA5         : int  6 4 2 5 5 4 2 6 6 7 ...
##  $ PHB1         : int  6 3 5 5 5 4 3 6 6 7 ...
##  $ PHB2         : int  7 4 2 5 6 6 3 7 5 7 ...
##  $ PHB3         : int  6 3 6 5 6 7 1 4 3 7 ...
##  $ PHB4         : int  7 4 6 5 6 6 4 6 7 7 ...
##  $ PHB5         : int  7 6 6 5 5 4 4 6 7 7 ...
##  $ PEV1         : int  4 5 6 5 6 7 4 6 7 7 ...
##  $ PEV2         : int  4 6 5 5 6 7 2 7 7 7 ...
##  $ PEV3         : int  4 6 6 5 6 5 2 6 6 7 ...
##  $ PEV4         : int  4 4 6 5 6 2 2 6 6 7 ...
##  $ PVA1         : int  6 5 6 5 6 6 6 6 6 6 ...
##  $ PVA2         : int  6 5 2 5 2 5 4 5 4 6 ...
##  $ PVA3         : int  6 3 4 5 3 5 5 6 5 6 ...
##  $ EC_Stop      : Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop      : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus       : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus       : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender       : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ MarriedStatus: Factor w/ 2 levels "Married","Single": 2 2 2 2 2 2 2 1 2 2 ...
##  $ Occupation   : Factor w/ 7 levels "Students/Pupils",..: 1 1 1 1 1 1 1 2 1 1 ...
##  $ Education    : Factor w/ 5 levels "Secondary.school",..: 2 2 2 2 2 3 5 1 2 3 ...
##  $ Income       : Factor w/ 4 levels "<5millions","5-10millions",..: 1 1 1 1 1 1 1 1 1 1 ...
dim(SEM1)
## [1] 863  46
names(SEM1)
##  [1] "MCCar"         "MCMotor"       "AGE"           "CITY"         
##  [5] "FRE"           "TripPurpose"   "Departure"     "TimeUseonBus" 
##  [9] "TravelTime"    "PSB1"          "PSB2"          "PSB3"         
## [13] "PSB4"          "LOY1"          "LOY2"          "LOY3"         
## [17] "LOY4"          "LOY5"          "LOY6"          "LOY7"         
## [21] "IMA1"          "IMA2"          "IMA3"          "IMA4"         
## [25] "IMA5"          "PHB1"          "PHB2"          "PHB3"         
## [29] "PHB4"          "PHB5"          "PEV1"          "PEV2"         
## [33] "PEV3"          "PEV4"          "PVA1"          "PVA2"         
## [37] "PVA3"          "EC_Stop"       "WC_Stop"       "EC_Bus"       
## [41] "WC_Bus"        "Gender"        "MarriedStatus" "Occupation"   
## [45] "Education"     "Income"
SEM1_likert = SEM1[,c(3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42)] # BPE gom cac bien tu 10-37
head(SEM1_likert)
##     AGE      CITY PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1
## 1 16-25 HoChiMinh    6    6    4    6    4    6    5    7    6    6    6    6
## 2 16-25 HoChiMinh    3    3    4    4    4    5    4    4    4    4    5    4
## 3 16-25 HoChiMinh    2    6    6    4    2    6    6    4    4    2    2    4
## 4 16-25 HoChiMinh    5    6    6    5    5    5    5    5    5    5    5    5
## 5 16-25 HoChiMinh    2    2    5    5    4    5    5    4    4    4    6    5
## 6 16-25 HoChiMinh    2    5    4    4    4    3    2    5    5    6    6    4
##   IMA2 IMA3 IMA4 IMA5 PHB1 PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4 PVA1 PVA2
## 1    6    6    4    6    6    7    6    7    7    4    4    4    4    6    6
## 2    5    6    5    4    3    4    3    4    6    5    6    6    4    5    5
## 3    2    5    2    2    5    2    6    6    6    6    5    6    6    6    2
## 4    5    5    5    5    5    5    5    5    5    5    5    5    5    5    5
## 5    4    5    4    5    5    6    6    6    5    6    6    6    6    6    2
## 6    3    2    2    4    4    6    7    6    4    7    7    5    2    6    5
##   PVA3 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1    6   Never    Ever  Never   Ever Female
## 2    3   Never   Never   Ever  Never Female
## 3    4   Never   Never  Never  Never   Male
## 4    5   Never    Ever  Never   Ever   Male
## 5    3   Never    Ever  Never   Ever   Male
## 6    5   Never    Ever  Never   Ever Female
str(SEM1_likert)
## 'data.frame':    863 obs. of  35 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ PSB1   : int  6 3 2 5 2 2 1 6 2 4 ...
##  $ PSB2   : int  6 3 6 6 2 5 1 6 4 4 ...
##  $ PSB3   : int  4 4 6 6 5 4 5 6 4 7 ...
##  $ PSB4   : int  6 4 4 5 5 4 1 6 4 4 ...
##  $ LOY1   : int  4 4 2 5 4 4 2 6 6 6 ...
##  $ LOY2   : int  6 5 6 5 5 3 1 6 5 6 ...
##  $ LOY3   : int  5 4 6 5 5 2 1 6 6 6 ...
##  $ LOY4   : int  7 4 4 5 4 5 2 7 5 6 ...
##  $ LOY5   : int  6 4 4 5 4 5 2 7 5 4 ...
##  $ LOY6   : int  6 4 2 5 4 6 1 7 5 6 ...
##  $ LOY7   : int  6 5 2 5 6 6 2 7 5 7 ...
##  $ IMA1   : int  6 4 4 5 5 4 2 6 5 6 ...
##  $ IMA2   : int  6 5 2 5 4 3 3 6 6 7 ...
##  $ IMA3   : int  6 6 5 5 5 2 2 5 6 7 ...
##  $ IMA4   : int  4 5 2 5 4 2 2 6 6 7 ...
##  $ IMA5   : int  6 4 2 5 5 4 2 6 6 7 ...
##  $ PHB1   : int  6 3 5 5 5 4 3 6 6 7 ...
##  $ PHB2   : int  7 4 2 5 6 6 3 7 5 7 ...
##  $ PHB3   : int  6 3 6 5 6 7 1 4 3 7 ...
##  $ PHB4   : int  7 4 6 5 6 6 4 6 7 7 ...
##  $ PHB5   : int  7 6 6 5 5 4 4 6 7 7 ...
##  $ PEV1   : int  4 5 6 5 6 7 4 6 7 7 ...
##  $ PEV2   : int  4 6 5 5 6 7 2 7 7 7 ...
##  $ PEV3   : int  4 6 6 5 6 5 2 6 6 7 ...
##  $ PEV4   : int  4 4 6 5 6 2 2 6 6 7 ...
##  $ PVA1   : int  6 5 6 5 6 6 6 6 6 6 ...
##  $ PVA2   : int  6 5 2 5 2 5 4 5 4 6 ...
##  $ PVA3   : int  6 3 4 5 3 5 5 6 5 6 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
dim(SEM1_likert)
## [1] 863  35
## Creat dat_PSB1 - cot 3 cua SEM1_likert
dat_PSB1 <- SEM1_likert [, c(1,2,3,31, 32, 33, 34, 35)]
head(dat_PSB1)
##     AGE      CITY PSB1 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    3   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    2   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    2   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    2   Never    Ever  Never   Ever Female
dim(dat_PSB1)
## [1] 863   8
size <- 863
dat_PSB1$BPE <- rep("PSB1", size = size) # Tạo biến mới BPE
names(dat_PSB1)[names(dat_PSB1) == "PSB1"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PSB1)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PSB1
## 2 16-25 HoChiMinh   3   Never   Never   Ever  Never Female PSB1
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male PSB1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PSB1
## 5 16-25 HoChiMinh   2   Never    Ever  Never   Ever   Male PSB1
## 6 16-25 HoChiMinh   2   Never    Ever  Never   Ever Female PSB1
## Creat dat_PSB2 - cot 4 cua SEM1_likert
dat_PSB2 <- SEM1_likert [, c(1,2,4,31, 32, 33, 34, 35)]
head(dat_PSB2)
##     AGE      CITY PSB2 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    3   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    2   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    5   Never    Ever  Never   Ever Female
dim(dat_PSB2)
## [1] 863   8
size <- 863
dat_PSB2$BPE <- rep("PSB2", size = size) # Tạo biến mới BPE
names(dat_PSB2)[names(dat_PSB2) == "PSB2"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PSB2)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PSB2
## 2 16-25 HoChiMinh   3   Never   Never   Ever  Never Female PSB2
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PSB2
## 4 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PSB2
## 5 16-25 HoChiMinh   2   Never    Ever  Never   Ever   Male PSB2
## 6 16-25 HoChiMinh   5   Never    Ever  Never   Ever Female PSB2
## Creat dat_PSB3 - cot 5 cua SEN1_likert
dat_PSB3 <- SEM1_likert [, c(1,2,5,31, 32, 33, 34, 35)]
head(dat_PSB3)
##     AGE      CITY PSB3 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
dim(dat_PSB3)
## [1] 863   8
size <- 863
dat_PSB3$BPE <- rep("PSB3", size = size) # Tạo biến mới BPE
names(dat_PSB3)[names(dat_PSB3) == "PSB3"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PSB3)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PSB3
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female PSB3
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PSB3
## 4 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PSB3
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PSB3
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PSB3
## Creat dat_PSB4 - cot 6 cua SEM1_likert
dat_PSB4 <- SEM1_likert [, c(1,2,6,31, 32, 33, 34, 35)]
head(dat_PSB4)
##     AGE      CITY PSB4 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    4   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
dim(dat_PSB4)
## [1] 863   8
size <- 863
dat_PSB4$BPE <- rep("PSB4", size = size) # Tạo biến mới BPE
names(dat_PSB4)[names(dat_PSB4) == "PSB4"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PSB4)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PSB4
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female PSB4
## 3 16-25 HoChiMinh   4   Never   Never  Never  Never   Male PSB4
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PSB4
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PSB4
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PSB4
## Creat dat_LOY1 - cot 7 cua SEM1_likert
dat_LOY1 <- SEM1_likert [, c(1,2,7,31, 32, 33, 34, 35)]
head(dat_LOY1)
##     AGE      CITY LOY1 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    2   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    4   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
dim(dat_LOY1)
## [1] 863   8
size <- 863
dat_LOY1$BPE <- rep("LOY1", size = size) # Tạo biến mới BPE
names(dat_LOY1)[names(dat_LOY1) == "LOY1"] <- "Res" # Đổi tên biến BST thành Res
head(dat_LOY1)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female LOY1
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female LOY1
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male LOY1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY1
## 5 16-25 HoChiMinh   4   Never    Ever  Never   Ever   Male LOY1
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female LOY1
## Creat dat_LOY2 - cot 8 cua SEM1_likert
dat_LOY2 <- SEM1_likert [, c(1,2,8,31, 32, 33, 34, 35)]
head(dat_LOY2)
##     AGE      CITY LOY2 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    5   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    3   Never    Ever  Never   Ever Female
dim(dat_LOY2)
## [1] 863   8
size <- 863

dat_LOY2$BPE <- rep("LOY2", size = size) # Tạo biến mới BPE
names(dat_LOY2)[names(dat_LOY2) == "LOY2"] <- "Res" # Đổi tên biến BST thành Res
head(dat_LOY2)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female LOY2
## 2 16-25 HoChiMinh   5   Never   Never   Ever  Never Female LOY2
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male LOY2
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY2
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY2
## 6 16-25 HoChiMinh   3   Never    Ever  Never   Ever Female LOY2
## Creat dat_LOY3 - cot 9 cua SEM1_likert
dat_LOY3 <- SEM1_likert [, c(1,2,9,31, 32, 33, 34, 35)]
head(dat_LOY3)
##     AGE      CITY LOY3 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    5   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    2   Never    Ever  Never   Ever Female
dim(dat_LOY3)
## [1] 863   8
size <- 863
dat_LOY3$BPE <- rep("LOY3", size = size) # Tạo biến mới BPE
names(dat_LOY3)[names(dat_LOY3) == "LOY3"] <- "Res" # Đổi tên biến BST thành Res
head(dat_LOY3)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   5   Never    Ever  Never   Ever Female LOY3
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female LOY3
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male LOY3
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY3
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY3
## 6 16-25 HoChiMinh   2   Never    Ever  Never   Ever Female LOY3
## Creat dat_LOY4 - cot 10 cua SEM1_likert
dat_LOY4 <- SEM1_likert [, c(1,2,10,31, 32, 33, 34, 35)]
head(dat_LOY4)
##     AGE      CITY LOY4 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    7   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    4   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    4   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    5   Never    Ever  Never   Ever Female
dim(dat_LOY4)
## [1] 863   8
size <- 863
dat_LOY4$BPE <- rep("LOY4", size = size) # Tạo biến mới BPE
names(dat_LOY4)[names(dat_LOY4) == "LOY4"] <- "Res" # Đổi tên biến BST thành Res
head(dat_LOY4)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   7   Never    Ever  Never   Ever Female LOY4
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female LOY4
## 3 16-25 HoChiMinh   4   Never   Never  Never  Never   Male LOY4
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY4
## 5 16-25 HoChiMinh   4   Never    Ever  Never   Ever   Male LOY4
## 6 16-25 HoChiMinh   5   Never    Ever  Never   Ever Female LOY4
## Creat dat_LIN5 - cot 11 cua SEM1_likert
dat_LOY5 <- SEM1_likert [, c(1,2,11,31, 32, 33, 34, 35)]
head(dat_LOY5)
##     AGE      CITY LOY5 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    4   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    4   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    5   Never    Ever  Never   Ever Female
dim(dat_LOY5)
## [1] 863   8
size <- 863
dat_LOY5$BPE <- rep("LOY5", size = size) # Tạo biến mới BPE
names(dat_LOY5)[names(dat_LOY5) == "LOY5"] <- "Res" # Đổi tên biến BST thành Res
head(dat_LOY5)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female LOY5
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female LOY5
## 3 16-25 HoChiMinh   4   Never   Never  Never  Never   Male LOY5
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY5
## 5 16-25 HoChiMinh   4   Never    Ever  Never   Ever   Male LOY5
## 6 16-25 HoChiMinh   5   Never    Ever  Never   Ever Female LOY5
## Creat dat_LOY6 - cot 12 cua SEM1_likert
dat_LOY6 <- SEM1_likert [, c(1,2,12,31, 32, 33, 34, 35)]
head(dat_LOY6)
##     AGE      CITY LOY6 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    2   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    4   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
dim(dat_LOY6)
## [1] 863   8
size <- 863
dat_LOY6$BPE <- rep("LOY6", size = size) # Tạo biến mới BPE
names(dat_LOY6)[names(dat_LOY6) == "LOY6"] <- "Res" # Đổi tên biến BST thành Res
head(dat_LOY6)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female LOY6
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female LOY6
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male LOY6
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY6
## 5 16-25 HoChiMinh   4   Never    Ever  Never   Ever   Male LOY6
## 6 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female LOY6
## Creat dat_LOY7 - cot 13 cua SEM1_likert
dat_LOY7 <- SEM1_likert [, c(1,2,13,31, 32, 33, 34, 35)]
head(dat_LOY7)
##     AGE      CITY LOY7 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    5   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    2   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
dim(dat_LOY7)
## [1] 863   8
size <- 863
dat_LOY7$BPE <- rep("LOY7", size = size) # Tạo biến mới BPE
names(dat_LOY7)[names(dat_LOY7) == "LOY7"] <- "Res" # Đổi tên biến BST thành Res
head(dat_LOY7)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female LOY7
## 2 16-25 HoChiMinh   5   Never   Never   Ever  Never Female LOY7
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male LOY7
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY7
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male LOY7
## 6 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female LOY7
## Creat dat_IMA1 - cot 14 cua SEM1_likert
dat_IMA1 <- SEM1_likert [, c(1,2,14,31, 32, 33, 34, 35)]
head(dat_IMA1)
##     AGE      CITY IMA1 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    4   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
dim(dat_IMA1)
## [1] 863   8
size <- 863
dat_IMA1$BPE <- rep("IMA1", size = size) # Tạo biến mới BPE
names(dat_IMA1)[names(dat_IMA1) == "IMA1"] <- "Res" # Đổi tên biến BST thành Res
head(dat_IMA1)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female IMA1
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female IMA1
## 3 16-25 HoChiMinh   4   Never   Never  Never  Never   Male IMA1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA1
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA1
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female IMA1
## Creat dat_IMA2 - cot 15 cua SEM1_likert
dat_IMA2 <- SEM1_likert [, c(1,2,15,31, 32, 33, 34, 35)]
head(dat_IMA2)
##     AGE      CITY IMA2 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    5   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    2   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    4   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    3   Never    Ever  Never   Ever Female
dim(dat_IMA2)
## [1] 863   8
size <- 863
dat_IMA2$BPE <- rep("IMA2", size = size) # Tạo biến mới BPE
names(dat_IMA2)[names(dat_IMA2) == "IMA2"] <- "Res" # Đổi tên biến BST thành Res
head(dat_IMA2)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female IMA2
## 2 16-25 HoChiMinh   5   Never   Never   Ever  Never Female IMA2
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male IMA2
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA2
## 5 16-25 HoChiMinh   4   Never    Ever  Never   Ever   Male IMA2
## 6 16-25 HoChiMinh   3   Never    Ever  Never   Ever Female IMA2
## Creat dat_IMA3 - cot 16 cua SEM1_likert
dat_IMA3 <- SEM1_likert [, c(1,2,16,31, 32, 33, 34, 35)]
head(dat_IMA3)
##     AGE      CITY IMA3 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    6   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    5   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    2   Never    Ever  Never   Ever Female
dim(dat_IMA3)
## [1] 863   8
size <- 863
dat_IMA3$BPE <- rep("IMA3", size = size) # Tạo biến mới BPE
names(dat_IMA3)[names(dat_IMA3) == "IMA3"] <- "Res" # Đổi tên biến BST thành Res
head(dat_IMA3)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female IMA3
## 2 16-25 HoChiMinh   6   Never   Never   Ever  Never Female IMA3
## 3 16-25 HoChiMinh   5   Never   Never  Never  Never   Male IMA3
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA3
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA3
## 6 16-25 HoChiMinh   2   Never    Ever  Never   Ever Female IMA3
## Creat dat_IMA4 - cot 17 cua SEM1_likert
dat_IMA4 <- SEM1_likert [, c(1,2,17,31, 32, 33, 34, 35)]
head(dat_IMA4)
##     AGE      CITY IMA4 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    5   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    2   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    4   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    2   Never    Ever  Never   Ever Female
dim(dat_IMA4)
## [1] 863   8
size <- 863
dat_IMA4$BPE <- rep("IMA4", size = size) # Tạo biến mới BPE
names(dat_IMA4)[names(dat_IMA4) == "IMA4"] <- "Res" # Đổi tên biến BST thành Res
head(dat_IMA4)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female IMA4
## 2 16-25 HoChiMinh   5   Never   Never   Ever  Never Female IMA4
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male IMA4
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA4
## 5 16-25 HoChiMinh   4   Never    Ever  Never   Ever   Male IMA4
## 6 16-25 HoChiMinh   2   Never    Ever  Never   Ever Female IMA4
## Creat dat_IMA5 - cot 18 cua SEM1_likert
dat_IMA5 <- SEM1_likert [, c(1,2,18,31, 32, 33, 34, 35)]
head(dat_IMA5)
##     AGE      CITY IMA5 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    2   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
dim(dat_IMA5)
## [1] 863   8
size <- 863
dat_IMA5$BPE <- rep("IMA5", size = size) # Tạo biến mới BPE
names(dat_IMA5)[names(dat_IMA5) == "IMA5"] <- "Res" # Đổi tên biến BST thành Res
head(dat_IMA5)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female IMA5
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female IMA5
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male IMA5
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA5
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA5
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female IMA5
## Creat dat_PHB1 - cot 19 cua SEM1_likert
dat_PHB1 <- SEM1_likert [, c(1,2,19,31, 32, 33, 34, 35)]
head(dat_PHB1)
##     AGE      CITY PHB1 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    3   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    5   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
dim(dat_PHB1)
## [1] 863   8
size <- 863
dat_PHB1$BPE <- rep("PHB1", size = size) # Tạo biến mới BPE
names(dat_PHB1)[names(dat_PHB1) == "PHB1"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PHB1)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PHB1
## 2 16-25 HoChiMinh   3   Never   Never   Ever  Never Female PHB1
## 3 16-25 HoChiMinh   5   Never   Never  Never  Never   Male PHB1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PHB1
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PHB1
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PHB1
## Creat dat_PHB2 - cot 20 cua SEM1_likert
dat_PHB2 <- SEM1_likert [, c(1,2,20,31, 32, 33, 34, 35)]
head(dat_PHB2)
##     AGE      CITY PHB2 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    7   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    2   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
dim(dat_PHB2)
## [1] 863   8
size <- 863
dat_PHB2$BPE <- rep("PHB2", size = size) # Tạo biến mới BPE
names(dat_PHB2)[names(dat_PHB2) == "PHB2"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PHB2)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   7   Never    Ever  Never   Ever Female PHB2
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female PHB2
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male PHB2
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PHB2
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PHB2
## 6 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PHB2
## Creat dat_PHB3 - cot 21 cua SEN1_likert
dat_PHB3 <- SEM1_likert [, c(1,2,21,31, 32, 33, 34, 35)]
head(dat_PHB3)
##     AGE      CITY PHB3 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    3   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    7   Never    Ever  Never   Ever Female
dim(dat_PHB3)
## [1] 863   8
size <- 863
dat_PHB3$BPE <- rep("PHB3", size = size) # Tạo biến mới BPE
names(dat_PHB3)[names(dat_PHB3) == "PHB3"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PHB3)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PHB3
## 2 16-25 HoChiMinh   3   Never   Never   Ever  Never Female PHB3
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PHB3
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PHB3
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PHB3
## 6 16-25 HoChiMinh   7   Never    Ever  Never   Ever Female PHB3
## Creat dat_PHB4 - cot 22 cua SEM1_likert
dat_PHB4 <- SEM1_likert [, c(1,2,22,31, 32, 33, 34, 35)]
head(dat_PHB4)
##     AGE      CITY PHB4 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    7   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
dim(dat_PHB4)
## [1] 863   8
size <- 863
dat_PHB4$BPE <- rep("PHB4", size = size) # Tạo biến mới BPE
names(dat_PHB4)[names(dat_PHB4) == "PHB4"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PHB4)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   7   Never    Ever  Never   Ever Female PHB4
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female PHB4
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PHB4
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PHB4
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PHB4
## 6 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PHB4
## Creat dat_PHB5 - cot 23 cua SEM1_likert
dat_PHB5 <- SEM1_likert [, c(1,2,23,31, 32, 33, 34, 35)]
head(dat_PHB5)
##     AGE      CITY PHB5 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    7   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    6   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
dim(dat_PHB5)
## [1] 863   8
size <- 863
dat_PHB5$BPE <- rep("PHB5", size = size) # Tạo biến mới BPE
names(dat_PHB5)[names(dat_PHB5) == "PHB5"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PHB5)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   7   Never    Ever  Never   Ever Female PHB5
## 2 16-25 HoChiMinh   6   Never   Never   Ever  Never Female PHB5
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PHB5
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PHB5
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PHB5
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PHB5
## Creat dat_PEV1 - cot 24 cua SEM1_likert
dat_PEV1 <- SEM1_likert [, c(1,2,24,31, 32, 33, 34, 35)]
head(dat_PEV1)
##     AGE      CITY PEV1 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    5   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    7   Never    Ever  Never   Ever Female
dim(dat_PEV1)
## [1] 863   8
size <- 863
dat_PEV1$BPE <- rep("PEV1", size = size) # Tạo biến mới BPE
names(dat_PEV1)[names(dat_PEV1) == "PEV1"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PEV1)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PEV1
## 2 16-25 HoChiMinh   5   Never   Never   Ever  Never Female PEV1
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PEV1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PEV1
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PEV1
## 6 16-25 HoChiMinh   7   Never    Ever  Never   Ever Female PEV1
dim(dat_PEV1)
## [1] 863   9
## Creat dat_PEV2 - cot 25 cua SEM1_likert
dat_PEV2 <- SEM1_likert [, c(1,2,25,31, 32, 33, 34, 35)]
head(dat_PEV2)
##     AGE      CITY PEV2 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    6   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    5   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    7   Never    Ever  Never   Ever Female
dim(dat_PEV2)
## [1] 863   8
size <- 863
dat_PEV2$BPE <- rep("PEV2", size = size) # Tạo biến mới BPE
names(dat_PEV2)[names(dat_PEV2) == "PEV2"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PEV2)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PEV2
## 2 16-25 HoChiMinh   6   Never   Never   Ever  Never Female PEV2
## 3 16-25 HoChiMinh   5   Never   Never  Never  Never   Male PEV2
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PEV2
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PEV2
## 6 16-25 HoChiMinh   7   Never    Ever  Never   Ever Female PEV2
dim(dat_PEV2)
## [1] 863   9
## Creat dat_PEV3 - cot 26 cua SEM1_likert
dat_PEV3 <- SEM1_likert [, c(1,2,26,31, 32, 33, 34, 35)]
head(dat_PEV3)
##     AGE      CITY PEV3 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    6   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    5   Never    Ever  Never   Ever Female
dim(dat_PEV3)
## [1] 863   8
size <- 863
dat_PEV3$BPE <- rep("PEV3", size = size) # Tạo biến mới BPE
names(dat_PEV3)[names(dat_PEV3) == "PEV3"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PEV3)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PEV3
## 2 16-25 HoChiMinh   6   Never   Never   Ever  Never Female PEV3
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PEV3
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PEV3
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PEV3
## 6 16-25 HoChiMinh   5   Never    Ever  Never   Ever Female PEV3
dim(dat_PEV3)
## [1] 863   9
## Creat dat_PEV4 - cot 27 cua SEM1_likert
dat_PEV4 <- SEM1_likert [, c(1,2,27,31, 32, 33, 34, 35)]
head(dat_PEV4)
##     AGE      CITY PEV4 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    4   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    4   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    2   Never    Ever  Never   Ever Female
dim(dat_PEV4)
## [1] 863   8
size <- 863
dat_PEV4$BPE <- rep("PEV4", size = size) # Tạo biến mới BPE
names(dat_PEV4)[names(dat_PEV4) == "PEV4"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PEV4)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PEV4
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female PEV4
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PEV4
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PEV4
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PEV4
## 6 16-25 HoChiMinh   2   Never    Ever  Never   Ever Female PEV4
dim(PEV4)
## NULL
## Creat dat_PVA1 - cot 28 cua SEM1_likert
dat_PVA1 <- SEM1_likert [, c(1,2,28,31, 32, 33, 34, 35)]
head(dat_PVA1)
##     AGE      CITY PVA1 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    5   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    6   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    6   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
dim(dat_PVA1)
## [1] 863   8
size <- 863
dat_PVA1$BPE <- rep("PVA1", size = size) # Tạo biến mới BPE
names(dat_PVA1)[names(dat_PVA1) == "PVA1"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PVA1)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PVA1
## 2 16-25 HoChiMinh   5   Never   Never   Ever  Never Female PVA1
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PVA1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PVA1
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PVA1
## 6 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PVA1
## Creat dat_PVA2 - cot 29 cua SEM1_likert
dat_PVA2 <- SEM1_likert [, c(1,2,29,31, 32, 33, 34, 35)]
head(dat_PVA2)
##     AGE      CITY PVA2 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    5   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    2   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    2   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    5   Never    Ever  Never   Ever Female
dim(dat_PVA2)
## [1] 863   8
size <- 863
dat_PVA2$BPE <- rep("PVA2", size = size) # Tạo biến mới BPE
names(dat_PVA2)[names(dat_PVA2) == "PVA2"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PVA2)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PVA2
## 2 16-25 HoChiMinh   5   Never   Never   Ever  Never Female PVA2
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male PVA2
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PVA2
## 5 16-25 HoChiMinh   2   Never    Ever  Never   Ever   Male PVA2
## 6 16-25 HoChiMinh   5   Never    Ever  Never   Ever Female PVA2
## Creat dat_PVA3 - cot 30 cua SEM1_likert
dat_PVA3 <- SEM1_likert [, c(1,2,30,31, 32, 33, 34, 35)]
head(dat_PVA3)
##     AGE      CITY PVA3 EC_Stop WC_Stop EC_Bus WC_Bus Gender
## 1 16-25 HoChiMinh    6   Never    Ever  Never   Ever Female
## 2 16-25 HoChiMinh    3   Never   Never   Ever  Never Female
## 3 16-25 HoChiMinh    4   Never   Never  Never  Never   Male
## 4 16-25 HoChiMinh    5   Never    Ever  Never   Ever   Male
## 5 16-25 HoChiMinh    3   Never    Ever  Never   Ever   Male
## 6 16-25 HoChiMinh    5   Never    Ever  Never   Ever Female
dim(dat_PVA3)
## [1] 863   8
size <- 863
dat_PVA3$BPE <- rep("PVA3", size = size) # Tạo biến mới BPE
names(dat_PVA3)[names(dat_PVA3) == "PVA3"] <- "Res" # Đổi tên biến BST thành Res
head(dat_PVA3)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PVA3
## 2 16-25 HoChiMinh   3   Never   Never   Ever  Never Female PVA3
## 3 16-25 HoChiMinh   4   Never   Never  Never  Never   Male PVA3
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PVA3
## 5 16-25 HoChiMinh   3   Never    Ever  Never   Ever   Male PVA3
## 6 16-25 HoChiMinh   5   Never    Ever  Never   Ever Female PVA3
## Tạo dữ liệu likert từ 28 dữ liệu: dat_PSA1-4, dat_LOY1-7, dat_IMA1-5, dat_PHB1-5, dat_PEV1-4, dat_PVA1-3 
B.Per <- rbind(dat_PSB1, dat_PSB2, dat_PSB3, dat_PSB4, dat_LOY1, dat_LOY2, dat_LOY3, dat_LOY4, dat_LOY5, dat_LOY6, dat_LOY7, dat_IMA1, dat_IMA2, dat_IMA3, dat_IMA4, dat_IMA5, dat_PHB1, dat_PHB2, dat_PHB3, dat_PHB4, dat_PHB5, dat_PEV1, dat_PEV2, dat_PEV3, dat_PEV4, dat_PVA1, dat_PVA2, dat_PVA3)
head(B.Per)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PSB1
## 2 16-25 HoChiMinh   3   Never   Never   Ever  Never Female PSB1
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male PSB1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PSB1
## 5 16-25 HoChiMinh   2   Never    Ever  Never   Ever   Male PSB1
## 6 16-25 HoChiMinh   2   Never    Ever  Never   Ever Female PSB1
str(B.Per)
## 'data.frame':    24164 obs. of  9 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Res    : int  6 3 2 5 2 2 1 6 2 4 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ BPE    : chr  "PSB1" "PSB1" "PSB1" "PSB1" ...
dim(B.Per)
## [1] 24164     9
## Tạo dữ liệu likert theo constructs
B.Per_PSB <- rbind(dat_PSB1, dat_PSB2, dat_PSB3, dat_PSB4)
head(B.Per_PSB)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PSB1
## 2 16-25 HoChiMinh   3   Never   Never   Ever  Never Female PSB1
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male PSB1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PSB1
## 5 16-25 HoChiMinh   2   Never    Ever  Never   Ever   Male PSB1
## 6 16-25 HoChiMinh   2   Never    Ever  Never   Ever Female PSB1
str(B.Per_PSB)
## 'data.frame':    3452 obs. of  9 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Res    : int  6 3 2 5 2 2 1 6 2 4 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ BPE    : chr  "PSB1" "PSB1" "PSB1" "PSB1" ...
dim(B.Per_PSB)
## [1] 3452    9
B.Per_LOY <- rbind(dat_LOY1, dat_LOY2, dat_LOY3, dat_LOY4, dat_LOY5, dat_LOY6, dat_LOY7)
head(B.Per_LOY)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female LOY1
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female LOY1
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male LOY1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male LOY1
## 5 16-25 HoChiMinh   4   Never    Ever  Never   Ever   Male LOY1
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female LOY1
str(B.Per_LOY)
## 'data.frame':    6041 obs. of  9 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Res    : int  4 4 2 5 4 4 2 6 6 6 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ BPE    : chr  "LOY1" "LOY1" "LOY1" "LOY1" ...
dim(B.Per_LOY)
## [1] 6041    9
B.Per_IMA <- rbind(dat_IMA1, dat_IMA2, dat_IMA3, dat_IMA4, dat_IMA5)
head(B.Per_IMA)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female IMA1
## 2 16-25 HoChiMinh   4   Never   Never   Ever  Never Female IMA1
## 3 16-25 HoChiMinh   4   Never   Never  Never  Never   Male IMA1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA1
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male IMA1
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female IMA1
str(B.Per_IMA)
## 'data.frame':    4315 obs. of  9 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Res    : int  6 4 4 5 5 4 2 6 5 6 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ BPE    : chr  "IMA1" "IMA1" "IMA1" "IMA1" ...
dim(B.Per_IMA)
## [1] 4315    9
B.Per_PHB <- rbind(dat_PHB1, dat_PHB2, dat_PHB3, dat_PHB4, dat_PHB5)
head(B.Per_PHB)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PHB1
## 2 16-25 HoChiMinh   3   Never   Never   Ever  Never Female PHB1
## 3 16-25 HoChiMinh   5   Never   Never  Never  Never   Male PHB1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PHB1
## 5 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PHB1
## 6 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PHB1
str(B.Per_PHB)
## 'data.frame':    4315 obs. of  9 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Res    : int  6 3 5 5 5 4 3 6 6 7 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ BPE    : chr  "PHB1" "PHB1" "PHB1" "PHB1" ...
dim(B.Per_PHB)
## [1] 4315    9
B.Per_PEV <- rbind(dat_PEV1, dat_PEV2, dat_PEV3, dat_PEV4)
head(B.Per_PEV)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   4   Never    Ever  Never   Ever Female PEV1
## 2 16-25 HoChiMinh   5   Never   Never   Ever  Never Female PEV1
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PEV1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PEV1
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PEV1
## 6 16-25 HoChiMinh   7   Never    Ever  Never   Ever Female PEV1
str(B.Per_PEV)
## 'data.frame':    3452 obs. of  9 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Res    : int  4 5 6 5 6 7 4 6 7 7 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ BPE    : chr  "PEV1" "PEV1" "PEV1" "PEV1" ...
dim(B.Per_PEV)
## [1] 3452    9
B.Per_PVA <- rbind(dat_PVA1, dat_PVA2, dat_PVA3)
head(B.Per_PVA)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PVA1
## 2 16-25 HoChiMinh   5   Never   Never   Ever  Never Female PVA1
## 3 16-25 HoChiMinh   6   Never   Never  Never  Never   Male PVA1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PVA1
## 5 16-25 HoChiMinh   6   Never    Ever  Never   Ever   Male PVA1
## 6 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PVA1
str(B.Per_PVA)
## 'data.frame':    2589 obs. of  9 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Res    : int  6 5 6 5 6 6 6 6 6 6 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ BPE    : chr  "PVA1" "PVA1" "PVA1" "PVA1" ...
dim(B.Per_PVA)
## [1] 2589    9
# Cách 2: Tạo dữ liệu thủ công trên excel với các cột BPE, Res, BUB, MSI, SBM, SBS: t_likert = "F:\\NGHIEN CUU SINH\\NCS - PHUONG ANH\\Part 3-Mode shift\\So lieu mode choice tu phan 1\\8. MS-Bieu do Likert.csv" ; BP_likert = read.csv(t_likert, header = T, sep = ";")

# Data Bus perception of people in Da Nang
DN_SEM1 = subset(B.Per, CITY == "DaNang")
head(DN_SEM1)
##       AGE   CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 449 26-35 DaNang   6   Never   Never  Never  Never   Male PSB1
## 450 46-55 DaNang   6   Never   Never  Never  Never Female PSB1
## 451 46-55 DaNang   6   Never   Never  Never  Never Female PSB1
## 452 26-35 DaNang   5   Never   Never  Never  Never Female PSB1
## 453 46-55 DaNang   6   Never   Never  Never  Never Female PSB1
## 454 16-25 DaNang   6   Never   Never  Never  Never Female PSB1
str(DN_SEM1)
## 'data.frame':    11620 obs. of  9 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 2 4 4 2 4 1 1 2 1 2 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Res    : int  6 6 6 5 6 6 6 7 4 6 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 2 1 1 1 1 1 1 2 2 1 ...
##  $ BPE    : chr  "PSB1" "PSB1" "PSB1" "PSB1" ...
dim(DN_SEM1)
## [1] 11620     9
DN_SEM1 = DN_SEM1[ , c(3, 9)]
dim(DN_SEM1)
## [1] 11620     2
head(DN_SEM1)
##     Res  BPE
## 449   6 PSB1
## 450   6 PSB1
## 451   6 PSB1
## 452   5 PSB1
## 453   6 PSB1
## 454   6 PSB1
# Data Bus perception of people in Ho Chi Minh
HCM_SEM1 = subset(B.Per, CITY == "HoChiMinh")
head(HCM_SEM1)
##     AGE      CITY Res EC_Stop WC_Stop EC_Bus WC_Bus Gender  BPE
## 1 16-25 HoChiMinh   6   Never    Ever  Never   Ever Female PSB1
## 2 16-25 HoChiMinh   3   Never   Never   Ever  Never Female PSB1
## 3 16-25 HoChiMinh   2   Never   Never  Never  Never   Male PSB1
## 4 16-25 HoChiMinh   5   Never    Ever  Never   Ever   Male PSB1
## 5 16-25 HoChiMinh   2   Never    Ever  Never   Ever   Male PSB1
## 6 16-25 HoChiMinh   2   Never    Ever  Never   Ever Female PSB1
str(HCM_SEM1)
## 'data.frame':    12544 obs. of  9 variables:
##  $ AGE    : Factor w/ 5 levels "16-25","26-35",..: 1 1 1 1 1 1 1 3 1 1 ...
##  $ CITY   : Factor w/ 2 levels "DaNang","HoChiMinh": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Res    : int  6 3 2 5 2 2 1 6 2 4 ...
##  $ EC_Stop: Factor w/ 2 levels "Never","Ever": 1 1 1 1 1 1 1 1 1 1 ...
##  $ WC_Stop: Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ EC_Bus : Factor w/ 2 levels "Never","Ever": 1 2 1 1 1 1 1 1 1 1 ...
##  $ WC_Bus : Factor w/ 2 levels "Never","Ever": 2 1 1 2 2 2 1 1 2 1 ...
##  $ Gender : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 1 1 1 1 ...
##  $ BPE    : chr  "PSB1" "PSB1" "PSB1" "PSB1" ...
dim(HCM_SEM1)
## [1] 12544     9
HCM_SEM1 = HCM_SEM1[ , c(3, 9)]
dim(HCM_SEM1)
## [1] 12544     2
head(HCM_SEM1)
##   Res  BPE
## 1   6 PSB1
## 2   3 PSB1
## 3   2 PSB1
## 4   5 PSB1
## 5   2 PSB1
## 6   2 PSB1
# Data Bus perception of people in both Da Nang and Ho Chi Minh cities
DN_HCM_SEM1 = B.Per[, c(3, 9)]
str(DN_HCM_SEM1)
## 'data.frame':    24164 obs. of  2 variables:
##  $ Res: int  6 3 2 5 2 2 1 6 2 4 ...
##  $ BPE: chr  "PSB1" "PSB1" "PSB1" "PSB1" ...
dim(DN_HCM_SEM1)
## [1] 24164     2
head(DN_HCM_SEM1)
##   Res  BPE
## 1   6 PSB1
## 2   3 PSB1
## 3   2 PSB1
## 4   5 PSB1
## 5   2 PSB1
## 6   2 PSB1
# Data bus perception of people in both Da Nang and Ho Chi Minh cities - according constructs 
## Construct PSA
DN_HCM_SEM1_PSB = B.Per_PSB[ , c(3, 9)]
dim(DN_HCM_SEM1_PSB)
## [1] 3452    2
head(DN_HCM_SEM1_PSB)
##   Res  BPE
## 1   6 PSB1
## 2   3 PSB1
## 3   2 PSB1
## 4   5 PSB1
## 5   2 PSB1
## 6   2 PSB1
## Construct LOY
DN_HCM_SEM1_LOY = B.Per_LOY[ , c(3, 9)]
dim(DN_HCM_SEM1_LOY)
## [1] 6041    2
head(DN_HCM_SEM1_LOY)
##   Res  BPE
## 1   4 LOY1
## 2   4 LOY1
## 3   2 LOY1
## 4   5 LOY1
## 5   4 LOY1
## 6   4 LOY1
## Construct IMA
DN_HCM_SEM1_IMA = B.Per_IMA[ , c(3, 9)]
dim(DN_HCM_SEM1_IMA)
## [1] 4315    2
head(DN_HCM_SEM1_IMA)
##   Res  BPE
## 1   6 IMA1
## 2   4 IMA1
## 3   4 IMA1
## 4   5 IMA1
## 5   5 IMA1
## 6   4 IMA1
## Construct PHB
DN_HCM_SEM1_PHB = B.Per_PHB[ , c(3, 9)]
dim(DN_HCM_SEM1_PHB)
## [1] 4315    2
head(DN_HCM_SEM1_PHB)
##   Res  BPE
## 1   6 PHB1
## 2   3 PHB1
## 3   5 PHB1
## 4   5 PHB1
## 5   5 PHB1
## 6   4 PHB1
## Construct PEV
DN_HCM_SEM1_PEV = B.Per_PEV[ , c(3, 9)]
dim(DN_HCM_SEM1_PEV)
## [1] 3452    2
head(DN_HCM_SEM1_PEV)
##   Res  BPE
## 1   4 PEV1
## 2   5 PEV1
## 3   6 PEV1
## 4   5 PEV1
## 5   6 PEV1
## 6   7 PEV1
## Construct PVA
DN_HCM_SEM1_PVA = B.Per_PVA[ , c(3, 9)]
dim(DN_HCM_SEM1_PVA)
## [1] 2589    2
head(DN_HCM_SEM1_PVA)
##   Res  BPE
## 1   6 PVA1
## 2   5 PVA1
## 3   6 PVA1
## 4   5 PVA1
## 5   6 PVA1
## 6   6 PVA1

3.2. Vẽ biểu đồ likert với dữ liệu chung Đà Nẵng và Hồ Chí Minh - Nhãn tiếng anh

# Plot Likert graph for people in both Da Nang and Ho Chi Minh cities
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.4     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x tidyr::extract()   masks magrittr::extract()
## x dplyr::filter()    masks stats::filter()
## x dplyr::lag()       masks stats::lag()
## x purrr::set_names() masks magrittr::set_names()
library(compareGroups)
head(DN_HCM_SEM1)
##   Res  BPE
## 1   6 PSB1
## 2   3 PSB1
## 3   2 PSB1
## 4   5 PSB1
## 5   2 PSB1
## 6   2 PSB1
dim(DN_HCM_SEM1)
## [1] 24164     2
attach(DN_HCM_SEM1)
DN_HCM_SEM1 = within(DN_HCM_SEM1, {
  Res = factor(Res, labels = c("Very Disagree", "Disagree", "Quite Disagree", "Normal", "Quite Agree", "Agree", "Very Agree"))
  BPE = factor(BPE, labels = c("PSB1", "PSB2", "PSB3", "PSB4", "LOY1", "LOY2", "LOY3", "LOY4", "LOY5", "LOY6", "LOY7", "IMA1", "IMA2", "IMA3", "IMA4", "IMA5", "PHB1", "PHB2", "PHB3", "PHB4", "PHB5", "PEV1", "PEV2", "PEV3", "PEV4", "PVA1", "PVA2", "PVA3"))
  })
str(DN_HCM_SEM1)
## 'data.frame':    24164 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "Very Disagree",..: 6 3 2 5 2 2 1 6 2 4 ...
##  $ BPE: Factor w/ 28 levels "PSB1","PSB2",..: 22 22 22 22 22 22 22 22 22 22 ...
summary(DN_HCM_SEM1)
##              Res             BPE       
##  Very Disagree :  460   PSB1   :  863  
##  Disagree      :  746   PSB2   :  863  
##  Quite Disagree:  768   PSB3   :  863  
##  Normal        : 2719   PSB4   :  863  
##  Quite Agree   : 4804   LOY1   :  863  
##  Agree         :10731   LOY2   :  863  
##  Very Agree    : 3936   (Other):18986
    ## t <- compareGroups(Res ~ BPE, data = DN_HCM_SEM1)
    ## createTable(t)
DN_HCM_SEM1 %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  # arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
##  [1] PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 IMA2 IMA3 IMA4
## [16] IMA5 PHB1 PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4 PVA1 PVA2 PVA3
## 28 Levels: PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 ... PVA3
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 196 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PSB1  Very Disagree      6    0.7  0.7%    
##  2 PSB1  Disagree          20    2.32 2.32%   
##  3 PSB1  Quite Disagree    32    3.71 3.71%   
##  4 PSB1  Normal           156   18.1  18.08%  
##  5 PSB1  Quite Agree      230   26.6  26.65%  
##  6 PSB1  Agree            354   41.0  41.02%  
##  7 PSB1  Very Agree        65    7.53 7.53%   
##  8 PSB2  Very Disagree     11    1.27 1.27%   
##  9 PSB2  Disagree          19    2.2  2.2%    
## 10 PSB2  Quite Disagree    26    3.01 3.01%   
## # ... with 186 more rows
library(extrafont)
## Registering fonts with R
theme_set(theme_minimal())
gg <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "Perception of passengers towards bus systems in urban areas of Viet Nam", 
       subtitle = "Likert scale is a type of rating scale commonly used in surveys. When responding to a Likert type question,\nrespondents simply state their level of agreement or disagreement on a symmetric agree-disagree scale.")
gg
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "Very Disagree") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 28 x 5
##    BPE   Res               n percent bar_text
##    <fct> <fct>         <int>   <dbl> <chr>   
##  1 PSB1  Very Disagree     6    0.7  0.7%    
##  2 PSB2  Very Disagree    11    1.27 1.27%   
##  3 PSB3  Very Disagree    11    1.27 1.27%   
##  4 PSB4  Very Disagree    14    1.62 1.62%   
##  5 LOY1  Very Disagree     6    0.7  0.7%    
##  6 LOY2  Very Disagree     4    0.46 0.46%   
##  7 LOY3  Very Disagree     6    0.7  0.7%    
##  8 LOY4  Very Disagree     3    0.35 0.35%   
##  9 LOY5  Very Disagree    13    1.51 1.51%   
## 10 LOY6  Very Disagree    12    1.39 1.39%   
## # ... with 18 more rows
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Disagree") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 28 x 5
##    BPE   Res          n percent bar_text
##    <fct> <fct>    <int>   <dbl> <chr>   
##  1 PSB1  Disagree    20    2.32 2.32%   
##  2 PSB2  Disagree    19    2.2  2.2%    
##  3 PSB3  Disagree    11    1.27 1.27%   
##  4 PSB4  Disagree    14    1.62 1.62%   
##  5 LOY1  Disagree    21    2.43 2.43%   
##  6 LOY2  Disagree    18    2.09 2.09%   
##  7 LOY3  Disagree    22    2.55 2.55%   
##  8 LOY4  Disagree    16    1.85 1.85%   
##  9 LOY5  Disagree    18    2.09 2.09%   
## 10 LOY6  Disagree    24    2.78 2.78%   
## # ... with 18 more rows
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Quite Disagree") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 28 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PSB1  Quite Disagree    32    3.71 3.71%   
##  2 PSB2  Quite Disagree    26    3.01 3.01%   
##  3 PSB3  Quite Disagree    20    2.32 2.32%   
##  4 PSB4  Quite Disagree    26    3.01 3.01%   
##  5 LOY1  Quite Disagree    21    2.43 2.43%   
##  6 LOY2  Quite Disagree    20    2.32 2.32%   
##  7 LOY3  Quite Disagree    17    1.97 1.97%   
##  8 LOY4  Quite Disagree    19    2.2  2.2%    
##  9 LOY5  Quite Disagree    32    3.71 3.71%   
## 10 LOY6  Quite Disagree    26    3.01 3.01%   
## # ... with 18 more rows
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "Normal") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 28 x 5
##    BPE   Res        n percent bar_text
##    <fct> <fct>  <int>   <dbl> <chr>   
##  1 PSB1  Normal   156    18.1 18.08%  
##  2 PSB2  Normal   160    18.5 18.54%  
##  3 PSB3  Normal   130    15.1 15.06%  
##  4 PSB4  Normal   114    13.2 13.21%  
##  5 LOY1  Normal   106    12.3 12.28%  
##  6 LOY2  Normal   114    13.2 13.21%  
##  7 LOY3  Normal    99    11.5 11.47%  
##  8 LOY4  Normal    92    10.7 10.66%  
##  9 LOY5  Normal   130    15.1 15.06%  
## 10 LOY6  Normal   111    12.9 12.86%  
## # ... with 18 more rows
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Quite Agree") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 28 x 5
##    BPE   Res             n percent bar_text
##    <fct> <fct>       <int>   <dbl> <chr>   
##  1 PSB1  Quite Agree   230    26.6 26.65%  
##  2 PSB2  Quite Agree   213    24.7 24.68%  
##  3 PSB3  Quite Agree   209    24.2 24.22%  
##  4 PSB4  Quite Agree   201    23.3 23.29%  
##  5 LOY1  Quite Agree   205    23.8 23.75%  
##  6 LOY2  Quite Agree   168    19.5 19.47%  
##  7 LOY3  Quite Agree   171    19.8 19.81%  
##  8 LOY4  Quite Agree   161    18.7 18.66%  
##  9 LOY5  Quite Agree   212    24.6 24.57%  
## 10 LOY6  Quite Agree   214    24.8 24.8%   
## # ... with 18 more rows
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "Agree") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 28 x 5
##    BPE   Res       n percent bar_text
##    <fct> <fct> <int>   <dbl> <chr>   
##  1 PSB1  Agree   354    41.0 41.02%  
##  2 PSB2  Agree   368    42.6 42.64%  
##  3 PSB3  Agree   403    46.7 46.7%   
##  4 PSB4  Agree   408    47.3 47.28%  
##  5 LOY1  Agree   420    48.7 48.67%  
##  6 LOY2  Agree   433    50.2 50.17%  
##  7 LOY3  Agree   430    49.8 49.83%  
##  8 LOY4  Agree   440    51.0 50.98%  
##  9 LOY5  Agree   340    39.4 39.4%   
## 10 LOY6  Agree   347    40.2 40.21%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  Very Agree    65    7.53 7.53%   
##  2 PSB2  Very Agree    66    7.65 7.65%   
##  3 PSB3  Very Agree    79    9.15 9.15%   
##  4 PSB4  Very Agree    86    9.97 9.97%   
##  5 LOY1  Very Agree    84    9.73 9.73%   
##  6 LOY2  Very Agree   106   12.3  12.28%  
##  7 LOY3  Very Agree   118   13.7  13.67%  
##  8 LOY4  Very Agree   132   15.3  15.3%   
##  9 LOY5  Very Agree   118   13.7  13.67%  
## 10 LOY6  Very Agree   129   15.0  14.95%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") -> df_for_text
df_for_text
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  Very Agree    65    7.53 7.53%   
##  2 PSB2  Very Agree    66    7.65 7.65%   
##  3 PSB3  Very Agree    79    9.15 9.15%   
##  4 PSB4  Very Agree    86    9.97 9.97%   
##  5 LOY1  Very Agree    84    9.73 9.73%   
##  6 LOY2  Very Agree   106   12.3  12.28%  
##  7 LOY3  Very Agree   118   13.7  13.67%  
##  8 LOY4  Very Agree   132   15.3  15.3%   
##  9 LOY5  Very Agree   118   13.7  13.67%  
## 10 LOY6  Very Agree   129   15.0  14.95%  
## # ... with 18 more rows
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 2, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-35, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.3. Vẽ biểu đồ likert với dữ liệu chung Đà Nẵng và Hồ Chí Minh - Nhãn tiếng việt

# Nhan tieng viet
DN_HCM_SEM1_v = within(DN_HCM_SEM1, {
  Res = factor(Res, labels = c("R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD", "B\u00ECnh th\u01B0\u1EDDng", "Kh\u00E1 \u0111\u1ED3ng \u00FD", "\u0110\u1ED3ng \u00FD", "R\u1EA5t \u0111\u1ED3ng \u00FD"))
  BPE = factor(BPE, labels = c("PSB1", "PSB2", "PSB3", "PSB4", "LOY1", "LOY2", "LOY3", "LOY4", "LOY5", "LOY6", "LOY7", "IMA1", "IMA2", "IMA3", "IMA4", "IMA5", "PHB1", "PHB2", "PHB3", "PHB4", "PHB5", "PEV1", "PEV2", "PEV3", "PEV4", "PVA1", "PVA2", "PVA3"))
  })
str(DN_HCM_SEM1_v)
## 'data.frame':    24164 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "R<U+1EA5>t không d<U+1ED3>ng ý",..: 6 3 2 5 2 2 1 6 2 4 ...
##  $ BPE: Factor w/ 28 levels "PSB1","PSB2",..: 22 22 22 22 22 22 22 22 22 22 ...
summary(DN_HCM_SEM1_v)
##                              Res             BPE       
##  R<U+1EA5>t không d<U+1ED3>ng ý:  460   PSB1   :  863  
##  Không d<U+1ED3>ng ý           :  746   PSB2   :  863  
##  Khá không d<U+1ED3>ng ý       :  768   PSB3   :  863  
##  Bình thu<U+1EDD>ng            : 2719   PSB4   :  863  
##  Khá d<U+1ED3>ng ý             : 4804   LOY1   :  863  
##  Ð<U+1ED3>ng ý                 :10731   LOY2   :  863  
##  R<U+1EA5>t d<U+1ED3>ng ý      : 3936   (Other):18986
    ## t <- compareGroups(Res ~ BPE, data = DN_HCM_SEM1)
    ## createTable(t)
DN_HCM_SEM1_v %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  # arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
##  [1] PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 IMA2 IMA3 IMA4
## [16] IMA5 PHB1 PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4 PVA1 PVA2 PVA3
## 28 Levels: PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 ... PVA3
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 196 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t không d<U+1ED3>ng ý     6    0.7  0.7%    
##  2 PSB1  Không d<U+1ED3>ng ý        20    2.32 2.32%   
##  3 PSB1  Khá không d<U+1ED3>ng ý    32    3.71 3.71%   
##  4 PSB1  Bình thu<U+1EDD>ng        156   18.1  18.08%  
##  5 PSB1  Khá d<U+1ED3>ng ý         230   26.6  26.65%  
##  6 PSB1  Ð<U+1ED3>ng ý             354   41.0  41.02%  
##  7 PSB1  R<U+1EA5>t d<U+1ED3>ng ý          65    7.53 7.53%   
##  8 PSB2  R<U+1EA5>t không d<U+1ED3>ng ý    11    1.27 1.27%   
##  9 PSB2  Không d<U+1ED3>ng ý        19    2.2  2.2%    
## 10 PSB2  Khá không d<U+1ED3>ng ý    26    3.01 3.01%   
## # ... with 186 more rows
library(extrafont)
theme_set(theme_minimal())
gg_viet <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "C\u1EA3m nh\u1EADn c\u1EE7a h\u00E0nh kh\u00E1ch v\u1EC1 h\u1EC7 th\u1ED1ng xe bu\u00FDt \u0111\u00F4 th\u1ECB Vi\u1EC7t Nam", 
       subtitle = "Thang \u0111o Likert l\u00E0 lo\u1EA1i thang \u0111o \u0111i\u1EC3m \u0111\u00E1nh gi\u00E1 \u0111\u01B0\u1EE3c s\u1EED d\u1EE5ng ph\u1ED5 bi\u1EBFn trong kh\u1EA3o s\u00E1t. Nghi\u00EAn c\u1EE9u s\u1EED d\u1EE5ng thang \u0111o 7 m\u1EE9c \u0111\u1ED9 t\u01B0\u01A1ng \u1EE9ng v\u1EDBi 1 - R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD \u0111\u1EBFn 7 - R\u1EA5t \u0111\u1ED3ng \u00FD.")
gg_viet
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 28 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t không d<U+1ED3>ng ý     6    0.7  0.7%    
##  2 PSB2  R<U+1EA5>t không d<U+1ED3>ng ý    11    1.27 1.27%   
##  3 PSB3  R<U+1EA5>t không d<U+1ED3>ng ý    11    1.27 1.27%   
##  4 PSB4  R<U+1EA5>t không d<U+1ED3>ng ý    14    1.62 1.62%   
##  5 LOY1  R<U+1EA5>t không d<U+1ED3>ng ý     6    0.7  0.7%    
##  6 LOY2  R<U+1EA5>t không d<U+1ED3>ng ý     4    0.46 0.46%   
##  7 LOY3  R<U+1EA5>t không d<U+1ED3>ng ý     6    0.7  0.7%    
##  8 LOY4  R<U+1EA5>t không d<U+1ED3>ng ý     3    0.35 0.35%   
##  9 LOY5  R<U+1EA5>t không d<U+1ED3>ng ý    13    1.51 1.51%   
## 10 LOY6  R<U+1EA5>t không d<U+1ED3>ng ý    12    1.39 1.39%   
## # ... with 18 more rows
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 28 x 5
##    BPE   Res              n percent bar_text
##    <fct> <fct>        <int>   <dbl> <chr>   
##  1 PSB1  Không d<U+1ED3>ng ý    20    2.32 2.32%   
##  2 PSB2  Không d<U+1ED3>ng ý    19    2.2  2.2%    
##  3 PSB3  Không d<U+1ED3>ng ý    11    1.27 1.27%   
##  4 PSB4  Không d<U+1ED3>ng ý    14    1.62 1.62%   
##  5 LOY1  Không d<U+1ED3>ng ý    21    2.43 2.43%   
##  6 LOY2  Không d<U+1ED3>ng ý    18    2.09 2.09%   
##  7 LOY3  Không d<U+1ED3>ng ý    22    2.55 2.55%   
##  8 LOY4  Không d<U+1ED3>ng ý    16    1.85 1.85%   
##  9 LOY5  Không d<U+1ED3>ng ý    18    2.09 2.09%   
## 10 LOY6  Không d<U+1ED3>ng ý    24    2.78 2.78%   
## # ... with 18 more rows
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 28 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  Khá không d<U+1ED3>ng ý    32    3.71 3.71%   
##  2 PSB2  Khá không d<U+1ED3>ng ý    26    3.01 3.01%   
##  3 PSB3  Khá không d<U+1ED3>ng ý    20    2.32 2.32%   
##  4 PSB4  Khá không d<U+1ED3>ng ý    26    3.01 3.01%   
##  5 LOY1  Khá không d<U+1ED3>ng ý    21    2.43 2.43%   
##  6 LOY2  Khá không d<U+1ED3>ng ý    20    2.32 2.32%   
##  7 LOY3  Khá không d<U+1ED3>ng ý    17    1.97 1.97%   
##  8 LOY4  Khá không d<U+1ED3>ng ý    19    2.2  2.2%    
##  9 LOY5  Khá không d<U+1ED3>ng ý    32    3.71 3.71%   
## 10 LOY6  Khá không d<U+1ED3>ng ý    26    3.01 3.01%   
## # ... with 18 more rows
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "B\u00ECnh th\u01B0\u1EDDng") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 28 x 5
##    BPE   Res             n percent bar_text
##    <fct> <fct>       <int>   <dbl> <chr>   
##  1 PSB1  Bình thu<U+1EDD>ng   156    18.1 18.08%  
##  2 PSB2  Bình thu<U+1EDD>ng   160    18.5 18.54%  
##  3 PSB3  Bình thu<U+1EDD>ng   130    15.1 15.06%  
##  4 PSB4  Bình thu<U+1EDD>ng   114    13.2 13.21%  
##  5 LOY1  Bình thu<U+1EDD>ng   106    12.3 12.28%  
##  6 LOY2  Bình thu<U+1EDD>ng   114    13.2 13.21%  
##  7 LOY3  Bình thu<U+1EDD>ng    99    11.5 11.47%  
##  8 LOY4  Bình thu<U+1EDD>ng    92    10.7 10.66%  
##  9 LOY5  Bình thu<U+1EDD>ng   130    15.1 15.06%  
## 10 LOY6  Bình thu<U+1EDD>ng   111    12.9 12.86%  
## # ... with 18 more rows
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  Khá d<U+1ED3>ng ý   230    26.6 26.65%  
##  2 PSB2  Khá d<U+1ED3>ng ý   213    24.7 24.68%  
##  3 PSB3  Khá d<U+1ED3>ng ý   209    24.2 24.22%  
##  4 PSB4  Khá d<U+1ED3>ng ý   201    23.3 23.29%  
##  5 LOY1  Khá d<U+1ED3>ng ý   205    23.8 23.75%  
##  6 LOY2  Khá d<U+1ED3>ng ý   168    19.5 19.47%  
##  7 LOY3  Khá d<U+1ED3>ng ý   171    19.8 19.81%  
##  8 LOY4  Khá d<U+1ED3>ng ý   161    18.7 18.66%  
##  9 LOY5  Khá d<U+1ED3>ng ý   212    24.6 24.57%  
## 10 LOY6  Khá d<U+1ED3>ng ý   214    24.8 24.8%   
## # ... with 18 more rows
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "\u0110\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 28 x 5
##    BPE   Res        n percent bar_text
##    <fct> <fct>  <int>   <dbl> <chr>   
##  1 PSB1  Ð<U+1ED3>ng ý   354    41.0 41.02%  
##  2 PSB2  Ð<U+1ED3>ng ý   368    42.6 42.64%  
##  3 PSB3  Ð<U+1ED3>ng ý   403    46.7 46.7%   
##  4 PSB4  Ð<U+1ED3>ng ý   408    47.3 47.28%  
##  5 LOY1  Ð<U+1ED3>ng ý   420    48.7 48.67%  
##  6 LOY2  Ð<U+1ED3>ng ý   433    50.2 50.17%  
##  7 LOY3  Ð<U+1ED3>ng ý   430    49.8 49.83%  
##  8 LOY4  Ð<U+1ED3>ng ý   440    51.0 50.98%  
##  9 LOY5  Ð<U+1ED3>ng ý   340    39.4 39.4%   
## 10 LOY6  Ð<U+1ED3>ng ý   347    40.2 40.21%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t d<U+1ED3>ng ý    65    7.53 7.53%   
##  2 PSB2  R<U+1EA5>t d<U+1ED3>ng ý    66    7.65 7.65%   
##  3 PSB3  R<U+1EA5>t d<U+1ED3>ng ý    79    9.15 9.15%   
##  4 PSB4  R<U+1EA5>t d<U+1ED3>ng ý    86    9.97 9.97%   
##  5 LOY1  R<U+1EA5>t d<U+1ED3>ng ý    84    9.73 9.73%   
##  6 LOY2  R<U+1EA5>t d<U+1ED3>ng ý   106   12.3  12.28%  
##  7 LOY3  R<U+1EA5>t d<U+1ED3>ng ý   118   13.7  13.67%  
##  8 LOY4  R<U+1EA5>t d<U+1ED3>ng ý   132   15.3  15.3%   
##  9 LOY5  R<U+1EA5>t d<U+1ED3>ng ý   118   13.7  13.67%  
## 10 LOY6  R<U+1EA5>t d<U+1ED3>ng ý   129   15.0  14.95%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") -> df_for_text
df_for_text
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t d<U+1ED3>ng ý    65    7.53 7.53%   
##  2 PSB2  R<U+1EA5>t d<U+1ED3>ng ý    66    7.65 7.65%   
##  3 PSB3  R<U+1EA5>t d<U+1ED3>ng ý    79    9.15 9.15%   
##  4 PSB4  R<U+1EA5>t d<U+1ED3>ng ý    86    9.97 9.97%   
##  5 LOY1  R<U+1EA5>t d<U+1ED3>ng ý    84    9.73 9.73%   
##  6 LOY2  R<U+1EA5>t d<U+1ED3>ng ý   106   12.3  12.28%  
##  7 LOY3  R<U+1EA5>t d<U+1ED3>ng ý   118   13.7  13.67%  
##  8 LOY4  R<U+1EA5>t d<U+1ED3>ng ý   132   15.3  15.3%   
##  9 LOY5  R<U+1EA5>t d<U+1ED3>ng ý   118   13.7  13.67%  
## 10 LOY6  R<U+1EA5>t d<U+1ED3>ng ý   129   15.0  14.95%  
## # ... with 18 more rows
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg_viet + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 2, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-35, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.4. Vẽ biểu đồ likert cho dữ liệu Đà Nẵng - Nhãn tiếng anh

# Plot Likert graph for people in Da Nang 
library(tidyverse)
library(compareGroups)
head(DN_SEM1)
##     Res  BPE
## 449   6 PSB1
## 450   6 PSB1
## 451   6 PSB1
## 452   5 PSB1
## 453   6 PSB1
## 454   6 PSB1
dim(DN_SEM1)
## [1] 11620     2
attach(DN_SEM1)
## The following objects are masked from DN_HCM_SEM1:
## 
##     BPE, Res
DN_SEM1 = within(DN_SEM1, {
  Res = factor(Res, labels = c("Very Disagree", "Disagree", "Quite Disagree", "Normal", "Quite Agree", "Agree", "Very Agree"))
  BPE = factor(BPE, labels = c("PSB1", "PSB2", "PSB3", "PSB4", "LOY1", "LOY2", "LOY3", "LOY4", "LOy5", "LOY6", "LOY7", "IMA1", "IMA2", "IMA3", "IMA4", "IMA5", "PHB1", "PHB2", "PHB3", "PHB4", "PHB5", "PEV1", "PEV2", "PEV3", "PEV4", "PVA1", "PVA2", "PVA3"))
  })
str(DN_SEM1)
## 'data.frame':    11620 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "Very Disagree",..: 6 6 6 5 6 6 6 7 4 6 ...
##  $ BPE: Factor w/ 28 levels "PSB1","PSB2",..: 22 22 22 22 22 22 22 22 22 22 ...
summary(DN_SEM1)
##              Res            BPE      
##  Very Disagree :  37   PSB1   : 415  
##  Disagree      : 125   PSB2   : 415  
##  Quite Disagree: 314   PSB3   : 415  
##  Normal        : 790   PSB4   : 415  
##  Quite Agree   :2636   LOY1   : 415  
##  Agree         :5567   LOY2   : 415  
##  Very Agree    :2151   (Other):9130
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_SEM1 %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  # arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
##  [1] PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOy5 LOY6 LOY7 IMA1 IMA2 IMA3 IMA4
## [16] IMA5 PHB1 PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4 PVA1 PVA2 PVA3
## 28 Levels: PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOy5 LOY6 LOY7 IMA1 ... PVA3
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 181 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PSB1  Very Disagree      1    0.24 0.24%   
##  2 PSB1  Disagree           3    0.72 0.72%   
##  3 PSB1  Quite Disagree    11    2.65 2.65%   
##  4 PSB1  Normal            56   13.5  13.49%  
##  5 PSB1  Quite Agree      137   33.0  33.01%  
##  6 PSB1  Agree            174   41.9  41.93%  
##  7 PSB1  Very Agree        33    7.95 7.95%   
##  8 PSB2  Disagree           1    0.24 0.24%   
##  9 PSB2  Quite Disagree     8    1.93 1.93%   
## 10 PSB2  Normal            49   11.8  11.81%  
## # ... with 171 more rows
library(extrafont)
theme_set(theme_minimal())
gg <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "Perception of passengers towards bus systems in Da Nang", 
       subtitle = "Likert scale is a type of rating scale commonly used in surveys. When responding to a Likert type question,\nrespondents simply state their level of agreement or disagreement on a symmetric agree-disagree scale.")
gg
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "Very Disagree") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 16 x 5
##    BPE   Res               n percent bar_text
##    <fct> <fct>         <int>   <dbl> <chr>   
##  1 PSB1  Very Disagree     1    0.24 0.24%   
##  2 PSB4  Very Disagree     1    0.24 0.24%   
##  3 LOy5  Very Disagree     1    0.24 0.24%   
##  4 LOY6  Very Disagree     1    0.24 0.24%   
##  5 LOY7  Very Disagree     1    0.24 0.24%   
##  6 IMA1  Very Disagree     1    0.24 0.24%   
##  7 IMA2  Very Disagree     2    0.48 0.48%   
##  8 IMA3  Very Disagree     2    0.48 0.48%   
##  9 IMA4  Very Disagree     1    0.24 0.24%   
## 10 PHB1  Very Disagree     1    0.24 0.24%   
## 11 PHB5  Very Disagree     1    0.24 0.24%   
## 12 PEV1  Very Disagree     9    2.17 2.17%   
## 13 PEV2  Very Disagree     6    1.45 1.45%   
## 14 PEV3  Very Disagree     4    0.96 0.96%   
## 15 PEV4  Very Disagree     4    0.96 0.96%   
## 16 PVA2  Very Disagree     1    0.24 0.24%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Disagree") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 25 x 5
##    BPE   Res          n percent bar_text
##    <fct> <fct>    <int>   <dbl> <chr>   
##  1 PSB1  Disagree     3    0.72 0.72%   
##  2 PSB2  Disagree     1    0.24 0.24%   
##  3 PSB3  Disagree     1    0.24 0.24%   
##  4 LOY1  Disagree     1    0.24 0.24%   
##  5 LOY2  Disagree     2    0.48 0.48%   
##  6 LOY3  Disagree     1    0.24 0.24%   
##  7 LOY4  Disagree     1    0.24 0.24%   
##  8 LOy5  Disagree     1    0.24 0.24%   
##  9 LOY6  Disagree     3    0.72 0.72%   
## 10 LOY7  Disagree    16    3.86 3.86%   
## # ... with 15 more rows
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Quite Disagree") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 28 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PSB1  Quite Disagree    11    2.65 2.65%   
##  2 PSB2  Quite Disagree     8    1.93 1.93%   
##  3 PSB3  Quite Disagree     4    0.96 0.96%   
##  4 PSB4  Quite Disagree     8    1.93 1.93%   
##  5 LOY1  Quite Disagree     6    1.45 1.45%   
##  6 LOY2  Quite Disagree     7    1.69 1.69%   
##  7 LOY3  Quite Disagree     7    1.69 1.69%   
##  8 LOY4  Quite Disagree     5    1.2  1.2%    
##  9 LOy5  Quite Disagree    18    4.34 4.34%   
## 10 LOY6  Quite Disagree    13    3.13 3.13%   
## # ... with 18 more rows
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "Normal") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 28 x 5
##    BPE   Res        n percent bar_text
##    <fct> <fct>  <int>   <dbl> <chr>   
##  1 PSB1  Normal    56   13.5  13.49%  
##  2 PSB2  Normal    49   11.8  11.81%  
##  3 PSB3  Normal    41    9.88 9.88%   
##  4 PSB4  Normal    26    6.27 6.27%   
##  5 LOY1  Normal    25    6.02 6.02%   
##  6 LOY2  Normal    27    6.51 6.51%   
##  7 LOY3  Normal    24    5.78 5.78%   
##  8 LOY4  Normal    31    7.47 7.47%   
##  9 LOy5  Normal    40    9.64 9.64%   
## 10 LOY6  Normal    33    7.95 7.95%   
## # ... with 18 more rows
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Quite Agree") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 28 x 5
##    BPE   Res             n percent bar_text
##    <fct> <fct>       <int>   <dbl> <chr>   
##  1 PSB1  Quite Agree   137    33.0 33.01%  
##  2 PSB2  Quite Agree   113    27.2 27.23%  
##  3 PSB3  Quite Agree   109    26.3 26.27%  
##  4 PSB4  Quite Agree   104    25.1 25.06%  
##  5 LOY1  Quite Agree    98    23.6 23.61%  
##  6 LOY2  Quite Agree    92    22.2 22.17%  
##  7 LOY3  Quite Agree    88    21.2 21.2%   
##  8 LOY4  Quite Agree    75    18.1 18.07%  
##  9 LOy5  Quite Agree   122    29.4 29.4%   
## 10 LOY6  Quite Agree   115    27.7 27.71%  
## # ... with 18 more rows
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "Agree") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 28 x 5
##    BPE   Res       n percent bar_text
##    <fct> <fct> <int>   <dbl> <chr>   
##  1 PSB1  Agree   174    41.9 41.93%  
##  2 PSB2  Agree   210    50.6 50.6%   
##  3 PSB3  Agree   216    52.0 52.05%  
##  4 PSB4  Agree   223    53.7 53.73%  
##  5 LOY1  Agree   233    56.1 56.14%  
##  6 LOY2  Agree   221    53.2 53.25%  
##  7 LOY3  Agree   224    54.0 53.98%  
##  8 LOY4  Agree   223    53.7 53.73%  
##  9 LOy5  Agree   163    39.3 39.28%  
## 10 LOY6  Agree   170    41.0 40.96%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  Very Agree    33    7.95 7.95%   
##  2 PSB2  Very Agree    34    8.19 8.19%   
##  3 PSB3  Very Agree    44   10.6  10.6%   
##  4 PSB4  Very Agree    53   12.8  12.77%  
##  5 LOY1  Very Agree    52   12.5  12.53%  
##  6 LOY2  Very Agree    66   15.9  15.9%   
##  7 LOY3  Very Agree    71   17.1  17.11%  
##  8 LOY4  Very Agree    80   19.3  19.28%  
##  9 LOy5  Very Agree    70   16.9  16.87%  
## 10 LOY6  Very Agree    80   19.3  19.28%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") -> df_for_text
df_for_text
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  Very Agree    33    7.95 7.95%   
##  2 PSB2  Very Agree    34    8.19 8.19%   
##  3 PSB3  Very Agree    44   10.6  10.6%   
##  4 PSB4  Very Agree    53   12.8  12.77%  
##  5 LOY1  Very Agree    52   12.5  12.53%  
##  6 LOY2  Very Agree    66   15.9  15.9%   
##  7 LOY3  Very Agree    71   17.1  17.11%  
##  8 LOY4  Very Agree    80   19.3  19.28%  
##  9 LOy5  Very Agree    70   16.9  16.87%  
## 10 LOY6  Very Agree    80   19.3  19.28%  
## # ... with 18 more rows
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 2, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-40, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.5. Vẽ biểu đồ likert cho dữ liệu Đà Nẵng - Nhãn tiếng việt

# Nhãm tiếng việt
DN_SEM1_viet = within(DN_SEM1, {
  Res = factor(Res, labels = c("R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD", "B\u00ECnh th\u01B0\u1EDDng", "Kh\u00E1 \u0111\u1ED3ng \u00FD", "\u0110\u1ED3ng \u00FD", "R\u1EA5t \u0111\u1ED3ng \u00FD"))
  BPE = factor(BPE, labels = c("PSB1", "PSB2", "PSB3", "PSB4", "LOY1", "LOY2", "LOY3", "LOY4", "LOy5", "LOY6", "LOY7", "IMA1", "IMA2", "IMA3", "IMA4", "IMA5", "PHB1", "PHB2", "PHB3", "PHB4", "PHB5", "PEV1", "PEV2", "PEV3", "PEV4", "PVA1", "PVA2", "PVA3"))
  })
str(DN_SEM1_viet)
## 'data.frame':    11620 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "R<U+1EA5>t không d<U+1ED3>ng ý",..: 6 6 6 5 6 6 6 7 4 6 ...
##  $ BPE: Factor w/ 28 levels "PSB1","PSB2",..: 22 22 22 22 22 22 22 22 22 22 ...
summary(DN_SEM1_viet)
##                              Res            BPE      
##  R<U+1EA5>t không d<U+1ED3>ng ý:  37   PSB1   : 415  
##  Không d<U+1ED3>ng ý           : 125   PSB2   : 415  
##  Khá không d<U+1ED3>ng ý       : 314   PSB3   : 415  
##  Bình thu<U+1EDD>ng            : 790   PSB4   : 415  
##  Khá d<U+1ED3>ng ý             :2636   LOY1   : 415  
##  Ð<U+1ED3>ng ý                 :5567   LOY2   : 415  
##  R<U+1EA5>t d<U+1ED3>ng ý      :2151   (Other):9130
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_SEM1_viet %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  # arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
##  [1] PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOy5 LOY6 LOY7 IMA1 IMA2 IMA3 IMA4
## [16] IMA5 PHB1 PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4 PVA1 PVA2 PVA3
## 28 Levels: PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOy5 LOY6 LOY7 IMA1 ... PVA3
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 181 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
##  2 PSB1  Không d<U+1ED3>ng ý         3    0.72 0.72%   
##  3 PSB1  Khá không d<U+1ED3>ng ý    11    2.65 2.65%   
##  4 PSB1  Bình thu<U+1EDD>ng         56   13.5  13.49%  
##  5 PSB1  Khá d<U+1ED3>ng ý         137   33.0  33.01%  
##  6 PSB1  Ð<U+1ED3>ng ý             174   41.9  41.93%  
##  7 PSB1  R<U+1EA5>t d<U+1ED3>ng ý          33    7.95 7.95%   
##  8 PSB2  Không d<U+1ED3>ng ý         1    0.24 0.24%   
##  9 PSB2  Khá không d<U+1ED3>ng ý     8    1.93 1.93%   
## 10 PSB2  Bình thu<U+1EDD>ng         49   11.8  11.81%  
## # ... with 171 more rows
library(extrafont)
theme_set(theme_minimal())
gg_viet <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "C\u1EA3m nh\u1EADn c\u1EE7a h\u00E0nh kh\u00E1ch v\u1EC1 h\u1EC7 th\u1ED1ng xe bu\u00FDt \u0111\u00F4 th\u1ECB \u0110\u00E0 N\u1EB5ng", 
       subtitle = "Thang \u0111o Likert l\u00E0 lo\u1EA1i thang \u0111o \u0111i\u1EC3m \u0111\u00E1nh gi\u00E1 \u0111\u01B0\u1EE3c s\u1EED d\u1EE5ng ph\u1ED5 bi\u1EBFn trong kh\u1EA3o s\u00E1t. Nghi\u00EAn c\u1EE9u s\u1EED d\u1EE5ng thang \u0111o 7 m\u1EE9c \u0111\u1ED9 t\u01B0\u01A1ng \u1EE9ng v\u1EDBi 1 - R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD \u0111\u1EBFn 7 - R\u1EA5t \u0111\u1ED3ng \u00FD.")
gg_viet
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 16 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
##  2 PSB4  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
##  3 LOy5  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
##  4 LOY6  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
##  5 LOY7  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
##  6 IMA1  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
##  7 IMA2  R<U+1EA5>t không d<U+1ED3>ng ý     2    0.48 0.48%   
##  8 IMA3  R<U+1EA5>t không d<U+1ED3>ng ý     2    0.48 0.48%   
##  9 IMA4  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
## 10 PHB1  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
## 11 PHB5  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%   
## 12 PEV1  R<U+1EA5>t không d<U+1ED3>ng ý     9    2.17 2.17%   
## 13 PEV2  R<U+1EA5>t không d<U+1ED3>ng ý     6    1.45 1.45%   
## 14 PEV3  R<U+1EA5>t không d<U+1ED3>ng ý     4    0.96 0.96%   
## 15 PEV4  R<U+1EA5>t không d<U+1ED3>ng ý     4    0.96 0.96%   
## 16 PVA2  R<U+1EA5>t không d<U+1ED3>ng ý     1    0.24 0.24%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 25 x 5
##    BPE   Res              n percent bar_text
##    <fct> <fct>        <int>   <dbl> <chr>   
##  1 PSB1  Không d<U+1ED3>ng ý     3    0.72 0.72%   
##  2 PSB2  Không d<U+1ED3>ng ý     1    0.24 0.24%   
##  3 PSB3  Không d<U+1ED3>ng ý     1    0.24 0.24%   
##  4 LOY1  Không d<U+1ED3>ng ý     1    0.24 0.24%   
##  5 LOY2  Không d<U+1ED3>ng ý     2    0.48 0.48%   
##  6 LOY3  Không d<U+1ED3>ng ý     1    0.24 0.24%   
##  7 LOY4  Không d<U+1ED3>ng ý     1    0.24 0.24%   
##  8 LOy5  Không d<U+1ED3>ng ý     1    0.24 0.24%   
##  9 LOY6  Không d<U+1ED3>ng ý     3    0.72 0.72%   
## 10 LOY7  Không d<U+1ED3>ng ý    16    3.86 3.86%   
## # ... with 15 more rows
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 28 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  Khá không d<U+1ED3>ng ý    11    2.65 2.65%   
##  2 PSB2  Khá không d<U+1ED3>ng ý     8    1.93 1.93%   
##  3 PSB3  Khá không d<U+1ED3>ng ý     4    0.96 0.96%   
##  4 PSB4  Khá không d<U+1ED3>ng ý     8    1.93 1.93%   
##  5 LOY1  Khá không d<U+1ED3>ng ý     6    1.45 1.45%   
##  6 LOY2  Khá không d<U+1ED3>ng ý     7    1.69 1.69%   
##  7 LOY3  Khá không d<U+1ED3>ng ý     7    1.69 1.69%   
##  8 LOY4  Khá không d<U+1ED3>ng ý     5    1.2  1.2%    
##  9 LOy5  Khá không d<U+1ED3>ng ý    18    4.34 4.34%   
## 10 LOY6  Khá không d<U+1ED3>ng ý    13    3.13 3.13%   
## # ... with 18 more rows
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "B\u00ECnh th\u01B0\u1EDDng") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 28 x 5
##    BPE   Res             n percent bar_text
##    <fct> <fct>       <int>   <dbl> <chr>   
##  1 PSB1  Bình thu<U+1EDD>ng    56   13.5  13.49%  
##  2 PSB2  Bình thu<U+1EDD>ng    49   11.8  11.81%  
##  3 PSB3  Bình thu<U+1EDD>ng    41    9.88 9.88%   
##  4 PSB4  Bình thu<U+1EDD>ng    26    6.27 6.27%   
##  5 LOY1  Bình thu<U+1EDD>ng    25    6.02 6.02%   
##  6 LOY2  Bình thu<U+1EDD>ng    27    6.51 6.51%   
##  7 LOY3  Bình thu<U+1EDD>ng    24    5.78 5.78%   
##  8 LOY4  Bình thu<U+1EDD>ng    31    7.47 7.47%   
##  9 LOy5  Bình thu<U+1EDD>ng    40    9.64 9.64%   
## 10 LOY6  Bình thu<U+1EDD>ng    33    7.95 7.95%   
## # ... with 18 more rows
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  Khá d<U+1ED3>ng ý   137    33.0 33.01%  
##  2 PSB2  Khá d<U+1ED3>ng ý   113    27.2 27.23%  
##  3 PSB3  Khá d<U+1ED3>ng ý   109    26.3 26.27%  
##  4 PSB4  Khá d<U+1ED3>ng ý   104    25.1 25.06%  
##  5 LOY1  Khá d<U+1ED3>ng ý    98    23.6 23.61%  
##  6 LOY2  Khá d<U+1ED3>ng ý    92    22.2 22.17%  
##  7 LOY3  Khá d<U+1ED3>ng ý    88    21.2 21.2%   
##  8 LOY4  Khá d<U+1ED3>ng ý    75    18.1 18.07%  
##  9 LOy5  Khá d<U+1ED3>ng ý   122    29.4 29.4%   
## 10 LOY6  Khá d<U+1ED3>ng ý   115    27.7 27.71%  
## # ... with 18 more rows
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "\u0110\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 28 x 5
##    BPE   Res        n percent bar_text
##    <fct> <fct>  <int>   <dbl> <chr>   
##  1 PSB1  Ð<U+1ED3>ng ý   174    41.9 41.93%  
##  2 PSB2  Ð<U+1ED3>ng ý   210    50.6 50.6%   
##  3 PSB3  Ð<U+1ED3>ng ý   216    52.0 52.05%  
##  4 PSB4  Ð<U+1ED3>ng ý   223    53.7 53.73%  
##  5 LOY1  Ð<U+1ED3>ng ý   233    56.1 56.14%  
##  6 LOY2  Ð<U+1ED3>ng ý   221    53.2 53.25%  
##  7 LOY3  Ð<U+1ED3>ng ý   224    54.0 53.98%  
##  8 LOY4  Ð<U+1ED3>ng ý   223    53.7 53.73%  
##  9 LOy5  Ð<U+1ED3>ng ý   163    39.3 39.28%  
## 10 LOY6  Ð<U+1ED3>ng ý   170    41.0 40.96%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t d<U+1ED3>ng ý    33    7.95 7.95%   
##  2 PSB2  R<U+1EA5>t d<U+1ED3>ng ý    34    8.19 8.19%   
##  3 PSB3  R<U+1EA5>t d<U+1ED3>ng ý    44   10.6  10.6%   
##  4 PSB4  R<U+1EA5>t d<U+1ED3>ng ý    53   12.8  12.77%  
##  5 LOY1  R<U+1EA5>t d<U+1ED3>ng ý    52   12.5  12.53%  
##  6 LOY2  R<U+1EA5>t d<U+1ED3>ng ý    66   15.9  15.9%   
##  7 LOY3  R<U+1EA5>t d<U+1ED3>ng ý    71   17.1  17.11%  
##  8 LOY4  R<U+1EA5>t d<U+1ED3>ng ý    80   19.3  19.28%  
##  9 LOy5  R<U+1EA5>t d<U+1ED3>ng ý    70   16.9  16.87%  
## 10 LOY6  R<U+1EA5>t d<U+1ED3>ng ý    80   19.3  19.28%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") -> df_for_text
df_for_text
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t d<U+1ED3>ng ý    33    7.95 7.95%   
##  2 PSB2  R<U+1EA5>t d<U+1ED3>ng ý    34    8.19 8.19%   
##  3 PSB3  R<U+1EA5>t d<U+1ED3>ng ý    44   10.6  10.6%   
##  4 PSB4  R<U+1EA5>t d<U+1ED3>ng ý    53   12.8  12.77%  
##  5 LOY1  R<U+1EA5>t d<U+1ED3>ng ý    52   12.5  12.53%  
##  6 LOY2  R<U+1EA5>t d<U+1ED3>ng ý    66   15.9  15.9%   
##  7 LOY3  R<U+1EA5>t d<U+1ED3>ng ý    71   17.1  17.11%  
##  8 LOY4  R<U+1EA5>t d<U+1ED3>ng ý    80   19.3  19.28%  
##  9 LOy5  R<U+1EA5>t d<U+1ED3>ng ý    70   16.9  16.87%  
## 10 LOY6  R<U+1EA5>t d<U+1ED3>ng ý    80   19.3  19.28%  
## # ... with 18 more rows
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg_viet + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 2, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-40, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.6. Vẽ biểu đồ likert cho dữ liệu Hồ Chí Minh-Nhãn tiếng anh

# Plot Likert graph for people in Ho Chi Minh 
library(tidyverse)
library(compareGroups)
head(HCM_SEM1)
##   Res  BPE
## 1   6 PSB1
## 2   3 PSB1
## 3   2 PSB1
## 4   5 PSB1
## 5   2 PSB1
## 6   2 PSB1
dim(HCM_SEM1)
## [1] 12544     2
attach(HCM_SEM1)
## The following objects are masked from DN_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1:
## 
##     BPE, Res
HCM_SEM1 = within(HCM_SEM1, {
  Res = factor(Res, labels = c("Very Disagree", "Disagree", "Quite Disagree", "Normal", "Quite Agree", "Agree", "Very Agree"))
  BPE = factor(BPE, labels = c("PSB1", "PSB2", "PSB3", "PSB4", "LOY1", "LOY2", "LOY3", "LOY4", "LOY5", "LOY6", "LOY7", "IMA1", "IMA2", "IMA3", "IMA4", "IMA5", "PHB1", "PHB2", "PHB3", "PHB4", "PHB5", "PEV1", "PEV2", "PEV3", "PEV4", "PVA1", "PVA2", "PVA3"))
  })
str(HCM_SEM1)
## 'data.frame':    12544 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "Very Disagree",..: 6 3 2 5 2 2 1 6 2 4 ...
##  $ BPE: Factor w/ 28 levels "PSB1","PSB2",..: 22 22 22 22 22 22 22 22 22 22 ...
summary(HCM_SEM1)
##              Res            BPE      
##  Very Disagree : 423   PSB1   : 448  
##  Disagree      : 621   PSB2   : 448  
##  Quite Disagree: 454   PSB3   : 448  
##  Normal        :1929   PSB4   : 448  
##  Quite Agree   :2168   LOY1   : 448  
##  Agree         :5164   LOY2   : 448  
##  Very Agree    :1785   (Other):9856
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
HCM_SEM1 %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  # arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
##  [1] PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 IMA2 IMA3 IMA4
## [16] IMA5 PHB1 PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4 PVA1 PVA2 PVA3
## 28 Levels: PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 ... PVA3
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 196 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PSB1  Very Disagree      5    1.12 1.12%   
##  2 PSB1  Disagree          17    3.79 3.79%   
##  3 PSB1  Quite Disagree    21    4.69 4.69%   
##  4 PSB1  Normal           100   22.3  22.32%  
##  5 PSB1  Quite Agree       93   20.8  20.76%  
##  6 PSB1  Agree            180   40.2  40.18%  
##  7 PSB1  Very Agree        32    7.14 7.14%   
##  8 PSB2  Very Disagree     11    2.46 2.46%   
##  9 PSB2  Disagree          18    4.02 4.02%   
## 10 PSB2  Quite Disagree    18    4.02 4.02%   
## # ... with 186 more rows
library(extrafont)
theme_set(theme_minimal())
gg <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "Perception of passengers towards bus systems in Ho Chi Minh", 
       subtitle = "Likert scale is a type of rating scale commonly used in surveys. When responding to a Likert type question,\nrespondents simply state their level of agreement or disagreement on a symmetric agree-disagree scale.")
gg
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "Very Disagree") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 28 x 5
##    BPE   Res               n percent bar_text
##    <fct> <fct>         <int>   <dbl> <chr>   
##  1 PSB1  Very Disagree     5    1.12 1.12%   
##  2 PSB2  Very Disagree    11    2.46 2.46%   
##  3 PSB3  Very Disagree    11    2.46 2.46%   
##  4 PSB4  Very Disagree    13    2.9  2.9%    
##  5 LOY1  Very Disagree     6    1.34 1.34%   
##  6 LOY2  Very Disagree     4    0.89 0.89%   
##  7 LOY3  Very Disagree     6    1.34 1.34%   
##  8 LOY4  Very Disagree     3    0.67 0.67%   
##  9 LOY5  Very Disagree    12    2.68 2.68%   
## 10 LOY6  Very Disagree    11    2.46 2.46%   
## # ... with 18 more rows
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Disagree") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 28 x 5
##    BPE   Res          n percent bar_text
##    <fct> <fct>    <int>   <dbl> <chr>   
##  1 PSB1  Disagree    17    3.79 3.79%   
##  2 PSB2  Disagree    18    4.02 4.02%   
##  3 PSB3  Disagree    10    2.23 2.23%   
##  4 PSB4  Disagree    14    3.12 3.12%   
##  5 LOY1  Disagree    20    4.46 4.46%   
##  6 LOY2  Disagree    16    3.57 3.57%   
##  7 LOY3  Disagree    21    4.69 4.69%   
##  8 LOY4  Disagree    15    3.35 3.35%   
##  9 LOY5  Disagree    17    3.79 3.79%   
## 10 LOY6  Disagree    21    4.69 4.69%   
## # ... with 18 more rows
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Quite Disagree") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 28 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PSB1  Quite Disagree    21    4.69 4.69%   
##  2 PSB2  Quite Disagree    18    4.02 4.02%   
##  3 PSB3  Quite Disagree    16    3.57 3.57%   
##  4 PSB4  Quite Disagree    18    4.02 4.02%   
##  5 LOY1  Quite Disagree    15    3.35 3.35%   
##  6 LOY2  Quite Disagree    13    2.9  2.9%    
##  7 LOY3  Quite Disagree    10    2.23 2.23%   
##  8 LOY4  Quite Disagree    14    3.12 3.12%   
##  9 LOY5  Quite Disagree    14    3.12 3.12%   
## 10 LOY6  Quite Disagree    13    2.9  2.9%    
## # ... with 18 more rows
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "Normal") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 28 x 5
##    BPE   Res        n percent bar_text
##    <fct> <fct>  <int>   <dbl> <chr>   
##  1 PSB1  Normal   100    22.3 22.32%  
##  2 PSB2  Normal   111    24.8 24.78%  
##  3 PSB3  Normal    89    19.9 19.87%  
##  4 PSB4  Normal    88    19.6 19.64%  
##  5 LOY1  Normal    81    18.1 18.08%  
##  6 LOY2  Normal    87    19.4 19.42%  
##  7 LOY3  Normal    75    16.7 16.74%  
##  8 LOY4  Normal    61    13.6 13.62%  
##  9 LOY5  Normal    90    20.1 20.09%  
## 10 LOY6  Normal    78    17.4 17.41%  
## # ... with 18 more rows
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Quite Agree") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 28 x 5
##    BPE   Res             n percent bar_text
##    <fct> <fct>       <int>   <dbl> <chr>   
##  1 PSB1  Quite Agree    93    20.8 20.76%  
##  2 PSB2  Quite Agree   100    22.3 22.32%  
##  3 PSB3  Quite Agree   100    22.3 22.32%  
##  4 PSB4  Quite Agree    97    21.6 21.65%  
##  5 LOY1  Quite Agree   107    23.9 23.88%  
##  6 LOY2  Quite Agree    76    17.0 16.96%  
##  7 LOY3  Quite Agree    83    18.5 18.53%  
##  8 LOY4  Quite Agree    86    19.2 19.2%   
##  9 LOY5  Quite Agree    90    20.1 20.09%  
## 10 LOY6  Quite Agree    99    22.1 22.1%   
## # ... with 18 more rows
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "Agree") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 28 x 5
##    BPE   Res       n percent bar_text
##    <fct> <fct> <int>   <dbl> <chr>   
##  1 PSB1  Agree   180    40.2 40.18%  
##  2 PSB2  Agree   158    35.3 35.27%  
##  3 PSB3  Agree   187    41.7 41.74%  
##  4 PSB4  Agree   185    41.3 41.29%  
##  5 LOY1  Agree   187    41.7 41.74%  
##  6 LOY2  Agree   212    47.3 47.32%  
##  7 LOY3  Agree   206    46.0 45.98%  
##  8 LOY4  Agree   217    48.4 48.44%  
##  9 LOY5  Agree   177    39.5 39.51%  
## 10 LOY6  Agree   177    39.5 39.51%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  Very Agree    32    7.14 7.14%   
##  2 PSB2  Very Agree    32    7.14 7.14%   
##  3 PSB3  Very Agree    35    7.81 7.81%   
##  4 PSB4  Very Agree    33    7.37 7.37%   
##  5 LOY1  Very Agree    32    7.14 7.14%   
##  6 LOY2  Very Agree    40    8.93 8.93%   
##  7 LOY3  Very Agree    47   10.5  10.49%  
##  8 LOY4  Very Agree    52   11.6  11.61%  
##  9 LOY5  Very Agree    48   10.7  10.71%  
## 10 LOY6  Very Agree    49   10.9  10.94%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") -> df_for_text
df_for_text
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  Very Agree    32    7.14 7.14%   
##  2 PSB2  Very Agree    32    7.14 7.14%   
##  3 PSB3  Very Agree    35    7.81 7.81%   
##  4 PSB4  Very Agree    33    7.37 7.37%   
##  5 LOY1  Very Agree    32    7.14 7.14%   
##  6 LOY2  Very Agree    40    8.93 8.93%   
##  7 LOY3  Very Agree    47   10.5  10.49%  
##  8 LOY4  Very Agree    52   11.6  11.61%  
##  9 LOY5  Very Agree    48   10.7  10.71%  
## 10 LOY6  Very Agree    49   10.9  10.94%  
## # ... with 18 more rows
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 4, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-30, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-2, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.7. Vẽ biểu đồ likert cho dữ liệu Hồ Chí Minh-Nhãn tiếng việt

HCM_SEM1_viet = within(HCM_SEM1, {
  Res = factor(Res, labels = c("R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD", "B\u00ECnh th\u01B0\u1EDDng", "Kh\u00E1 \u0111\u1ED3ng \u00FD", "\u0110\u1ED3ng \u00FD", "R\u1EA5t \u0111\u1ED3ng \u00FD"))
  BPE = factor(BPE, labels = c("PSB1", "PSB2", "PSB3", "PSB4", "LOY1", "LOY2", "LOY3", "LOY4", "LOY5", "LOY6", "LOY7", "IMA1", "IMA2", "IMA3", "IMA4", "IMA5", "PHB1", "PHB2", "PHB3", "PHB4", "PHB5", "PEV1", "PEV2", "PEV3", "PEV4", "PVA1", "PVA2", "PVA3"))
  })
str(HCM_SEM1_viet)
## 'data.frame':    12544 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "R<U+1EA5>t không d<U+1ED3>ng ý",..: 6 3 2 5 2 2 1 6 2 4 ...
##  $ BPE: Factor w/ 28 levels "PSB1","PSB2",..: 22 22 22 22 22 22 22 22 22 22 ...
summary(HCM_SEM1_viet)
##                              Res            BPE      
##  R<U+1EA5>t không d<U+1ED3>ng ý: 423   PSB1   : 448  
##  Không d<U+1ED3>ng ý           : 621   PSB2   : 448  
##  Khá không d<U+1ED3>ng ý       : 454   PSB3   : 448  
##  Bình thu<U+1EDD>ng            :1929   PSB4   : 448  
##  Khá d<U+1ED3>ng ý             :2168   LOY1   : 448  
##  Ð<U+1ED3>ng ý                 :5164   LOY2   : 448  
##  R<U+1EA5>t d<U+1ED3>ng ý      :1785   (Other):9856
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
HCM_SEM1_viet %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  # arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
##  [1] PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 IMA2 IMA3 IMA4
## [16] IMA5 PHB1 PHB2 PHB3 PHB4 PHB5 PEV1 PEV2 PEV3 PEV4 PVA1 PVA2 PVA3
## 28 Levels: PSB1 PSB2 PSB3 PSB4 LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7 IMA1 ... PVA3
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 196 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t không d<U+1ED3>ng ý     5    1.12 1.12%   
##  2 PSB1  Không d<U+1ED3>ng ý        17    3.79 3.79%   
##  3 PSB1  Khá không d<U+1ED3>ng ý    21    4.69 4.69%   
##  4 PSB1  Bình thu<U+1EDD>ng        100   22.3  22.32%  
##  5 PSB1  Khá d<U+1ED3>ng ý          93   20.8  20.76%  
##  6 PSB1  Ð<U+1ED3>ng ý             180   40.2  40.18%  
##  7 PSB1  R<U+1EA5>t d<U+1ED3>ng ý          32    7.14 7.14%   
##  8 PSB2  R<U+1EA5>t không d<U+1ED3>ng ý    11    2.46 2.46%   
##  9 PSB2  Không d<U+1ED3>ng ý        18    4.02 4.02%   
## 10 PSB2  Khá không d<U+1ED3>ng ý    18    4.02 4.02%   
## # ... with 186 more rows
library(extrafont)
theme_set(theme_minimal())
gg_viet <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "C\u1EA3m nh\u1EADn c\u1EE7a h\u00E0nh kh\u00E1ch v\u1EC1 h\u1EC7 th\u1ED1ng xe bu\u00FDt \u0111\u00F4 th\u1ECB Th\u00E0nh ph\u1ED1 H\u1ED3 Ch\u00ED Minh", 
       subtitle = "Thang \u0111o Likert l\u00E0 lo\u1EA1i thang \u0111o \u0111i\u1EC3m \u0111\u00E1nh gi\u00E1 \u0111\u01B0\u1EE3c s\u1EED d\u1EE5ng ph\u1ED5 bi\u1EBFn trong kh\u1EA3o s\u00E1t. Nghi\u00EAn c\u1EE9u s\u1EED d\u1EE5ng thang \u0111o 7 m\u1EE9c \u0111\u1ED9 t\u01B0\u01A1ng \u1EE9ng v\u1EDBi 1 - R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD \u0111\u1EBFn 7 - R\u1EA5t \u0111\u1ED3ng \u00FD.")
gg_viet
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 28 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t không d<U+1ED3>ng ý     5    1.12 1.12%   
##  2 PSB2  R<U+1EA5>t không d<U+1ED3>ng ý    11    2.46 2.46%   
##  3 PSB3  R<U+1EA5>t không d<U+1ED3>ng ý    11    2.46 2.46%   
##  4 PSB4  R<U+1EA5>t không d<U+1ED3>ng ý    13    2.9  2.9%    
##  5 LOY1  R<U+1EA5>t không d<U+1ED3>ng ý     6    1.34 1.34%   
##  6 LOY2  R<U+1EA5>t không d<U+1ED3>ng ý     4    0.89 0.89%   
##  7 LOY3  R<U+1EA5>t không d<U+1ED3>ng ý     6    1.34 1.34%   
##  8 LOY4  R<U+1EA5>t không d<U+1ED3>ng ý     3    0.67 0.67%   
##  9 LOY5  R<U+1EA5>t không d<U+1ED3>ng ý    12    2.68 2.68%   
## 10 LOY6  R<U+1EA5>t không d<U+1ED3>ng ý    11    2.46 2.46%   
## # ... with 18 more rows
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 28 x 5
##    BPE   Res              n percent bar_text
##    <fct> <fct>        <int>   <dbl> <chr>   
##  1 PSB1  Không d<U+1ED3>ng ý    17    3.79 3.79%   
##  2 PSB2  Không d<U+1ED3>ng ý    18    4.02 4.02%   
##  3 PSB3  Không d<U+1ED3>ng ý    10    2.23 2.23%   
##  4 PSB4  Không d<U+1ED3>ng ý    14    3.12 3.12%   
##  5 LOY1  Không d<U+1ED3>ng ý    20    4.46 4.46%   
##  6 LOY2  Không d<U+1ED3>ng ý    16    3.57 3.57%   
##  7 LOY3  Không d<U+1ED3>ng ý    21    4.69 4.69%   
##  8 LOY4  Không d<U+1ED3>ng ý    15    3.35 3.35%   
##  9 LOY5  Không d<U+1ED3>ng ý    17    3.79 3.79%   
## 10 LOY6  Không d<U+1ED3>ng ý    21    4.69 4.69%   
## # ... with 18 more rows
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 28 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  Khá không d<U+1ED3>ng ý    21    4.69 4.69%   
##  2 PSB2  Khá không d<U+1ED3>ng ý    18    4.02 4.02%   
##  3 PSB3  Khá không d<U+1ED3>ng ý    16    3.57 3.57%   
##  4 PSB4  Khá không d<U+1ED3>ng ý    18    4.02 4.02%   
##  5 LOY1  Khá không d<U+1ED3>ng ý    15    3.35 3.35%   
##  6 LOY2  Khá không d<U+1ED3>ng ý    13    2.9  2.9%    
##  7 LOY3  Khá không d<U+1ED3>ng ý    10    2.23 2.23%   
##  8 LOY4  Khá không d<U+1ED3>ng ý    14    3.12 3.12%   
##  9 LOY5  Khá không d<U+1ED3>ng ý    14    3.12 3.12%   
## 10 LOY6  Khá không d<U+1ED3>ng ý    13    2.9  2.9%    
## # ... with 18 more rows
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "B\u00ECnh th\u01B0\u1EDDng") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 28 x 5
##    BPE   Res             n percent bar_text
##    <fct> <fct>       <int>   <dbl> <chr>   
##  1 PSB1  Bình thu<U+1EDD>ng   100    22.3 22.32%  
##  2 PSB2  Bình thu<U+1EDD>ng   111    24.8 24.78%  
##  3 PSB3  Bình thu<U+1EDD>ng    89    19.9 19.87%  
##  4 PSB4  Bình thu<U+1EDD>ng    88    19.6 19.64%  
##  5 LOY1  Bình thu<U+1EDD>ng    81    18.1 18.08%  
##  6 LOY2  Bình thu<U+1EDD>ng    87    19.4 19.42%  
##  7 LOY3  Bình thu<U+1EDD>ng    75    16.7 16.74%  
##  8 LOY4  Bình thu<U+1EDD>ng    61    13.6 13.62%  
##  9 LOY5  Bình thu<U+1EDD>ng    90    20.1 20.09%  
## 10 LOY6  Bình thu<U+1EDD>ng    78    17.4 17.41%  
## # ... with 18 more rows
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  Khá d<U+1ED3>ng ý    93    20.8 20.76%  
##  2 PSB2  Khá d<U+1ED3>ng ý   100    22.3 22.32%  
##  3 PSB3  Khá d<U+1ED3>ng ý   100    22.3 22.32%  
##  4 PSB4  Khá d<U+1ED3>ng ý    97    21.6 21.65%  
##  5 LOY1  Khá d<U+1ED3>ng ý   107    23.9 23.88%  
##  6 LOY2  Khá d<U+1ED3>ng ý    76    17.0 16.96%  
##  7 LOY3  Khá d<U+1ED3>ng ý    83    18.5 18.53%  
##  8 LOY4  Khá d<U+1ED3>ng ý    86    19.2 19.2%   
##  9 LOY5  Khá d<U+1ED3>ng ý    90    20.1 20.09%  
## 10 LOY6  Khá d<U+1ED3>ng ý    99    22.1 22.1%   
## # ... with 18 more rows
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "\u0110\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 28 x 5
##    BPE   Res        n percent bar_text
##    <fct> <fct>  <int>   <dbl> <chr>   
##  1 PSB1  Ð<U+1ED3>ng ý   180    40.2 40.18%  
##  2 PSB2  Ð<U+1ED3>ng ý   158    35.3 35.27%  
##  3 PSB3  Ð<U+1ED3>ng ý   187    41.7 41.74%  
##  4 PSB4  Ð<U+1ED3>ng ý   185    41.3 41.29%  
##  5 LOY1  Ð<U+1ED3>ng ý   187    41.7 41.74%  
##  6 LOY2  Ð<U+1ED3>ng ý   212    47.3 47.32%  
##  7 LOY3  Ð<U+1ED3>ng ý   206    46.0 45.98%  
##  8 LOY4  Ð<U+1ED3>ng ý   217    48.4 48.44%  
##  9 LOY5  Ð<U+1ED3>ng ý   177    39.5 39.51%  
## 10 LOY6  Ð<U+1ED3>ng ý   177    39.5 39.51%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t d<U+1ED3>ng ý    32    7.14 7.14%   
##  2 PSB2  R<U+1EA5>t d<U+1ED3>ng ý    32    7.14 7.14%   
##  3 PSB3  R<U+1EA5>t d<U+1ED3>ng ý    35    7.81 7.81%   
##  4 PSB4  R<U+1EA5>t d<U+1ED3>ng ý    33    7.37 7.37%   
##  5 LOY1  R<U+1EA5>t d<U+1ED3>ng ý    32    7.14 7.14%   
##  6 LOY2  R<U+1EA5>t d<U+1ED3>ng ý    40    8.93 8.93%   
##  7 LOY3  R<U+1EA5>t d<U+1ED3>ng ý    47   10.5  10.49%  
##  8 LOY4  R<U+1EA5>t d<U+1ED3>ng ý    52   11.6  11.61%  
##  9 LOY5  R<U+1EA5>t d<U+1ED3>ng ý    48   10.7  10.71%  
## 10 LOY6  R<U+1EA5>t d<U+1ED3>ng ý    49   10.9  10.94%  
## # ... with 18 more rows
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") -> df_for_text
df_for_text
## # A tibble: 28 x 5
##    BPE   Res            n percent bar_text
##    <fct> <fct>      <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t d<U+1ED3>ng ý    32    7.14 7.14%   
##  2 PSB2  R<U+1EA5>t d<U+1ED3>ng ý    32    7.14 7.14%   
##  3 PSB3  R<U+1EA5>t d<U+1ED3>ng ý    35    7.81 7.81%   
##  4 PSB4  R<U+1EA5>t d<U+1ED3>ng ý    33    7.37 7.37%   
##  5 LOY1  R<U+1EA5>t d<U+1ED3>ng ý    32    7.14 7.14%   
##  6 LOY2  R<U+1EA5>t d<U+1ED3>ng ý    40    8.93 8.93%   
##  7 LOY3  R<U+1EA5>t d<U+1ED3>ng ý    47   10.5  10.49%  
##  8 LOY4  R<U+1EA5>t d<U+1ED3>ng ý    52   11.6  11.61%  
##  9 LOY5  R<U+1EA5>t d<U+1ED3>ng ý    48   10.7  10.71%  
## 10 LOY6  R<U+1EA5>t d<U+1ED3>ng ý    49   10.9  10.94%  
## # ... with 18 more rows
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg_viet + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 4, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-30, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-2, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8. Vẽ biểu đồ likert cho dữ liệu của 2 thành phố theo từng constructs 3.8.1. Construct PSA - Nhãn tiếng anh

# Plot Likert graph for people in both cities for construct PSA
library(tidyverse)
library(compareGroups)
head(DN_HCM_SEM1_PSB)
##   Res  BPE
## 1   6 PSB1
## 2   3 PSB1
## 3   2 PSB1
## 4   5 PSB1
## 5   2 PSB1
## 6   2 PSB1
dim(DN_HCM_SEM1_PSB)
## [1] 3452    2
attach(DN_HCM_SEM1_PSB)
## The following objects are masked from HCM_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1:
## 
##     BPE, Res
DN_HCM_SEM1_PSB = within(DN_HCM_SEM1_PSB, {
  Res = factor(Res, labels = c("Very Disagree", "Disagree", "Quite Disagree", "Normal", "Quite Agree", "Agree", "Very Agree"))
  BPE = factor(BPE, labels = c("PSB1", "PSB2", "PSB3", "PSB4"))
  })
str(DN_HCM_SEM1_PSB)
## 'data.frame':    3452 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "Very Disagree",..: 6 3 2 5 2 2 1 6 2 4 ...
##  $ BPE: Factor w/ 4 levels "PSB1","PSB2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_PSB)
##              Res         BPE     
##  Very Disagree : 146   PSB1:863  
##  Disagree      : 296   PSB2:863  
##  Quite Disagree: 190   PSB3:863  
##  Normal        : 381   PSB4:863  
##  Quite Agree   : 563             
##  Agree         :1518             
##  Very Agree    : 358
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_PSB %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] PSB4 PSB1 PSB2 PSB3
## Levels: PSB1 PSB2 PSB3 PSB4
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 28 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PSB1  Very Disagree     59    6.84 6.84%   
##  2 PSB1  Disagree         129   15.0  14.95%  
##  3 PSB1  Quite Disagree    53    6.14 6.14%   
##  4 PSB1  Normal            80    9.27 9.27%   
##  5 PSB1  Quite Agree      127   14.7  14.72%  
##  6 PSB1  Agree            332   38.5  38.47%  
##  7 PSB1  Very Agree        83    9.62 9.62%   
##  8 PSB2  Very Disagree     37    4.29 4.29%   
##  9 PSB2  Disagree          65    7.53 7.53%   
## 10 PSB2  Quite Disagree    51    5.91 5.91%   
## # ... with 18 more rows
library(extrafont)
theme_set(theme_minimal())
gg <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "Perceived safety (PSA) on bus of passengers in urban areas of Viet Nam", 
       subtitle = "Likert scale is a type of rating scale commonly used in surveys. When responding to a Likert type question,\nrespondents simply state their level of agreement or disagreement on a symmetric agree-disagree scale.")
gg
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "Very Disagree") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 4 x 5
##   BPE   Res               n percent bar_text
##   <fct> <fct>         <int>   <dbl> <chr>   
## 1 PSB1  Very Disagree    59    6.84 6.84%   
## 2 PSB2  Very Disagree    37    4.29 4.29%   
## 3 PSB3  Very Disagree    18    2.09 2.09%   
## 4 PSB4  Very Disagree    32    3.71 3.71%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Disagree") %>% 
  filter(percent >= 3) -> df_for_text2
df_for_text2
## # A tibble: 3 x 5
##   BPE   Res          n percent bar_text
##   <fct> <fct>    <int>   <dbl> <chr>   
## 1 PSB1  Disagree   129   15.0  14.95%  
## 2 PSB2  Disagree    65    7.53 7.53%   
## 3 PSB4  Disagree    79    9.15 9.15%
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Quite Disagree") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 4 x 5
##   BPE   Res                n percent bar_text
##   <fct> <fct>          <int>   <dbl> <chr>   
## 1 PSB1  Quite Disagree    53    6.14 6.14%   
## 2 PSB2  Quite Disagree    51    5.91 5.91%   
## 3 PSB3  Quite Disagree    29    3.36 3.36%   
## 4 PSB4  Quite Disagree    57    6.6  6.6%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "Normal") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 4 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 PSB1  Normal    80    9.27 9.27%   
## 2 PSB2  Normal    87   10.1  10.08%  
## 3 PSB3  Normal    66    7.65 7.65%   
## 4 PSB4  Normal   148   17.2  17.15%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Quite Agree") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 4 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 PSB1  Quite Agree   127    14.7 14.72%  
## 2 PSB2  Quite Agree   127    14.7 14.72%  
## 3 PSB3  Quite Agree   148    17.2 17.15%  
## 4 PSB4  Quite Agree   161    18.7 18.66%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "Agree") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 4 x 5
##   BPE   Res       n percent bar_text
##   <fct> <fct> <int>   <dbl> <chr>   
## 1 PSB1  Agree   332    38.5 38.47%  
## 2 PSB2  Agree   402    46.6 46.58%  
## 3 PSB3  Agree   475    55.0 55.04%  
## 4 PSB4  Agree   309    35.8 35.81%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") %>% 
  filter(percent >= 0) -> df_for_text7 # Chỉ thể hiện giá trị có % lựa chọn >0
df_for_text7
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PSB1  Very Agree    83    9.62 9.62%   
## 2 PSB2  Very Agree    94   10.9  10.89%  
## 3 PSB3  Very Agree   104   12.0  12.05%  
## 4 PSB4  Very Agree    77    8.92 8.92%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") -> df_for_text
df_for_text
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PSB1  Very Agree    83    9.62 9.62%   
## 2 PSB2  Very Agree    94   10.9  10.89%  
## 3 PSB3  Very Agree   104   12.0  12.05%  
## 4 PSB4  Very Agree    77    8.92 8.92%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 2, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-30, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-2, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.2. Construct PSA - Nhãn tiếng việt

DN_HCM_SEM1_PSB_v = within(DN_HCM_SEM1_PSB, {
  Res = factor(Res, labels = c("R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD", "B\u00ECnh th\u01B0\u1EDDng", "Kh\u00E1 \u0111\u1ED3ng \u00FD", "\u0110\u1ED3ng \u00FD", "R\u1EA5t \u0111\u1ED3ng \u00FD"))
  BPE = factor(BPE, labels = c("PSB1", "PSB2", "PSB3", "PSB4"))
  })
str(DN_HCM_SEM1_PSB_v)
## 'data.frame':    3452 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "R<U+1EA5>t không d<U+1ED3>ng ý",..: 6 3 2 5 2 2 1 6 2 4 ...
##  $ BPE: Factor w/ 4 levels "PSB1","PSB2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_PSB_v)
##                              Res         BPE     
##  R<U+1EA5>t không d<U+1ED3>ng ý: 146   PSB1:863  
##  Không d<U+1ED3>ng ý           : 296   PSB2:863  
##  Khá không d<U+1ED3>ng ý       : 190   PSB3:863  
##  Bình thu<U+1EDD>ng            : 381   PSB4:863  
##  Khá d<U+1ED3>ng ý             : 563             
##  Ð<U+1ED3>ng ý                 :1518             
##  R<U+1EA5>t d<U+1ED3>ng ý      : 358
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_PSB_v %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] PSB4 PSB1 PSB2 PSB3
## Levels: PSB1 PSB2 PSB3 PSB4
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 28 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PSB1  R<U+1EA5>t không d<U+1ED3>ng ý    59    6.84 6.84%   
##  2 PSB1  Không d<U+1ED3>ng ý       129   15.0  14.95%  
##  3 PSB1  Khá không d<U+1ED3>ng ý    53    6.14 6.14%   
##  4 PSB1  Bình thu<U+1EDD>ng         80    9.27 9.27%   
##  5 PSB1  Khá d<U+1ED3>ng ý         127   14.7  14.72%  
##  6 PSB1  Ð<U+1ED3>ng ý             332   38.5  38.47%  
##  7 PSB1  R<U+1EA5>t d<U+1ED3>ng ý          83    9.62 9.62%   
##  8 PSB2  R<U+1EA5>t không d<U+1ED3>ng ý    37    4.29 4.29%   
##  9 PSB2  Không d<U+1ED3>ng ý        65    7.53 7.53%   
## 10 PSB2  Khá không d<U+1ED3>ng ý    51    5.91 5.91%   
## # ... with 18 more rows
library(extrafont)
theme_set(theme_minimal())
gg_v <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "C\u1EA3m nh\u1EADn v\u1EC1 an to\u00E0n (PSA) c\u1EE7a h\u00E0nh kh\u00E1ch \u0111\u1ED1i v\u1EDBi h\u1EC7 th\u1ED1ng xe bu\u00FDt \u0111\u00F4 thi Vi\u1EC7t Nam ", 
       subtitle = "Thang \u0111o Likert l\u00E0 lo\u1EA1i thang \u0111o \u0111i\u1EC3m \u0111\u00E1nh gi\u00E1 \u0111\u01B0\u1EE3c s\u1EED d\u1EE5ng ph\u1ED5 bi\u1EBFn trong kh\u1EA3o s\u00E1t. Nghi\u00EAn c\u1EE9u s\u1EED d\u1EE5ng thang \u0111o 7 m\u1EE9c \u0111\u1ED9 t\u01B0\u01A1ng \u1EE9ng v\u1EDBi 1 - R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD \u0111\u1EBFn 7 - R\u1EA5t \u0111\u1ED3ng \u00FD.")
gg_v
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 4 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 PSB1  R<U+1EA5>t không d<U+1ED3>ng ý    59    6.84 6.84%   
## 2 PSB2  R<U+1EA5>t không d<U+1ED3>ng ý    37    4.29 4.29%   
## 3 PSB3  R<U+1EA5>t không d<U+1ED3>ng ý    18    2.09 2.09%   
## 4 PSB4  R<U+1EA5>t không d<U+1ED3>ng ý    32    3.71 3.71%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 4 x 5
##   BPE   Res              n percent bar_text
##   <fct> <fct>        <int>   <dbl> <chr>   
## 1 PSB1  Không d<U+1ED3>ng ý   129   15.0  14.95%  
## 2 PSB2  Không d<U+1ED3>ng ý    65    7.53 7.53%   
## 3 PSB3  Không d<U+1ED3>ng ý    23    2.67 2.67%   
## 4 PSB4  Không d<U+1ED3>ng ý    79    9.15 9.15%
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 4 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 PSB1  Khá không d<U+1ED3>ng ý    53    6.14 6.14%   
## 2 PSB2  Khá không d<U+1ED3>ng ý    51    5.91 5.91%   
## 3 PSB3  Khá không d<U+1ED3>ng ý    29    3.36 3.36%   
## 4 PSB4  Khá không d<U+1ED3>ng ý    57    6.6  6.6%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "B\u00ECnh th\u01B0\u1EDDng") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 4 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 PSB1  Bình thu<U+1EDD>ng    80    9.27 9.27%   
## 2 PSB2  Bình thu<U+1EDD>ng    87   10.1  10.08%  
## 3 PSB3  Bình thu<U+1EDD>ng    66    7.65 7.65%   
## 4 PSB4  Bình thu<U+1EDD>ng   148   17.2  17.15%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PSB1  Khá d<U+1ED3>ng ý   127    14.7 14.72%  
## 2 PSB2  Khá d<U+1ED3>ng ý   127    14.7 14.72%  
## 3 PSB3  Khá d<U+1ED3>ng ý   148    17.2 17.15%  
## 4 PSB4  Khá d<U+1ED3>ng ý   161    18.7 18.66%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "\u0110\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 4 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 PSB1  Ð<U+1ED3>ng ý   332    38.5 38.47%  
## 2 PSB2  Ð<U+1ED3>ng ý   402    46.6 46.58%  
## 3 PSB3  Ð<U+1ED3>ng ý   475    55.0 55.04%  
## 4 PSB4  Ð<U+1ED3>ng ý   309    35.8 35.81%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PSB1  R<U+1EA5>t d<U+1ED3>ng ý    83    9.62 9.62%   
## 2 PSB2  R<U+1EA5>t d<U+1ED3>ng ý    94   10.9  10.89%  
## 3 PSB3  R<U+1EA5>t d<U+1ED3>ng ý   104   12.0  12.05%  
## 4 PSB4  R<U+1EA5>t d<U+1ED3>ng ý    77    8.92 8.92%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") -> df_for_text
df_for_text
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PSB1  R<U+1EA5>t d<U+1ED3>ng ý    83    9.62 9.62%   
## 2 PSB2  R<U+1EA5>t d<U+1ED3>ng ý    94   10.9  10.89%  
## 3 PSB3  R<U+1EA5>t d<U+1ED3>ng ý   104   12.0  12.05%  
## 4 PSB4  R<U+1EA5>t d<U+1ED3>ng ý    77    8.92 8.92%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg_v + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 2, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-30, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-2, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.3. Construct LOY-Nhãn tiếng anh

# Plot Likert graph for people in both cities for construct LIN
library(tidyverse)
library(compareGroups)
head(DN_HCM_SEM1_LOY)
##   Res  BPE
## 1   4 LOY1
## 2   4 LOY1
## 3   2 LOY1
## 4   5 LOY1
## 5   4 LOY1
## 6   4 LOY1
dim(DN_HCM_SEM1_LOY)
## [1] 6041    2
attach(DN_HCM_SEM1_LOY)
## The following objects are masked from DN_HCM_SEM1_PSB:
## 
##     BPE, Res
## The following objects are masked from HCM_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1:
## 
##     BPE, Res
DN_HCM_SEM1_LOY = within(DN_HCM_SEM1_LOY, {
  Res = factor(Res, labels = c("Very Disagree", "Disagree", "Quite Disagree", "Normal", "Quite Agree", "Agree", "Very Agree"))
  BPE = factor(BPE, labels = c("LOY1", "LOY2", "LOY3", "LOY4", "LOY5", "LOY6", "LOY7"))
  })
str(DN_HCM_SEM1_LOY)
## 'data.frame':    6041 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "Very Disagree",..: 4 4 2 5 4 4 2 6 6 6 ...
##  $ BPE: Factor w/ 7 levels "LOY1","LOY2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_LOY)
##              Res         BPE     
##  Very Disagree :  66   LOY1:863  
##  Disagree      : 174   LOY2:863  
##  Quite Disagree: 183   LOY3:863  
##  Normal        : 767   LOY4:863  
##  Quite Agree   :1255   LOY5:863  
##  Agree         :2692   LOY6:863  
##  Very Agree    : 904   LOY7:863
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_LOY %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] LOY1 LOY2 LOY4 LOY6 LOY5 LOY3 LOY7
## Levels: LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 49 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 LOY1  Very Disagree      4    0.46 0.46%   
##  2 LOY1  Disagree          18    2.09 2.09%   
##  3 LOY1  Quite Disagree    20    2.32 2.32%   
##  4 LOY1  Normal           114   13.2  13.21%  
##  5 LOY1  Quite Agree      168   19.5  19.47%  
##  6 LOY1  Agree            433   50.2  50.17%  
##  7 LOY1  Very Agree       106   12.3  12.28%  
##  8 LOY2  Very Disagree      6    0.7  0.7%    
##  9 LOY2  Disagree          22    2.55 2.55%   
## 10 LOY2  Quite Disagree    17    1.97 1.97%   
## # ... with 39 more rows
library(extrafont)
theme_set(theme_minimal())
gg <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "Perceived loylty (LOY) of passengers in urban areas of Viet Nam", 
       subtitle = "Likert scale is a type of rating scale commonly used in surveys. When responding to a Likert type question,\nrespondents simply state their level of agreement or disagreement on a symmetric agree-disagree scale.")
gg
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "Very Disagree") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 7 x 5
##   BPE   Res               n percent bar_text
##   <fct> <fct>         <int>   <dbl> <chr>   
## 1 LOY1  Very Disagree     4    0.46 0.46%   
## 2 LOY2  Very Disagree     6    0.7  0.7%    
## 3 LOY3  Very Disagree     3    0.35 0.35%   
## 4 LOY4  Very Disagree    13    1.51 1.51%   
## 5 LOY5  Very Disagree    12    1.39 1.39%   
## 6 LOY6  Very Disagree    15    1.74 1.74%   
## 7 LOY7  Very Disagree    13    1.51 1.51%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Disagree") %>% 
  filter(percent >= 3) -> df_for_text2
df_for_text2
## # A tibble: 1 x 5
##   BPE   Res          n percent bar_text
##   <fct> <fct>    <int>   <dbl> <chr>   
## 1 LOY6  Disagree    60    6.95 6.95%
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Quite Disagree") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 7 x 5
##   BPE   Res                n percent bar_text
##   <fct> <fct>          <int>   <dbl> <chr>   
## 1 LOY1  Quite Disagree    20    2.32 2.32%   
## 2 LOY2  Quite Disagree    17    1.97 1.97%   
## 3 LOY3  Quite Disagree    19    2.2  2.2%    
## 4 LOY4  Quite Disagree    32    3.71 3.71%   
## 5 LOY5  Quite Disagree    26    3.01 3.01%   
## 6 LOY6  Quite Disagree    52    6.03 6.03%   
## 7 LOY7  Quite Disagree    17    1.97 1.97%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "Normal") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 7 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 LOY1  Normal   114   13.2  13.21%  
## 2 LOY2  Normal    99   11.5  11.47%  
## 3 LOY3  Normal    92   10.7  10.66%  
## 4 LOY4  Normal   130   15.1  15.06%  
## 5 LOY5  Normal   111   12.9  12.86%  
## 6 LOY6  Normal   141   16.3  16.34%  
## 7 LOY7  Normal    80    9.27 9.27%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Quite Agree") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 7 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 LOY1  Quite Agree   168    19.5 19.47%  
## 2 LOY2  Quite Agree   171    19.8 19.81%  
## 3 LOY3  Quite Agree   161    18.7 18.66%  
## 4 LOY4  Quite Agree   212    24.6 24.57%  
## 5 LOY5  Quite Agree   214    24.8 24.8%   
## 6 LOY6  Quite Agree   172    19.9 19.93%  
## 7 LOY7  Quite Agree   157    18.2 18.19%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "Agree") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 7 x 5
##   BPE   Res       n percent bar_text
##   <fct> <fct> <int>   <dbl> <chr>   
## 1 LOY1  Agree   433    50.2 50.17%  
## 2 LOY2  Agree   430    49.8 49.83%  
## 3 LOY3  Agree   440    51.0 50.98%  
## 4 LOY4  Agree   340    39.4 39.4%   
## 5 LOY5  Agree   347    40.2 40.21%  
## 6 LOY6  Agree   296    34.3 34.3%   
## 7 LOY7  Agree   406    47.0 47.05%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") %>% 
  filter(percent >= 0) -> df_for_text7 # Chỉ thể hiện giá trị có % lựa chọn >0
df_for_text7
## # A tibble: 7 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 LOY1  Very Agree   106    12.3 12.28%  
## 2 LOY2  Very Agree   118    13.7 13.67%  
## 3 LOY3  Very Agree   132    15.3 15.3%   
## 4 LOY4  Very Agree   118    13.7 13.67%  
## 5 LOY5  Very Agree   129    15.0 14.95%  
## 6 LOY6  Very Agree   127    14.7 14.72%  
## 7 LOY7  Very Agree   174    20.2 20.16%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") -> df_for_text
df_for_text
## # A tibble: 7 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 LOY1  Very Agree   106    12.3 12.28%  
## 2 LOY2  Very Agree   118    13.7 13.67%  
## 3 LOY3  Very Agree   132    15.3 15.3%   
## 4 LOY4  Very Agree   118    13.7 13.67%  
## 5 LOY5  Very Agree   129    15.0 14.95%  
## 6 LOY6  Very Agree   127    14.7 14.72%  
## 7 LOY7  Very Agree   174    20.2 20.16%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text2, aes(x = BPE, y = 9, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text5, aes(x = BPE, y = 32, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-30, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.4. Construct LOY-Nhãn tiếng việt

DN_HCM_SEM1_LOY_v = within(DN_HCM_SEM1_LOY, {
  Res = factor(Res, labels = c("R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD", "B\u00ECnh th\u01B0\u1EDDng", "Kh\u00E1 \u0111\u1ED3ng \u00FD", "\u0110\u1ED3ng \u00FD", "R\u1EA5t \u0111\u1ED3ng \u00FD"))
  BPE = factor(BPE, labels = c("LOY1", "LOY2", "LOY3", "LOY4", "LOY5", "LOY6", "LOY7"))
  })
str(DN_HCM_SEM1_LOY_v)
## 'data.frame':    6041 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "R<U+1EA5>t không d<U+1ED3>ng ý",..: 4 4 2 5 4 4 2 6 6 6 ...
##  $ BPE: Factor w/ 7 levels "LOY1","LOY2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_LOY_v)
##                              Res         BPE     
##  R<U+1EA5>t không d<U+1ED3>ng ý:  66   LOY1:863  
##  Không d<U+1ED3>ng ý           : 174   LOY2:863  
##  Khá không d<U+1ED3>ng ý       : 183   LOY3:863  
##  Bình thu<U+1EDD>ng            : 767   LOY4:863  
##  Khá d<U+1ED3>ng ý             :1255   LOY5:863  
##  Ð<U+1ED3>ng ý                 :2692   LOY6:863  
##  R<U+1EA5>t d<U+1ED3>ng ý      : 904   LOY7:863
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_LOY_v %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] LOY1 LOY2 LOY4 LOY6 LOY5 LOY3 LOY7
## Levels: LOY1 LOY2 LOY3 LOY4 LOY5 LOY6 LOY7
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 49 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 LOY1  R<U+1EA5>t không d<U+1ED3>ng ý     4    0.46 0.46%   
##  2 LOY1  Không d<U+1ED3>ng ý        18    2.09 2.09%   
##  3 LOY1  Khá không d<U+1ED3>ng ý    20    2.32 2.32%   
##  4 LOY1  Bình thu<U+1EDD>ng        114   13.2  13.21%  
##  5 LOY1  Khá d<U+1ED3>ng ý         168   19.5  19.47%  
##  6 LOY1  Ð<U+1ED3>ng ý             433   50.2  50.17%  
##  7 LOY1  R<U+1EA5>t d<U+1ED3>ng ý         106   12.3  12.28%  
##  8 LOY2  R<U+1EA5>t không d<U+1ED3>ng ý     6    0.7  0.7%    
##  9 LOY2  Không d<U+1ED3>ng ý        22    2.55 2.55%   
## 10 LOY2  Khá không d<U+1ED3>ng ý    17    1.97 1.97%   
## # ... with 39 more rows
library(extrafont)
theme_set(theme_minimal())
gg_v <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "C\u1EA3m nh\u1EADn l\u00F2ng trung th\u00E0nh (LOY) c\u1EE7a h\u00E0nh kh\u00E1ch s\u1EED d\u1EE5ng xe bu\u00FDt \u0111\u00F4 thi \u1EDF Vi\u1EC7t Nam ", 
       subtitle = "Thang \u0111o Likert l\u00E0 lo\u1EA1i thang \u0111o \u0111i\u1EC3m \u0111\u00E1nh gi\u00E1 \u0111\u01B0\u1EE3c s\u1EED d\u1EE5ng ph\u1ED5 bi\u1EBFn trong kh\u1EA3o s\u00E1t. Nghi\u00EAn c\u1EE9u s\u1EED d\u1EE5ng thang \u0111o 7 m\u1EE9c \u0111\u1ED9 t\u01B0\u01A1ng \u1EE9ng v\u1EDBi 1 - R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD \u0111\u1EBFn 7 - R\u1EA5t \u0111\u1ED3ng \u00FD.")
gg_v
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 7 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 LOY1  R<U+1EA5>t không d<U+1ED3>ng ý     4    0.46 0.46%   
## 2 LOY2  R<U+1EA5>t không d<U+1ED3>ng ý     6    0.7  0.7%    
## 3 LOY3  R<U+1EA5>t không d<U+1ED3>ng ý     3    0.35 0.35%   
## 4 LOY4  R<U+1EA5>t không d<U+1ED3>ng ý    13    1.51 1.51%   
## 5 LOY5  R<U+1EA5>t không d<U+1ED3>ng ý    12    1.39 1.39%   
## 6 LOY6  R<U+1EA5>t không d<U+1ED3>ng ý    15    1.74 1.74%   
## 7 LOY7  R<U+1EA5>t không d<U+1ED3>ng ý    13    1.51 1.51%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 7 x 5
##   BPE   Res              n percent bar_text
##   <fct> <fct>        <int>   <dbl> <chr>   
## 1 LOY1  Không d<U+1ED3>ng ý    18    2.09 2.09%   
## 2 LOY2  Không d<U+1ED3>ng ý    22    2.55 2.55%   
## 3 LOY3  Không d<U+1ED3>ng ý    16    1.85 1.85%   
## 4 LOY4  Không d<U+1ED3>ng ý    18    2.09 2.09%   
## 5 LOY5  Không d<U+1ED3>ng ý    24    2.78 2.78%   
## 6 LOY6  Không d<U+1ED3>ng ý    60    6.95 6.95%   
## 7 LOY7  Không d<U+1ED3>ng ý    16    1.85 1.85%
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 7 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 LOY1  Khá không d<U+1ED3>ng ý    20    2.32 2.32%   
## 2 LOY2  Khá không d<U+1ED3>ng ý    17    1.97 1.97%   
## 3 LOY3  Khá không d<U+1ED3>ng ý    19    2.2  2.2%    
## 4 LOY4  Khá không d<U+1ED3>ng ý    32    3.71 3.71%   
## 5 LOY5  Khá không d<U+1ED3>ng ý    26    3.01 3.01%   
## 6 LOY6  Khá không d<U+1ED3>ng ý    52    6.03 6.03%   
## 7 LOY7  Khá không d<U+1ED3>ng ý    17    1.97 1.97%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "B\u00ECnh th\u01B0\u1EDDng") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 7 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 LOY1  Bình thu<U+1EDD>ng   114   13.2  13.21%  
## 2 LOY2  Bình thu<U+1EDD>ng    99   11.5  11.47%  
## 3 LOY3  Bình thu<U+1EDD>ng    92   10.7  10.66%  
## 4 LOY4  Bình thu<U+1EDD>ng   130   15.1  15.06%  
## 5 LOY5  Bình thu<U+1EDD>ng   111   12.9  12.86%  
## 6 LOY6  Bình thu<U+1EDD>ng   141   16.3  16.34%  
## 7 LOY7  Bình thu<U+1EDD>ng    80    9.27 9.27%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 7 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 LOY1  Khá d<U+1ED3>ng ý   168    19.5 19.47%  
## 2 LOY2  Khá d<U+1ED3>ng ý   171    19.8 19.81%  
## 3 LOY3  Khá d<U+1ED3>ng ý   161    18.7 18.66%  
## 4 LOY4  Khá d<U+1ED3>ng ý   212    24.6 24.57%  
## 5 LOY5  Khá d<U+1ED3>ng ý   214    24.8 24.8%   
## 6 LOY6  Khá d<U+1ED3>ng ý   172    19.9 19.93%  
## 7 LOY7  Khá d<U+1ED3>ng ý   157    18.2 18.19%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "\u0110\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 7 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 LOY1  Ð<U+1ED3>ng ý   433    50.2 50.17%  
## 2 LOY2  Ð<U+1ED3>ng ý   430    49.8 49.83%  
## 3 LOY3  Ð<U+1ED3>ng ý   440    51.0 50.98%  
## 4 LOY4  Ð<U+1ED3>ng ý   340    39.4 39.4%   
## 5 LOY5  Ð<U+1ED3>ng ý   347    40.2 40.21%  
## 6 LOY6  Ð<U+1ED3>ng ý   296    34.3 34.3%   
## 7 LOY7  Ð<U+1ED3>ng ý   406    47.0 47.05%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 7 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 LOY1  R<U+1EA5>t d<U+1ED3>ng ý   106    12.3 12.28%  
## 2 LOY2  R<U+1EA5>t d<U+1ED3>ng ý   118    13.7 13.67%  
## 3 LOY3  R<U+1EA5>t d<U+1ED3>ng ý   132    15.3 15.3%   
## 4 LOY4  R<U+1EA5>t d<U+1ED3>ng ý   118    13.7 13.67%  
## 5 LOY5  R<U+1EA5>t d<U+1ED3>ng ý   129    15.0 14.95%  
## 6 LOY6  R<U+1EA5>t d<U+1ED3>ng ý   127    14.7 14.72%  
## 7 LOY7  R<U+1EA5>t d<U+1ED3>ng ý   174    20.2 20.16%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") -> df_for_text
df_for_text
## # A tibble: 7 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 LOY1  R<U+1EA5>t d<U+1ED3>ng ý   106    12.3 12.28%  
## 2 LOY2  R<U+1EA5>t d<U+1ED3>ng ý   118    13.7 13.67%  
## 3 LOY3  R<U+1EA5>t d<U+1ED3>ng ý   132    15.3 15.3%   
## 4 LOY4  R<U+1EA5>t d<U+1ED3>ng ý   118    13.7 13.67%  
## 5 LOY5  R<U+1EA5>t d<U+1ED3>ng ý   129    15.0 14.95%  
## 6 LOY6  R<U+1EA5>t d<U+1ED3>ng ý   127    14.7 14.72%  
## 7 LOY7  R<U+1EA5>t d<U+1ED3>ng ý   174    20.2 20.16%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg_v + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text2, aes(x = BPE, y = 9, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text5, aes(x = BPE, y = 32, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-30, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.5. Construct IMA- Nhãn tiếng anh

# Plot Likert graph for people in both cities for construct IMA
library(tidyverse)
library(compareGroups)
head(DN_HCM_SEM1_IMA)
##   Res  BPE
## 1   6 IMA1
## 2   4 IMA1
## 3   4 IMA1
## 4   5 IMA1
## 5   5 IMA1
## 6   4 IMA1
dim(DN_HCM_SEM1_IMA)
## [1] 4315    2
attach(DN_HCM_SEM1_IMA)
## The following objects are masked from DN_HCM_SEM1_LOY:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_PSB:
## 
##     BPE, Res
## The following objects are masked from HCM_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1:
## 
##     BPE, Res
DN_HCM_SEM1_IMA = within(DN_HCM_SEM1_IMA, {
  Res = factor(Res, labels = c("Very Disagree", "Disagree", "Quite Disagree", "Normal", "Quite Agree", "Agree", "Very Agree"))
  BPE = factor(BPE, labels = c("IMA1", "IMA2", "IMA3", "IMA4", "IMA5"))
  })
str(DN_HCM_SEM1_IMA)
## 'data.frame':    4315 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "Very Disagree",..: 6 4 4 5 5 4 2 6 5 6 ...
##  $ BPE: Factor w/ 5 levels "IMA1","IMA2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_IMA)
##              Res         BPE     
##  Very Disagree :  48   IMA1:863  
##  Disagree      :  85   IMA2:863  
##  Quite Disagree: 125   IMA3:863  
##  Normal        : 666   IMA4:863  
##  Quite Agree   :1058   IMA5:863  
##  Agree         :1953             
##  Very Agree    : 380
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_IMA %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] IMA1 IMA2 IMA3 IMA5 IMA4
## Levels: IMA1 IMA2 IMA3 IMA4 IMA5
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 35 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 IMA1  Very Disagree      6    0.7  0.7%    
##  2 IMA1  Disagree          20    2.32 2.32%   
##  3 IMA1  Quite Disagree    32    3.71 3.71%   
##  4 IMA1  Normal           156   18.1  18.08%  
##  5 IMA1  Quite Agree      230   26.6  26.65%  
##  6 IMA1  Agree            354   41.0  41.02%  
##  7 IMA1  Very Agree        65    7.53 7.53%   
##  8 IMA2  Very Disagree     11    1.27 1.27%   
##  9 IMA2  Disagree          19    2.2  2.2%    
## 10 IMA2  Quite Disagree    26    3.01 3.01%   
## # ... with 25 more rows
library(extrafont)
theme_set(theme_minimal())
gg <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "Perceived image (IMA) of passengers in urban areas of Viet Nam", 
       subtitle = "Likert scale is a type of rating scale commonly used in surveys. When responding to a Likert type question,\nrespondents simply state their level of agreement or disagreement on a symmetric agree-disagree scale.")
gg
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "Very Disagree") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 5 x 5
##   BPE   Res               n percent bar_text
##   <fct> <fct>         <int>   <dbl> <chr>   
## 1 IMA1  Very Disagree     6    0.7  0.7%    
## 2 IMA2  Very Disagree    11    1.27 1.27%   
## 3 IMA3  Very Disagree    11    1.27 1.27%   
## 4 IMA4  Very Disagree    14    1.62 1.62%   
## 5 IMA5  Very Disagree     6    0.7  0.7%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Disagree") %>% 
  filter(percent >= 3) -> df_for_text2
df_for_text2
## # A tibble: 0 x 5
## # ... with 5 variables: BPE <fct>, Res <fct>, n <int>, percent <dbl>,
## #   bar_text <chr>
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Quite Disagree") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 5 x 5
##   BPE   Res                n percent bar_text
##   <fct> <fct>          <int>   <dbl> <chr>   
## 1 IMA1  Quite Disagree    32    3.71 3.71%   
## 2 IMA2  Quite Disagree    26    3.01 3.01%   
## 3 IMA3  Quite Disagree    20    2.32 2.32%   
## 4 IMA4  Quite Disagree    26    3.01 3.01%   
## 5 IMA5  Quite Disagree    21    2.43 2.43%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "Normal") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 5 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 IMA1  Normal   156    18.1 18.08%  
## 2 IMA2  Normal   160    18.5 18.54%  
## 3 IMA3  Normal   130    15.1 15.06%  
## 4 IMA4  Normal   114    13.2 13.21%  
## 5 IMA5  Normal   106    12.3 12.28%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Quite Agree") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 5 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 IMA1  Quite Agree   230    26.6 26.65%  
## 2 IMA2  Quite Agree   213    24.7 24.68%  
## 3 IMA3  Quite Agree   209    24.2 24.22%  
## 4 IMA4  Quite Agree   201    23.3 23.29%  
## 5 IMA5  Quite Agree   205    23.8 23.75%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "Agree") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 5 x 5
##   BPE   Res       n percent bar_text
##   <fct> <fct> <int>   <dbl> <chr>   
## 1 IMA1  Agree   354    41.0 41.02%  
## 2 IMA2  Agree   368    42.6 42.64%  
## 3 IMA3  Agree   403    46.7 46.7%   
## 4 IMA4  Agree   408    47.3 47.28%  
## 5 IMA5  Agree   420    48.7 48.67%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") %>% 
  filter(percent >= 0) -> df_for_text7 # Chỉ thể hiện giá trị có % lựa chọn >0
df_for_text7
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 IMA1  Very Agree    65    7.53 7.53%   
## 2 IMA2  Very Agree    66    7.65 7.65%   
## 3 IMA3  Very Agree    79    9.15 9.15%   
## 4 IMA4  Very Agree    86    9.97 9.97%   
## 5 IMA5  Very Agree    84    9.73 9.73%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") -> df_for_text
df_for_text
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 IMA1  Very Agree    65    7.53 7.53%   
## 2 IMA2  Very Agree    66    7.65 7.65%   
## 3 IMA3  Very Agree    79    9.15 9.15%   
## 4 IMA4  Very Agree    86    9.97 9.97%   
## 5 IMA5  Very Agree    84    9.73 9.73%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text4, aes(x = BPE, y = 13, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text5, aes(x = BPE, y = 32, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-30, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.6. Construct IMA- Nhãn tiếng việt

DN_HCM_SEM1_IMA_v = within(DN_HCM_SEM1_IMA, {
  Res = factor(Res, labels = c("R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD", "B\u00ECnh th\u01B0\u1EDDng", "Kh\u00E1 \u0111\u1ED3ng \u00FD", "\u0110\u1ED3ng \u00FD", "R\u1EA5t \u0111\u1ED3ng \u00FD"))
  BPE = factor(BPE, labels = c("IMA1", "IMA2", "IMA3", "IMA4", "IMA5"))
  })
str(DN_HCM_SEM1_IMA_v)
## 'data.frame':    4315 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "R<U+1EA5>t không d<U+1ED3>ng ý",..: 6 4 4 5 5 4 2 6 5 6 ...
##  $ BPE: Factor w/ 5 levels "IMA1","IMA2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_IMA_v)
##                              Res         BPE     
##  R<U+1EA5>t không d<U+1ED3>ng ý:  48   IMA1:863  
##  Không d<U+1ED3>ng ý           :  85   IMA2:863  
##  Khá không d<U+1ED3>ng ý       : 125   IMA3:863  
##  Bình thu<U+1EDD>ng            : 666   IMA4:863  
##  Khá d<U+1ED3>ng ý             :1058   IMA5:863  
##  Ð<U+1ED3>ng ý                 :1953             
##  R<U+1EA5>t d<U+1ED3>ng ý      : 380
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_IMA_v %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] IMA1 IMA2 IMA3 IMA5 IMA4
## Levels: IMA1 IMA2 IMA3 IMA4 IMA5
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 35 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 IMA1  R<U+1EA5>t không d<U+1ED3>ng ý     6    0.7  0.7%    
##  2 IMA1  Không d<U+1ED3>ng ý        20    2.32 2.32%   
##  3 IMA1  Khá không d<U+1ED3>ng ý    32    3.71 3.71%   
##  4 IMA1  Bình thu<U+1EDD>ng        156   18.1  18.08%  
##  5 IMA1  Khá d<U+1ED3>ng ý         230   26.6  26.65%  
##  6 IMA1  Ð<U+1ED3>ng ý             354   41.0  41.02%  
##  7 IMA1  R<U+1EA5>t d<U+1ED3>ng ý          65    7.53 7.53%   
##  8 IMA2  R<U+1EA5>t không d<U+1ED3>ng ý    11    1.27 1.27%   
##  9 IMA2  Không d<U+1ED3>ng ý        19    2.2  2.2%    
## 10 IMA2  Khá không d<U+1ED3>ng ý    26    3.01 3.01%   
## # ... with 25 more rows
library(extrafont)
theme_set(theme_minimal())
gg_v <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "C\u1EA3m nh\u1EADn v\u1EC1 h\u00ECnh \u1EA3nh (IMA) c\u1EE7a h\u00E0nh kh\u00E1ch \u0111\u1ED1i v\u1EDBi h\u1EC7 th\u1ED1ng xe bu\u00FDt \u0111\u00F4 thi Vi\u1EC7t Nam ", 
       subtitle = "Thang \u0111o Likert l\u00E0 lo\u1EA1i thang \u0111o \u0111i\u1EC3m \u0111\u00E1nh gi\u00E1 \u0111\u01B0\u1EE3c s\u1EED d\u1EE5ng ph\u1ED5 bi\u1EBFn trong kh\u1EA3o s\u00E1t. Nghi\u00EAn c\u1EE9u s\u1EED d\u1EE5ng thang \u0111o 7 m\u1EE9c \u0111\u1ED9 t\u01B0\u01A1ng \u1EE9ng v\u1EDBi 1 - R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD \u0111\u1EBFn 7 - R\u1EA5t \u0111\u1ED3ng \u00FD.")
gg_v
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 5 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 IMA1  R<U+1EA5>t không d<U+1ED3>ng ý     6    0.7  0.7%    
## 2 IMA2  R<U+1EA5>t không d<U+1ED3>ng ý    11    1.27 1.27%   
## 3 IMA3  R<U+1EA5>t không d<U+1ED3>ng ý    11    1.27 1.27%   
## 4 IMA4  R<U+1EA5>t không d<U+1ED3>ng ý    14    1.62 1.62%   
## 5 IMA5  R<U+1EA5>t không d<U+1ED3>ng ý     6    0.7  0.7%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 5 x 5
##   BPE   Res              n percent bar_text
##   <fct> <fct>        <int>   <dbl> <chr>   
## 1 IMA1  Không d<U+1ED3>ng ý    20    2.32 2.32%   
## 2 IMA2  Không d<U+1ED3>ng ý    19    2.2  2.2%    
## 3 IMA3  Không d<U+1ED3>ng ý    11    1.27 1.27%   
## 4 IMA4  Không d<U+1ED3>ng ý    14    1.62 1.62%   
## 5 IMA5  Không d<U+1ED3>ng ý    21    2.43 2.43%
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 5 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 IMA1  Khá không d<U+1ED3>ng ý    32    3.71 3.71%   
## 2 IMA2  Khá không d<U+1ED3>ng ý    26    3.01 3.01%   
## 3 IMA3  Khá không d<U+1ED3>ng ý    20    2.32 2.32%   
## 4 IMA4  Khá không d<U+1ED3>ng ý    26    3.01 3.01%   
## 5 IMA5  Khá không d<U+1ED3>ng ý    21    2.43 2.43%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "B\u00ECnh th\u01B0\u1EDDng") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 5 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 IMA1  Bình thu<U+1EDD>ng   156    18.1 18.08%  
## 2 IMA2  Bình thu<U+1EDD>ng   160    18.5 18.54%  
## 3 IMA3  Bình thu<U+1EDD>ng   130    15.1 15.06%  
## 4 IMA4  Bình thu<U+1EDD>ng   114    13.2 13.21%  
## 5 IMA5  Bình thu<U+1EDD>ng   106    12.3 12.28%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 IMA1  Khá d<U+1ED3>ng ý   230    26.6 26.65%  
## 2 IMA2  Khá d<U+1ED3>ng ý   213    24.7 24.68%  
## 3 IMA3  Khá d<U+1ED3>ng ý   209    24.2 24.22%  
## 4 IMA4  Khá d<U+1ED3>ng ý   201    23.3 23.29%  
## 5 IMA5  Khá d<U+1ED3>ng ý   205    23.8 23.75%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "\u0110\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 5 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 IMA1  Ð<U+1ED3>ng ý   354    41.0 41.02%  
## 2 IMA2  Ð<U+1ED3>ng ý   368    42.6 42.64%  
## 3 IMA3  Ð<U+1ED3>ng ý   403    46.7 46.7%   
## 4 IMA4  Ð<U+1ED3>ng ý   408    47.3 47.28%  
## 5 IMA5  Ð<U+1ED3>ng ý   420    48.7 48.67%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 IMA1  R<U+1EA5>t d<U+1ED3>ng ý    65    7.53 7.53%   
## 2 IMA2  R<U+1EA5>t d<U+1ED3>ng ý    66    7.65 7.65%   
## 3 IMA3  R<U+1EA5>t d<U+1ED3>ng ý    79    9.15 9.15%   
## 4 IMA4  R<U+1EA5>t d<U+1ED3>ng ý    86    9.97 9.97%   
## 5 IMA5  R<U+1EA5>t d<U+1ED3>ng ý    84    9.73 9.73%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") -> df_for_text
df_for_text
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 IMA1  R<U+1EA5>t d<U+1ED3>ng ý    65    7.53 7.53%   
## 2 IMA2  R<U+1EA5>t d<U+1ED3>ng ý    66    7.65 7.65%   
## 3 IMA3  R<U+1EA5>t d<U+1ED3>ng ý    79    9.15 9.15%   
## 4 IMA4  R<U+1EA5>t d<U+1ED3>ng ý    86    9.97 9.97%   
## 5 IMA5  R<U+1EA5>t d<U+1ED3>ng ý    84    9.73 9.73%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg_v + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text4, aes(x = BPE, y = 13, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text5, aes(x = BPE, y = 32, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-30, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.7. Construct PHB - Nhãn tiếng anh

# Plot Likert graph for people in both cities for construct PHB
library(tidyverse)
library(compareGroups)
head(DN_HCM_SEM1_PHB)
##   Res  BPE
## 1   6 PHB1
## 2   3 PHB1
## 3   5 PHB1
## 4   5 PHB1
## 5   5 PHB1
## 6   4 PHB1
dim(DN_HCM_SEM1_PHB)
## [1] 4315    2
attach(DN_HCM_SEM1_PHB)
## The following objects are masked from DN_HCM_SEM1_IMA:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_LOY:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_PSB:
## 
##     BPE, Res
## The following objects are masked from HCM_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1:
## 
##     BPE, Res
DN_HCM_SEM1_PHB = within(DN_HCM_SEM1_PHB, {
  Res = factor(Res, labels = c("Very Disagree", "Disagree", "Quite Disagree", "Normal", "Quite Agree", "Agree", "Very Agree"))
  BPE = factor(BPE, labels = c("PHB1", "PHB2", "PHB3", "PHB4", "PHB5"))
  })
str(DN_HCM_SEM1_PHB)
## 'data.frame':    4315 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "Very Disagree",..: 6 3 5 5 5 4 3 6 6 7 ...
##  $ BPE: Factor w/ 5 levels "PHB1","PHB2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_PHB)
##              Res         BPE     
##  Very Disagree :  88   PHB1:863  
##  Disagree      :  76   PHB2:863  
##  Quite Disagree: 115   PHB3:863  
##  Normal        : 377   PHB4:863  
##  Quite Agree   : 807   PHB5:863  
##  Agree         :1928             
##  Very Agree    : 924
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_PHB %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] PHB1 PHB3 PHB2 PHB5 PHB4
## Levels: PHB1 PHB2 PHB3 PHB4 PHB5
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 35 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PHB1  Very Disagree     24    2.78 2.78%   
##  2 PHB1  Disagree          20    2.32 2.32%   
##  3 PHB1  Quite Disagree    37    4.29 4.29%   
##  4 PHB1  Normal            99   11.5  11.47%  
##  5 PHB1  Quite Agree      216   25.0  25.03%  
##  6 PHB1  Agree            353   40.9  40.9%   
##  7 PHB1  Very Agree       114   13.2  13.21%  
##  8 PHB2  Very Disagree     13    1.51 1.51%   
##  9 PHB2  Disagree          22    2.55 2.55%   
## 10 PHB2  Quite Disagree    25    2.9  2.9%    
## # ... with 25 more rows
library(extrafont)
theme_set(theme_minimal())
gg <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "Perceived health benefits (PHB) of passengers towards urban bus systems of Viet Nam", 
       subtitle = "Likert scale is a type of rating scale commonly used in surveys. When responding to a Likert type question,\nrespondents simply state their level of agreement or disagreement on a symmetric agree-disagree scale.")
gg
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "Very Disagree") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 5 x 5
##   BPE   Res               n percent bar_text
##   <fct> <fct>         <int>   <dbl> <chr>   
## 1 PHB1  Very Disagree    24    2.78 2.78%   
## 2 PHB2  Very Disagree    13    1.51 1.51%   
## 3 PHB3  Very Disagree    15    1.74 1.74%   
## 4 PHB4  Very Disagree    13    1.51 1.51%   
## 5 PHB5  Very Disagree    23    2.67 2.67%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Disagree") %>% 
  filter(percent >= 3) -> df_for_text2
df_for_text2
## # A tibble: 0 x 5
## # ... with 5 variables: BPE <fct>, Res <fct>, n <int>, percent <dbl>,
## #   bar_text <chr>
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Quite Disagree") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 5 x 5
##   BPE   Res                n percent bar_text
##   <fct> <fct>          <int>   <dbl> <chr>   
## 1 PHB1  Quite Disagree    37   4.29  4.29%   
## 2 PHB2  Quite Disagree    25   2.9   2.9%    
## 3 PHB3  Quite Disagree    38   4.4   4.4%    
## 4 PHB4  Quite Disagree    10   1.16  1.16%   
## 5 PHB5  Quite Disagree     5   0.580 0.58%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "Normal") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 5 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 PHB1  Normal    99   11.5  11.47%  
## 2 PHB2  Normal    70    8.11 8.11%   
## 3 PHB3  Normal    89   10.3  10.31%  
## 4 PHB4  Normal    55    6.37 6.37%   
## 5 PHB5  Normal    64    7.42 7.42%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Quite Agree") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 5 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 PHB1  Quite Agree   216    25.0 25.03%  
## 2 PHB2  Quite Agree   164    19   19%     
## 3 PHB3  Quite Agree   176    20.4 20.39%  
## 4 PHB4  Quite Agree   134    15.5 15.53%  
## 5 PHB5  Quite Agree   117    13.6 13.56%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "Agree") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 5 x 5
##   BPE   Res       n percent bar_text
##   <fct> <fct> <int>   <dbl> <chr>   
## 1 PHB1  Agree   353    40.9 40.9%   
## 2 PHB2  Agree   381    44.2 44.15%  
## 3 PHB3  Agree   385    44.6 44.61%  
## 4 PHB4  Agree   391    45.3 45.31%  
## 5 PHB5  Agree   418    48.4 48.44%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") %>% 
  filter(percent >= 0) -> df_for_text7 # Chỉ thể hiện giá trị có % lựa chọn >0
df_for_text7
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PHB1  Very Agree   114    13.2 13.21%  
## 2 PHB2  Very Agree   188    21.8 21.78%  
## 3 PHB3  Very Agree   140    16.2 16.22%  
## 4 PHB4  Very Agree   253    29.3 29.32%  
## 5 PHB5  Very Agree   229    26.5 26.54%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") -> df_for_text
df_for_text
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PHB1  Very Agree   114    13.2 13.21%  
## 2 PHB2  Very Agree   188    21.8 21.78%  
## 3 PHB3  Very Agree   140    16.2 16.22%  
## 4 PHB4  Very Agree   253    29.3 29.32%  
## 5 PHB5  Very Agree   229    26.5 26.54%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text5, aes(x = BPE, y = 22, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-40, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.8. Construct PHB - Nhãn tiếng việt

DN_HCM_SEM1_PHB_v = within(DN_HCM_SEM1_PHB, {
  Res = factor(Res, labels = c("R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD", "B\u00ECnh th\u01B0\u1EDDng", "Kh\u00E1 \u0111\u1ED3ng \u00FD", "\u0110\u1ED3ng \u00FD", "R\u1EA5t \u0111\u1ED3ng \u00FD"))
  BPE = factor(BPE, labels = c("PHB1", "PHB2", "PHB3", "PHB4", "PHB5"))
  })
str(DN_HCM_SEM1_PHB_v)
## 'data.frame':    4315 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "R<U+1EA5>t không d<U+1ED3>ng ý",..: 6 3 5 5 5 4 3 6 6 7 ...
##  $ BPE: Factor w/ 5 levels "PHB1","PHB2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_PHB_v)
##                              Res         BPE     
##  R<U+1EA5>t không d<U+1ED3>ng ý:  88   PHB1:863  
##  Không d<U+1ED3>ng ý           :  76   PHB2:863  
##  Khá không d<U+1ED3>ng ý       : 115   PHB3:863  
##  Bình thu<U+1EDD>ng            : 377   PHB4:863  
##  Khá d<U+1ED3>ng ý             : 807   PHB5:863  
##  Ð<U+1ED3>ng ý                 :1928             
##  R<U+1EA5>t d<U+1ED3>ng ý      : 924
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_PHB_v %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] PHB1 PHB3 PHB2 PHB5 PHB4
## Levels: PHB1 PHB2 PHB3 PHB4 PHB5
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 35 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PHB1  R<U+1EA5>t không d<U+1ED3>ng ý    24    2.78 2.78%   
##  2 PHB1  Không d<U+1ED3>ng ý        20    2.32 2.32%   
##  3 PHB1  Khá không d<U+1ED3>ng ý    37    4.29 4.29%   
##  4 PHB1  Bình thu<U+1EDD>ng         99   11.5  11.47%  
##  5 PHB1  Khá d<U+1ED3>ng ý         216   25.0  25.03%  
##  6 PHB1  Ð<U+1ED3>ng ý             353   40.9  40.9%   
##  7 PHB1  R<U+1EA5>t d<U+1ED3>ng ý         114   13.2  13.21%  
##  8 PHB2  R<U+1EA5>t không d<U+1ED3>ng ý    13    1.51 1.51%   
##  9 PHB2  Không d<U+1ED3>ng ý        22    2.55 2.55%   
## 10 PHB2  Khá không d<U+1ED3>ng ý    25    2.9  2.9%    
## # ... with 25 more rows
library(extrafont)
theme_set(theme_minimal())
gg_v <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "C\u1EA3m nh\u1EADn v\u1EC1 l\u1EE3i \u00EDch s\u1EE9c kh\u1ECFe (PHB) c\u1EE7a h\u00E0nh kh\u00E1ch \u0111\u1ED1i v\u1EDBi h\u1EC7 th\u1ED1ng xe bu\u00FDt \u0111\u00F4 thi Vi\u1EC7t Nam ", 
       subtitle = "Thang \u0111o Likert l\u00E0 lo\u1EA1i thang \u0111o \u0111i\u1EC3m \u0111\u00E1nh gi\u00E1 \u0111\u01B0\u1EE3c s\u1EED d\u1EE5ng ph\u1ED5 bi\u1EBFn trong kh\u1EA3o s\u00E1t. Nghi\u00EAn c\u1EE9u s\u1EED d\u1EE5ng thang \u0111o 7 m\u1EE9c \u0111\u1ED9 t\u01B0\u01A1ng \u1EE9ng v\u1EDBi 1 - R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD \u0111\u1EBFn 7 - R\u1EA5t \u0111\u1ED3ng \u00FD.")
gg_v
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 5 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 PHB1  R<U+1EA5>t không d<U+1ED3>ng ý    24    2.78 2.78%   
## 2 PHB2  R<U+1EA5>t không d<U+1ED3>ng ý    13    1.51 1.51%   
## 3 PHB3  R<U+1EA5>t không d<U+1ED3>ng ý    15    1.74 1.74%   
## 4 PHB4  R<U+1EA5>t không d<U+1ED3>ng ý    13    1.51 1.51%   
## 5 PHB5  R<U+1EA5>t không d<U+1ED3>ng ý    23    2.67 2.67%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 5 x 5
##   BPE   Res              n percent bar_text
##   <fct> <fct>        <int>   <dbl> <chr>   
## 1 PHB1  Không d<U+1ED3>ng ý    20    2.32 2.32%   
## 2 PHB2  Không d<U+1ED3>ng ý    22    2.55 2.55%   
## 3 PHB3  Không d<U+1ED3>ng ý    20    2.32 2.32%   
## 4 PHB4  Không d<U+1ED3>ng ý     7    0.81 0.81%   
## 5 PHB5  Không d<U+1ED3>ng ý     7    0.81 0.81%
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 5 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 PHB1  Khá không d<U+1ED3>ng ý    37   4.29  4.29%   
## 2 PHB2  Khá không d<U+1ED3>ng ý    25   2.9   2.9%    
## 3 PHB3  Khá không d<U+1ED3>ng ý    38   4.4   4.4%    
## 4 PHB4  Khá không d<U+1ED3>ng ý    10   1.16  1.16%   
## 5 PHB5  Khá không d<U+1ED3>ng ý     5   0.580 0.58%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "B\u00ECnh th\u01B0\u1EDDng") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 5 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 PHB1  Bình thu<U+1EDD>ng    99   11.5  11.47%  
## 2 PHB2  Bình thu<U+1EDD>ng    70    8.11 8.11%   
## 3 PHB3  Bình thu<U+1EDD>ng    89   10.3  10.31%  
## 4 PHB4  Bình thu<U+1EDD>ng    55    6.37 6.37%   
## 5 PHB5  Bình thu<U+1EDD>ng    64    7.42 7.42%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PHB1  Khá d<U+1ED3>ng ý   216    25.0 25.03%  
## 2 PHB2  Khá d<U+1ED3>ng ý   164    19   19%     
## 3 PHB3  Khá d<U+1ED3>ng ý   176    20.4 20.39%  
## 4 PHB4  Khá d<U+1ED3>ng ý   134    15.5 15.53%  
## 5 PHB5  Khá d<U+1ED3>ng ý   117    13.6 13.56%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "\u0110\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 5 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 PHB1  Ð<U+1ED3>ng ý   353    40.9 40.9%   
## 2 PHB2  Ð<U+1ED3>ng ý   381    44.2 44.15%  
## 3 PHB3  Ð<U+1ED3>ng ý   385    44.6 44.61%  
## 4 PHB4  Ð<U+1ED3>ng ý   391    45.3 45.31%  
## 5 PHB5  Ð<U+1ED3>ng ý   418    48.4 48.44%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PHB1  R<U+1EA5>t d<U+1ED3>ng ý   114    13.2 13.21%  
## 2 PHB2  R<U+1EA5>t d<U+1ED3>ng ý   188    21.8 21.78%  
## 3 PHB3  R<U+1EA5>t d<U+1ED3>ng ý   140    16.2 16.22%  
## 4 PHB4  R<U+1EA5>t d<U+1ED3>ng ý   253    29.3 29.32%  
## 5 PHB5  R<U+1EA5>t d<U+1ED3>ng ý   229    26.5 26.54%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") -> df_for_text
df_for_text
## # A tibble: 5 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PHB1  R<U+1EA5>t d<U+1ED3>ng ý   114    13.2 13.21%  
## 2 PHB2  R<U+1EA5>t d<U+1ED3>ng ý   188    21.8 21.78%  
## 3 PHB3  R<U+1EA5>t d<U+1ED3>ng ý   140    16.2 16.22%  
## 4 PHB4  R<U+1EA5>t d<U+1ED3>ng ý   253    29.3 29.32%  
## 5 PHB5  R<U+1EA5>t d<U+1ED3>ng ý   229    26.5 26.54%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg_v + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text5, aes(x = BPE, y = 22, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-40, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.9. Construct PEV - Nhãn tiếng anh

# Plot Likert graph for people in both cities for construct PEV
library(tidyverse)
library(compareGroups)
head(DN_HCM_SEM1_PEV)
##   Res  BPE
## 1   4 PEV1
## 2   5 PEV1
## 3   6 PEV1
## 4   5 PEV1
## 5   6 PEV1
## 6   7 PEV1
dim(DN_HCM_SEM1_PEV)
## [1] 3452    2
attach(DN_HCM_SEM1_PEV)
## The following objects are masked from DN_HCM_SEM1_PHB:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_IMA:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_LOY:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_PSB:
## 
##     BPE, Res
## The following objects are masked from HCM_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1:
## 
##     BPE, Res
DN_HCM_SEM1_PEV = within(DN_HCM_SEM1_PEV, {
  Res = factor(Res, labels = c("Very Disagree", "Disagree", "Quite Disagree", "Normal", "Quite Agree", "Agree", "Very Agree"))
  BPE = factor(BPE, labels = c("PEV1", "PEV2", "PEV3", "PEV4"))
  })
str(DN_HCM_SEM1_PEV)
## 'data.frame':    3452 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "Very Disagree",..: 4 5 6 5 6 7 4 6 7 7 ...
##  $ BPE: Factor w/ 4 levels "PEV1","PEV2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_PEV)
##              Res         BPE     
##  Very Disagree : 101   PEV1:863  
##  Disagree      :  68   PEV2:863  
##  Quite Disagree:  77   PEV3:863  
##  Normal        : 301   PEV4:863  
##  Quite Agree   : 634             
##  Agree         :1462             
##  Very Agree    : 809
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_PEV %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] PEV4 PEV3 PEV2 PEV1
## Levels: PEV1 PEV2 PEV3 PEV4
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 28 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PEV1  Very Disagree     25    2.9  2.9%    
##  2 PEV1  Disagree           9    1.04 1.04%   
##  3 PEV1  Quite Disagree    13    1.51 1.51%   
##  4 PEV1  Normal            59    6.84 6.84%   
##  5 PEV1  Quite Agree      128   14.8  14.83%  
##  6 PEV1  Agree            393   45.5  45.54%  
##  7 PEV1  Very Agree       236   27.4  27.35%  
##  8 PEV2  Very Disagree     26    3.01 3.01%   
##  9 PEV2  Disagree          18    2.09 2.09%   
## 10 PEV2  Quite Disagree    20    2.32 2.32%   
## # ... with 18 more rows
library(extrafont)
theme_set(theme_minimal())
gg <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "Perceived environmental values (PEV) towards urban bus systems of Viet Nam", 
       subtitle = "Likert scale is a type of rating scale commonly used in surveys. When responding to a Likert type question,\nrespondents simply state their level of agreement or disagreement on a symmetric agree-disagree scale.")
gg
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "Very Disagree") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 4 x 5
##   BPE   Res               n percent bar_text
##   <fct> <fct>         <int>   <dbl> <chr>   
## 1 PEV1  Very Disagree    25    2.9  2.9%    
## 2 PEV2  Very Disagree    26    3.01 3.01%   
## 3 PEV3  Very Disagree    26    3.01 3.01%   
## 4 PEV4  Very Disagree    24    2.78 2.78%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Disagree") %>% 
  filter(percent >= 3) -> df_for_text2
df_for_text2
## # A tibble: 0 x 5
## # ... with 5 variables: BPE <fct>, Res <fct>, n <int>, percent <dbl>,
## #   bar_text <chr>
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Quite Disagree") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 4 x 5
##   BPE   Res                n percent bar_text
##   <fct> <fct>          <int>   <dbl> <chr>   
## 1 PEV1  Quite Disagree    13    1.51 1.51%   
## 2 PEV2  Quite Disagree    20    2.32 2.32%   
## 3 PEV3  Quite Disagree    22    2.55 2.55%   
## 4 PEV4  Quite Disagree    22    2.55 2.55%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "Normal") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 4 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 PEV1  Normal    59    6.84 6.84%   
## 2 PEV2  Normal    68    7.88 7.88%   
## 3 PEV3  Normal    78    9.04 9.04%   
## 4 PEV4  Normal    96   11.1  11.12%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Quite Agree") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 4 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 PEV1  Quite Agree   128    14.8 14.83%  
## 2 PEV2  Quite Agree   171    19.8 19.81%  
## 3 PEV3  Quite Agree   168    19.5 19.47%  
## 4 PEV4  Quite Agree   167    19.4 19.35%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "Agree") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 4 x 5
##   BPE   Res       n percent bar_text
##   <fct> <fct> <int>   <dbl> <chr>   
## 1 PEV1  Agree   393    45.5 45.54%  
## 2 PEV2  Agree   355    41.1 41.14%  
## 3 PEV3  Agree   355    41.1 41.14%  
## 4 PEV4  Agree   359    41.6 41.6%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") %>% 
  filter(percent >= 0) -> df_for_text7 # Chỉ thể hiện giá trị có % lựa chọn >0
df_for_text7
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PEV1  Very Agree   236    27.4 27.35%  
## 2 PEV2  Very Agree   205    23.8 23.75%  
## 3 PEV3  Very Agree   193    22.4 22.36%  
## 4 PEV4  Very Agree   175    20.3 20.28%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") -> df_for_text
df_for_text
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PEV1  Very Agree   236    27.4 27.35%  
## 2 PEV2  Very Agree   205    23.8 23.75%  
## 3 PEV3  Very Agree   193    22.4 22.36%  
## 4 PEV4  Very Agree   175    20.3 20.28%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text5, aes(x = BPE, y = 22, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-40, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.10. Construct PEV - Nhãn tiếng việt

DN_HCM_SEM1_PEV_v = within(DN_HCM_SEM1_PEV, {
  Res = factor(Res, labels = c("R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD", "B\u00ECnh th\u01B0\u1EDDng", "Kh\u00E1 \u0111\u1ED3ng \u00FD", "\u0110\u1ED3ng \u00FD", "R\u1EA5t \u0111\u1ED3ng \u00FD"))
  BPE = factor(BPE, labels = c("PEV1", "PEV2", "PEV3", "PEV4"))
  })
str(DN_HCM_SEM1_PEV_v)
## 'data.frame':    3452 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "R<U+1EA5>t không d<U+1ED3>ng ý",..: 4 5 6 5 6 7 4 6 7 7 ...
##  $ BPE: Factor w/ 4 levels "PEV1","PEV2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_PEV_v)
##                              Res         BPE     
##  R<U+1EA5>t không d<U+1ED3>ng ý: 101   PEV1:863  
##  Không d<U+1ED3>ng ý           :  68   PEV2:863  
##  Khá không d<U+1ED3>ng ý       :  77   PEV3:863  
##  Bình thu<U+1EDD>ng            : 301   PEV4:863  
##  Khá d<U+1ED3>ng ý             : 634             
##  Ð<U+1ED3>ng ý                 :1462             
##  R<U+1EA5>t d<U+1ED3>ng ý      : 809
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_PEV_v %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] PEV4 PEV3 PEV2 PEV1
## Levels: PEV1 PEV2 PEV3 PEV4
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 28 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PEV1  R<U+1EA5>t không d<U+1ED3>ng ý    25    2.9  2.9%    
##  2 PEV1  Không d<U+1ED3>ng ý         9    1.04 1.04%   
##  3 PEV1  Khá không d<U+1ED3>ng ý    13    1.51 1.51%   
##  4 PEV1  Bình thu<U+1EDD>ng         59    6.84 6.84%   
##  5 PEV1  Khá d<U+1ED3>ng ý         128   14.8  14.83%  
##  6 PEV1  Ð<U+1ED3>ng ý             393   45.5  45.54%  
##  7 PEV1  R<U+1EA5>t d<U+1ED3>ng ý         236   27.4  27.35%  
##  8 PEV2  R<U+1EA5>t không d<U+1ED3>ng ý    26    3.01 3.01%   
##  9 PEV2  Không d<U+1ED3>ng ý        18    2.09 2.09%   
## 10 PEV2  Khá không d<U+1ED3>ng ý    20    2.32 2.32%   
## # ... with 18 more rows
library(extrafont)
theme_set(theme_minimal())
gg_v <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "C\u1EA3m nh\u1EADn v\u1EC1 gi\u00E1 tr\u1ECB m\u00F4i tr\u01B0\u1EDDng (PEV) c\u1EE7a h\u00E0nh kh\u00E1ch \u0111\u1ED1i v\u1EDBi h\u1EC7 th\u1ED1ng xe bu\u00FDt \u0111\u00F4 thi Vi\u1EC7t Nam ", 
       subtitle = "Thang \u0111o Likert l\u00E0 lo\u1EA1i thang \u0111o \u0111i\u1EC3m \u0111\u00E1nh gi\u00E1 \u0111\u01B0\u1EE3c s\u1EED d\u1EE5ng ph\u1ED5 bi\u1EBFn trong kh\u1EA3o s\u00E1t. Nghi\u00EAn c\u1EE9u s\u1EED d\u1EE5ng thang \u0111o 7 m\u1EE9c \u0111\u1ED9 t\u01B0\u01A1ng \u1EE9ng v\u1EDBi 1 - R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD \u0111\u1EBFn 7 - R\u1EA5t \u0111\u1ED3ng \u00FD.")
gg_v
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 4 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 PEV1  R<U+1EA5>t không d<U+1ED3>ng ý    25    2.9  2.9%    
## 2 PEV2  R<U+1EA5>t không d<U+1ED3>ng ý    26    3.01 3.01%   
## 3 PEV3  R<U+1EA5>t không d<U+1ED3>ng ý    26    3.01 3.01%   
## 4 PEV4  R<U+1EA5>t không d<U+1ED3>ng ý    24    2.78 2.78%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 4 x 5
##   BPE   Res              n percent bar_text
##   <fct> <fct>        <int>   <dbl> <chr>   
## 1 PEV1  Không d<U+1ED3>ng ý     9    1.04 1.04%   
## 2 PEV2  Không d<U+1ED3>ng ý    18    2.09 2.09%   
## 3 PEV3  Không d<U+1ED3>ng ý    21    2.43 2.43%   
## 4 PEV4  Không d<U+1ED3>ng ý    20    2.32 2.32%
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 4 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 PEV1  Khá không d<U+1ED3>ng ý    13    1.51 1.51%   
## 2 PEV2  Khá không d<U+1ED3>ng ý    20    2.32 2.32%   
## 3 PEV3  Khá không d<U+1ED3>ng ý    22    2.55 2.55%   
## 4 PEV4  Khá không d<U+1ED3>ng ý    22    2.55 2.55%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "B\u00ECnh th\u01B0\u1EDDng") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 4 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 PEV1  Bình thu<U+1EDD>ng    59    6.84 6.84%   
## 2 PEV2  Bình thu<U+1EDD>ng    68    7.88 7.88%   
## 3 PEV3  Bình thu<U+1EDD>ng    78    9.04 9.04%   
## 4 PEV4  Bình thu<U+1EDD>ng    96   11.1  11.12%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PEV1  Khá d<U+1ED3>ng ý   128    14.8 14.83%  
## 2 PEV2  Khá d<U+1ED3>ng ý   171    19.8 19.81%  
## 3 PEV3  Khá d<U+1ED3>ng ý   168    19.5 19.47%  
## 4 PEV4  Khá d<U+1ED3>ng ý   167    19.4 19.35%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "\u0110\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 4 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 PEV1  Ð<U+1ED3>ng ý   393    45.5 45.54%  
## 2 PEV2  Ð<U+1ED3>ng ý   355    41.1 41.14%  
## 3 PEV3  Ð<U+1ED3>ng ý   355    41.1 41.14%  
## 4 PEV4  Ð<U+1ED3>ng ý   359    41.6 41.6%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PEV1  R<U+1EA5>t d<U+1ED3>ng ý   236    27.4 27.35%  
## 2 PEV2  R<U+1EA5>t d<U+1ED3>ng ý   205    23.8 23.75%  
## 3 PEV3  R<U+1EA5>t d<U+1ED3>ng ý   193    22.4 22.36%  
## 4 PEV4  R<U+1EA5>t d<U+1ED3>ng ý   175    20.3 20.28%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") -> df_for_text
df_for_text
## # A tibble: 4 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PEV1  R<U+1EA5>t d<U+1ED3>ng ý   236    27.4 27.35%  
## 2 PEV2  R<U+1EA5>t d<U+1ED3>ng ý   205    23.8 23.75%  
## 3 PEV3  R<U+1EA5>t d<U+1ED3>ng ý   193    22.4 22.36%  
## 4 PEV4  R<U+1EA5>t d<U+1ED3>ng ý   175    20.3 20.28%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg_v + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text5, aes(x = BPE, y = 22, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-40, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.11. Construct PVA - Nhãn tiếng anh

# Plot Likert graph for people in both cities for construct PVA
library(tidyverse)
library(compareGroups)
head(DN_HCM_SEM1_PVA)
##   Res  BPE
## 1   6 PVA1
## 2   5 PVA1
## 3   6 PVA1
## 4   5 PVA1
## 5   6 PVA1
## 6   6 PVA1
dim(DN_HCM_SEM1_PVA)
## [1] 2589    2
attach(DN_HCM_SEM1_PVA)
## The following objects are masked from DN_HCM_SEM1_PEV:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_PHB:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_IMA:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_LOY:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1_PSB:
## 
##     BPE, Res
## The following objects are masked from HCM_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_SEM1:
## 
##     BPE, Res
## The following objects are masked from DN_HCM_SEM1:
## 
##     BPE, Res
DN_HCM_SEM1_PVA = within(DN_HCM_SEM1_PVA, {
  Res = factor(Res, labels = c("Very Disagree", "Disagree", "Quite Disagree", "Normal", "Quite Agree", "Agree", "Very Agree"))
  BPE = factor(BPE, labels = c("PVA1", "PVA2", "PVA3"))
  })
str(DN_HCM_SEM1_PVA)
## 'data.frame':    2589 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "Very Disagree",..: 6 5 6 5 6 6 6 6 6 6 ...
##  $ BPE: Factor w/ 3 levels "PVA1","PVA2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_PVA)
##              Res         BPE     
##  Very Disagree :  11   PVA1:863  
##  Disagree      :  47   PVA2:863  
##  Quite Disagree:  78   PVA3:863  
##  Normal        : 227             
##  Quite Agree   : 487             
##  Agree         :1178             
##  Very Agree    : 561
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_PVA %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] PVA2 PVA3 PVA1
## Levels: PVA1 PVA2 PVA3
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 21 x 5
##    BPE   Res                n percent bar_text
##    <fct> <fct>          <int>   <dbl> <chr>   
##  1 PVA1  Very Disagree      5   0.580 0.58%   
##  2 PVA1  Disagree           8   0.93  0.93%   
##  3 PVA1  Quite Disagree    18   2.09  2.09%   
##  4 PVA1  Normal            56   6.49  6.49%   
##  5 PVA1  Quite Agree      130  15.1   15.06%  
##  6 PVA1  Agree            418  48.4   48.44%  
##  7 PVA1  Very Agree       228  26.4   26.42%  
##  8 PVA2  Very Disagree      4   0.46  0.46%   
##  9 PVA2  Disagree          26   3.01  3.01%   
## 10 PVA2  Quite Disagree    39   4.52  4.52%   
## # ... with 11 more rows
library(extrafont)
theme_set(theme_minimal())
gg <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "Perceived values (PVA) towards urban bus systems of Viet Nam", 
       subtitle = "Likert scale is a type of rating scale commonly used in surveys. When responding to a Likert type question,\nrespondents simply state their level of agreement or disagreement on a symmetric agree-disagree scale.")
gg
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "Very Disagree") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 3 x 5
##   BPE   Res               n percent bar_text
##   <fct> <fct>         <int>   <dbl> <chr>   
## 1 PVA1  Very Disagree     5   0.580 0.58%   
## 2 PVA2  Very Disagree     4   0.46  0.46%   
## 3 PVA3  Very Disagree     2   0.23  0.23%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Disagree") %>% 
  filter(percent >= 3) -> df_for_text2
df_for_text2
## # A tibble: 1 x 5
##   BPE   Res          n percent bar_text
##   <fct> <fct>    <int>   <dbl> <chr>   
## 1 PVA2  Disagree    26    3.01 3.01%
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Quite Disagree") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 3 x 5
##   BPE   Res                n percent bar_text
##   <fct> <fct>          <int>   <dbl> <chr>   
## 1 PVA1  Quite Disagree    18    2.09 2.09%   
## 2 PVA2  Quite Disagree    39    4.52 4.52%   
## 3 PVA3  Quite Disagree    21    2.43 2.43%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "Normal") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 3 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 PVA1  Normal    56    6.49 6.49%   
## 2 PVA2  Normal   101   11.7  11.7%   
## 3 PVA3  Normal    70    8.11 8.11%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Quite Agree") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 3 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 PVA1  Quite Agree   130    15.1 15.06%  
## 2 PVA2  Quite Agree   196    22.7 22.71%  
## 3 PVA3  Quite Agree   161    18.7 18.66%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "Agree") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 3 x 5
##   BPE   Res       n percent bar_text
##   <fct> <fct> <int>   <dbl> <chr>   
## 1 PVA1  Agree   418    48.4 48.44%  
## 2 PVA2  Agree   345    40.0 39.98%  
## 3 PVA3  Agree   415    48.1 48.09%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") %>% 
  filter(percent >= 0) -> df_for_text7 # Chỉ thể hiện giá trị có % lựa chọn >0
df_for_text7
## # A tibble: 3 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PVA1  Very Agree   228    26.4 26.42%  
## 2 PVA2  Very Agree   152    17.6 17.61%  
## 3 PVA3  Very Agree   181    21.0 20.97%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "Very Agree") -> df_for_text
df_for_text
## # A tibble: 3 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PVA1  Very Agree   228    26.4 26.42%  
## 2 PVA2  Very Agree   152    17.6 17.61%  
## 3 PVA3  Very Agree   181    21.0 20.97%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text5, aes(x = BPE, y = 23, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-40, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

3.8.12. Construct PVA - Nhãn tiếng anh

DN_HCM_SEM1_PVA_v = within(DN_HCM_SEM1_PVA, {
  Res = factor(Res, labels = c("R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00F4ng \u0111\u1ED3ng \u00FD", "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD", "B\u00ECnh th\u01B0\u1EDDng", "Kh\u00E1 \u0111\u1ED3ng \u00FD", "\u0110\u1ED3ng \u00FD", "R\u1EA5t \u0111\u1ED3ng \u00FD"))
  BPE = factor(BPE, labels = c("PVA1", "PVA2", "PVA3"))
  })
str(DN_HCM_SEM1_PVA_v)
## 'data.frame':    2589 obs. of  2 variables:
##  $ Res: Factor w/ 7 levels "R<U+1EA5>t không d<U+1ED3>ng ý",..: 6 5 6 5 6 6 6 6 6 6 ...
##  $ BPE: Factor w/ 3 levels "PVA1","PVA2",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(DN_HCM_SEM1_PVA_v)
##                              Res         BPE     
##  R<U+1EA5>t không d<U+1ED3>ng ý:  11   PVA1:863  
##  Không d<U+1ED3>ng ý           :  47   PVA2:863  
##  Khá không d<U+1ED3>ng ý       :  78   PVA3:863  
##  Bình thu<U+1EDD>ng            : 227             
##  Khá d<U+1ED3>ng ý             : 487             
##  Ð<U+1ED3>ng ý                 :1178             
##  R<U+1EA5>t d<U+1ED3>ng ý      : 561
    ## t <- compareGroups(Res ~ BPE, data = DN_SEM1)
    ## createTable(t)
DN_HCM_SEM1_PVA_v %>% 
  group_by(BPE, Res) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(BPE) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 2)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_ploting
df_for_ploting %>% 
  filter(Res == Res[7]) %>% 
  arrange(percent) %>% # sắp xếp theo %
  pull(BPE) -> order_x
order_x
## [1] PVA2 PVA3 PVA1
## Levels: PVA1 PVA2 PVA3
# Make a draft plot: 
my_colors <- c("#3e6487", "#829cb2", "pink", "pink3", "palevioletred", "rosybrown2", "rosybrown3")
my_font <- "Roboto Condensed"

df_for_ploting %>% 
  mutate(BPE = factor(BPE, levels = order_x), Res = factor(Res, levels = Res[7:1])) -> df_odered
df_odered
## # A tibble: 21 x 5
##    BPE   Res                  n percent bar_text
##    <fct> <fct>            <int>   <dbl> <chr>   
##  1 PVA1  R<U+1EA5>t không d<U+1ED3>ng ý     5   0.580 0.58%   
##  2 PVA1  Không d<U+1ED3>ng ý         8   0.93  0.93%   
##  3 PVA1  Khá không d<U+1ED3>ng ý    18   2.09  2.09%   
##  4 PVA1  Bình thu<U+1EDD>ng         56   6.49  6.49%   
##  5 PVA1  Khá d<U+1ED3>ng ý         130  15.1   15.06%  
##  6 PVA1  Ð<U+1ED3>ng ý             418  48.4   48.44%  
##  7 PVA1  R<U+1EA5>t d<U+1ED3>ng ý         228  26.4   26.42%  
##  8 PVA2  R<U+1EA5>t không d<U+1ED3>ng ý     4   0.46  0.46%   
##  9 PVA2  Không d<U+1ED3>ng ý        26   3.01  3.01%   
## 10 PVA2  Khá không d<U+1ED3>ng ý    39   4.52  4.52%   
## # ... with 11 more rows
library(extrafont)
theme_set(theme_minimal())
gg_v <- df_odered %>% 
  ggplot(aes(x = BPE, y = percent, fill = Res)) + 
  geom_col(width = 0.8) + 
  coord_flip() + 
  scale_fill_manual(values = my_colors[7:1], name = "") + 
  theme(legend.position = "top") + 
  theme(text = element_text(family = my_font)) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) + 
  theme(plot.title = element_text(size = 18), plot.subtitle = element_text(size = 11, color = "grey20")) + 
  theme(axis.text = element_text(color = "grey20", size = 10.2)) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + 
  theme(legend.key.height = unit(0.15, "mm")) + 
  labs(x = NULL, y = NULL, 
       title = "C\u1EA3m nh\u1EADn c\u1EE7a h\u00E0nh kh\u00E1ch v\u1EC1 gi\u00E1 tr\u1ECB (PVA) \u0111\u1ED1i v\u1EDBi h\u1EC7 th\u1ED1ng xe bu\u00FDt \u0111\u00F4 thi Vi\u1EC7t Nam ", 
       subtitle = "Thang \u0111o Likert l\u00E0 lo\u1EA1i thang \u0111o \u0111i\u1EC3m \u0111\u00E1nh gi\u00E1 \u0111\u01B0\u1EE3c s\u1EED d\u1EE5ng ph\u1ED5 bi\u1EBFn trong kh\u1EA3o s\u00E1t. Nghi\u00EAn c\u1EE9u s\u1EED d\u1EE5ng thang \u0111o 7 m\u1EE9c \u0111\u1ED9 t\u01B0\u01A1ng \u1EE9ng v\u1EDBi 1 - R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD \u0111\u1EBFn 7 - R\u1EA5t \u0111\u1ED3ng \u00FD.")
gg_v
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# For displaying percent of "Very Disagree": Điều chỉnh percent để thể hiện giá trị của %
df_odered %>% 
  filter(Res == "R\u1EA5t kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text1
df_for_text1
## # A tibble: 3 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 PVA1  R<U+1EA5>t không d<U+1ED3>ng ý     5   0.580 0.58%   
## 2 PVA2  R<U+1EA5>t không d<U+1ED3>ng ý     4   0.46  0.46%   
## 3 PVA3  R<U+1EA5>t không d<U+1ED3>ng ý     2   0.23  0.23%
# For displaying percent of "Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text2
df_for_text2
## # A tibble: 3 x 5
##   BPE   Res              n percent bar_text
##   <fct> <fct>        <int>   <dbl> <chr>   
## 1 PVA1  Không d<U+1ED3>ng ý     8    0.93 0.93%   
## 2 PVA2  Không d<U+1ED3>ng ý    26    3.01 3.01%   
## 3 PVA3  Không d<U+1ED3>ng ý    13    1.51 1.51%
# For displaying percent of "Quite Disagree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 kh\u00F4ng \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text3
df_for_text3
## # A tibble: 3 x 5
##   BPE   Res                  n percent bar_text
##   <fct> <fct>            <int>   <dbl> <chr>   
## 1 PVA1  Khá không d<U+1ED3>ng ý    18    2.09 2.09%   
## 2 PVA2  Khá không d<U+1ED3>ng ý    39    4.52 4.52%   
## 3 PVA3  Khá không d<U+1ED3>ng ý    21    2.43 2.43%
# For displaying percent of "Normal": 
df_odered %>% 
  filter(Res == "B\u00ECnh th\u01B0\u1EDDng") %>% 
  filter(percent >= 0) -> df_for_text4
df_for_text4
## # A tibble: 3 x 5
##   BPE   Res             n percent bar_text
##   <fct> <fct>       <int>   <dbl> <chr>   
## 1 PVA1  Bình thu<U+1EDD>ng    56    6.49 6.49%   
## 2 PVA2  Bình thu<U+1EDD>ng   101   11.7  11.7%   
## 3 PVA3  Bình thu<U+1EDD>ng    70    8.11 8.11%
# For displaying percent of "Quite Agree": 
df_odered %>% 
  filter(Res == "Kh\u00E1 \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text5
df_for_text5
## # A tibble: 3 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PVA1  Khá d<U+1ED3>ng ý   130    15.1 15.06%  
## 2 PVA2  Khá d<U+1ED3>ng ý   196    22.7 22.71%  
## 3 PVA3  Khá d<U+1ED3>ng ý   161    18.7 18.66%
# For displaying percent of "Agree": 
df_odered %>% 
  filter(Res == "\u0110\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text6
df_for_text6
## # A tibble: 3 x 5
##   BPE   Res        n percent bar_text
##   <fct> <fct>  <int>   <dbl> <chr>   
## 1 PVA1  Ð<U+1ED3>ng ý   418    48.4 48.44%  
## 2 PVA2  Ð<U+1ED3>ng ý   345    40.0 39.98%  
## 3 PVA3  Ð<U+1ED3>ng ý   415    48.1 48.09%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") %>% 
  filter(percent >= 0) -> df_for_text7
df_for_text7
## # A tibble: 3 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PVA1  R<U+1EA5>t d<U+1ED3>ng ý   228    26.4 26.42%  
## 2 PVA2  R<U+1EA5>t d<U+1ED3>ng ý   152    17.6 17.61%  
## 3 PVA3  R<U+1EA5>t d<U+1ED3>ng ý   181    21.0 20.97%
# For displaying percent of "Very Agree": 
df_odered %>% 
  filter(Res == "R\u1EA5t \u0111\u1ED3ng \u00FD") -> df_for_text
df_for_text
## # A tibble: 3 x 5
##   BPE   Res            n percent bar_text
##   <fct> <fct>      <int>   <dbl> <chr>   
## 1 PVA1  R<U+1EA5>t d<U+1ED3>ng ý   228    26.4 26.42%  
## 2 PVA2  R<U+1EA5>t d<U+1ED3>ng ý   152    17.6 17.61%  
## 3 PVA3  R<U+1EA5>t d<U+1ED3>ng ý   181    21.0 20.97%
# Ad text layers-thay đổi giá trị y để thể hiện vị trí ghi text phù hợp
gg_v + 
  geom_text(data = df_for_text1, aes(x = BPE, y = 3, label = bar_text), size = 4, color = "white", family = my_font) + 
  geom_text(data = df_for_text5, aes(x = BPE, y = 22, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text6, aes(x = BPE, y = 100-40, label = bar_text), size = 4, color = "white", family = my_font) +
  geom_text(data = df_for_text7, aes(x = BPE, y = 100-4, label = bar_text), size = 4, color = "white", family = my_font)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database