setwd('//media//datngo//Driver1//0.Downloads')
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
library(stringi)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
Sys.setenv(LANG = "vi")
survey <- read.csv('MarketingSurvey.csv',header = T)
colnames(survey) <- c('date','X1.gender','X2.age','X3.job','X4.monthly.income','X5.travel.frequency','X6.travel.purpose',
                    'X7.travel.with.whom','X8.selection.criteria','X9.take.photo','X10.photo.object','X11.photo.app',
                    'X12.photo.sharing.agree','X13.pose.confused',
                    'X14.1.spot.consult.KOLs','X14.2.spot.consult.relatives','X14.3.spot.consult.no',
                    'X15.1.pose.consult.KOLs','X15.2.pose.consult.relatives','X15.3.pose.consult.no',
                    'X16.new.photo.app.anxiety','X17.1.login','X17.2.location','X17.3.posing.guide.demand','X17.4.take.edit',
                    'X17.5.spot.searching','X17.6.personal.posing','X17.7.spot.recommenation.times',
                    'X17.8.app.connect.with.socialnetwork','X17.9.new.app.easy','X18.other.comments')

survey$X5.travel.frequency <- ifelse(survey$X5.travel.frequency == '1 - 2 lần/năm','1 - 2',
                                     ifelse(survey$X5.travel.frequency == '3 - 5 lần/năm','3 - 5', '5 +'))

survey$X4.monthly.income <- ifelse(survey$X4.monthly.income == 'Chưa có thu nhập','1/ Chưa có',
                                     ifelse(survey$X4.monthly.income == 'Dưới 2 triệu','2/ <2Tr',
                                            ifelse(survey$X4.monthly.income == 'Từ 2 - 5 triệu','3/ 2 - 5Tr',
                                                   ifelse(survey$X4.monthly.income == 'Từ 5 - 10 triệu','4/ 5 - 10Tr','5/ >10Tr'))))

survey$X6.travel.purpose <- ifelse(survey$X4.monthly.income == 'Chưa có thu nhập','1/ Chưa có',
                                   ifelse(survey$X4.monthly.income == 'Dưới 2 triệu','2/ <2Tr',
                                          ifelse(survey$X4.monthly.income == 'Từ 2 - 5 triệu','3/ 2 - 5Tr',
                                                 ifelse(survey$X4.monthly.income == 'Từ 5 - 10 triệu','4/ 5 - 10Tr','5/ >10Tr'))))
table(survey$X6.travel.purpose)
## 
## 5/ >10Tr 
##       97
survey$id <- seq.int(nrow(survey))
survey <- survey[c('id','date','X1.gender','X2.age','X3.job','X4.monthly.income','X5.travel.frequency','X6.travel.purpose',
                   'X7.travel.with.whom','X8.selection.criteria','X9.take.photo','X10.photo.object','X11.photo.app',
                   'X12.photo.sharing.agree','X13.pose.confused',
                   'X14.1.spot.consult.KOLs','X14.2.spot.consult.relatives','X14.3.spot.consult.no',
                   'X15.1.pose.consult.KOLs','X15.2.pose.consult.relatives','X15.3.pose.consult.no',
                   'X16.new.photo.app.anxiety','X17.1.login','X17.2.location','X17.3.posing.guide.demand','X17.4.take.edit',
                   'X17.5.spot.searching','X17.6.personal.posing','X17.7.spot.recommenation.times',
                   'X17.8.app.connect.with.socialnetwork','X17.9.new.app.easy','X18.other.comments')]

for (i in c('X6.travel.purpose','X7.travel.with.whom','X8.selection.criteria',
            'X10.photo.object','X11.photo.app')){
  survey[,i] <- as.character(survey[,i]);
  survey[,i] <- tolower(iconv(survey[,i], from = 'UTF-8', to = 'ASCII//TRANSLIT'));
   }

survey$X8.selection.criteria <- gsub("van hoa giai tri \\(am thuc, lich su, khu vui choi...\\)"
                                     ,"van hoa giai tri",survey$X8.selection.criteria)

survey$X6.travel.purpose <- gsub("du lich trai nghiem, tham hiem"
                                     ,"du lich trai nghiem",survey$X6.travel.purpose)

survey$X6.travel.purpose <- gsub("du lich van hoa, ton giao"
                                 ,"du lich van hoa/ton giao",survey$X6.travel.purpose)

survey$X6.travel.purpose <- gsub("du lich nghien cuu, hoc tap"
                                 ,"du lich nghien cuc/hoc tap",survey$X6.travel.purpose)


X6 <- separate_rows(survey[c('id','X6.travel.purpose')],X6.travel.purpose , sep = ', ', convert = TRUE)
X7 <- separate_rows(survey[c('id','X7.travel.with.whom')],X7.travel.with.whom , sep = ', ', convert = TRUE)
X8 <- separate_rows(survey[c('id','X8.selection.criteria')],X8.selection.criteria , sep = ', ', convert = TRUE)
X10 <- separate_rows(survey[c('id','X10.photo.object')],X10.photo.object , sep = ', ', convert = TRUE)
X11 <- separate_rows(survey[c('id','X11.photo.app')],X11.photo.app , sep = ', ', convert = TRUE)
X6_sum <- data.table(X6)[,.(No.of.answer = .N),.(X6.travel.purpose)]
X7_sum <- data.table(X7)[,.(No.of.answer = .N),.(X7.travel.with.whom)]
X8_sum <- data.table(X8)[,.(No.of.answer = .N),.(X8.selection.criteria)]
X9_sum <- data.table(survey)[,.(No.of.answer = .N),.(X9.take.photo)]
X10_sum <- data.table(X10)[,.(No.of.answer = .N),.(X10.photo.object)]
X11_sum <- data.table(X11)[,.(No.of.answer = .N),.(X11.photo.app)]

#### Customer profile ####
X1.plot <- ggplot(data = survey, aes(x="", fill=X1.gender, xmax=4, xmin=3)) +
  geom_bar(aes(y = ..count..), col = "black")+
  coord_polar("y") + theme_bw()  +
  theme_void() + 
  scale_fill_brewer(palette=7,direction = 1) + 
  ggtitle("Gioi tinh khach hang" ) +
  theme(plot.title = element_text(hjust = 0.5))

X2.plot <- ggplot(data = survey, aes(x="", fill=X2.age, xmax=4, xmin=3)) +
  geom_bar(aes(y = ..count..), col = "black")+
  coord_polar("y") + theme_bw()  +
  theme_void() + 
  scale_fill_brewer(palette=5,direction = -1) + 
  ggtitle("Do tuoi khach hang" ) +
  theme(plot.title = element_text(hjust = 0.5))

X4.plot <- ggplot(data = survey, aes(x="", fill=X4.monthly.income, xmax=4, xmin=3)) +
  geom_bar(aes(y = ..count..), col = "black")+
  coord_polar("y") + theme_bw()  +
  theme_void() + 
  scale_fill_brewer(palette=4,direction = -1) + 
  ggtitle("Nghe nghiep" ) +
  theme(plot.title = element_text(hjust = 0.5))

X5.plot <- ggplot(data = survey, aes(x="", fill=X5.travel.frequency, xmax=4, xmin=3)) +
  geom_bar(aes(y = ..count..), col = "black")+
  coord_polar("y") + theme_bw()  +
  theme_void() + 
  scale_fill_brewer(palette=11,direction = -1) + 
  ggtitle("Tan suat du lich" ) +
  theme(plot.title = element_text(hjust = 0.5))

grid.arrange(X1.plot,X2.plot, X4.plot, X5.plot, ncol=2)

#### Phan tich ####

ggplot(data = X6_sum, aes(x = reorder(X6.travel.purpose, - No.of.answer), y = No.of.answer )) + 
  geom_bar(stat = 'identity', fill = 'cadetblue') + 
  labs(x = '', y = '') +
  theme_bw() + 
  geom_text(aes(label = No.of.answer)) +
  theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_text(angle = 0)) +
  scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 10)) +
  ggtitle("Muc dich du lich" ) 

ggplot(data = X7_sum, aes(x = reorder(X7.travel.with.whom, - No.of.answer), y = No.of.answer )) + 
  geom_bar(stat = 'identity', fill = 'darkgoldenrod1') + 
  labs(x = '', y = '') +
  theme_bw() + 
  geom_text(aes(label = No.of.answer)) +
  scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 10)) +
  ggtitle("Nguoi du lich cung" ) 

ggplot(data = X8_sum[X8_sum$No.of.answer > 1,], aes(x = reorder(X8.selection.criteria, - No.of.answer), y = No.of.answer )) + 
  geom_bar(stat = 'identity', fill = 'cadetblue') + 
  labs(x = '', y = '') +
  theme_bw() + 
  geom_text(aes(label = No.of.answer)) +
  theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_text(angle = 0)) +
  scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 10)) +
  ggtitle("Tieu chi lua chon" ) 

ggplot(data = X9_sum[X9_sum$No.of.answer > 3,], aes(x = reorder(X9.take.photo, - No.of.answer), y = No.of.answer )) + 
  geom_bar(stat = 'identity', fill = 'cadetblue') + 
  labs(x = '', y = '') +
  theme_bw() + 
  geom_text(aes(label = No.of.answer)) +
  theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_text(angle = 0)) +
  scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 10)) +
  ggtitle("Tan suat chup anh" ) 

ggplot(data = X10_sum, aes(x = reorder(X10.photo.object, - No.of.answer), y = No.of.answer )) + 
  geom_bar(stat = 'identity', fill = 'cadetblue') + 
  labs(x = '', y = '') +
  theme_bw() + 
  geom_text(aes(label = No.of.answer)) +
  theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_text(angle = 0)) +
  scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 10)) +
  ggtitle("Doi tuong chup anh" ) 

ggplot(data = X11_sum[X11_sum$No.of.answer > 1,], aes(x = reorder(X11.photo.app, - No.of.answer), y = No.of.answer )) + 
  geom_bar(stat = 'identity', fill = 'cadetblue') + 
  labs(x = '', y = '') +
  theme_bw() + 
  geom_text(aes(label = No.of.answer)) +
  theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_text(angle = 0)) +
  scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 10)) +
  ggtitle("Ung dung chup anh")