## 展設
Dc <- filter(A,StageName=="簽約" & Amount != 0 & RecordTypeId=="展設") ##只有簽約的且有金額的展設
Dc$CreatedDate = as.Date(Dc$CreatedDate)

######################################################## 裝修
Rm <- filter(A,StageName=="簽約" & Amount != 0  & RecordTypeId=="裝修") ##只有簽約的且有金額的裝修
Rm$CreatedDate = as.Date(Rm$CreatedDate)

summary(Rm)
##   AccountId             firm            Probability        Amount       
##  Length:242         Length:242         Min.   : 90.0   Min.   :    840  
##  Class :character   Class :character   1st Qu.:100.0   1st Qu.:  20000  
##  Mode  :character   Mode  :character   Median :100.0   Median : 128710  
##                                        Mean   : 99.9   Mean   : 371668  
##                                        3rd Qu.:100.0   3rd Qu.: 367253  
##                                        Max.   :100.0   Max.   :4864029  
##  Loss_Reason__c      Field5__c         Sys_Principal__c  
##  Length:242         Length:242         Length:242        
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character  
##                                                          
##                                                          
##                                                          
##   LeadSource         Field46__c        product_type__c   
##  Length:242         Length:242         Length:242        
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character  
##                                                          
##                                                          
##                                                          
##    CloseDate           StageName          Budget__c        
##  Min.   :2018-02-10   Length:242         Length:242        
##  1st Qu.:2018-07-11   Class :character   Class :character  
##  Median :2018-09-23   Mode  :character   Mode  :character  
##  Mean   :2018-10-01                                        
##  3rd Qu.:2018-12-12                                        
##  Max.   :2019-04-02                                        
##      Type            CreatedDate         RecordTypeId      
##  Length:242         Min.   :2018-05-22   Length:242        
##  Class :character   1st Qu.:2018-06-12   Class :character  
##  Mode  :character   Median :2018-08-04   Mode  :character  
##                     Mean   :2018-09-02                     
##                     3rd Qu.:2018-11-12                     
##                     Max.   :2019-04-02                     
##  oppotunityID      
##  Length:242        
##  Class :character  
##  Mode  :character  
##                    
##                    
## 
n_distinct(Rm$AccountId) #裝修 242筆 有25個不重複顧客
## [1] 25
n_distinct(Rm$firm) # 24間不重複公司
## [1] 24
#### 修正ID不同但公司名相同公司
which(Rm$firm == "和展貿易有限公司")
## [1] 55
Rm[55,2] <-  "和展貿易有限公司cowa"


###產業
Rm[which(Rm$firm == "和展貿易有限公司cowa"),10] <- "珠寶/精品"
Rm[which(Rm$firm == "亞太電信股份有限公司"),10] <-  "通訊系統"
Rm[which(Rm$firm == "台松電器販賣股份有限公司"),10]
##   [1] "三C"       "三C"       "三C"       "三C"       "三C"      
##   [6] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [11] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [16] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [21] "三C"       "三C"       "珠寶/精品" "三C"       "三C"      
##  [26] "三C"       "三C"       "三C"       "其他"      "三C"      
##  [31] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [36] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [41] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [46] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [51] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [56] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [61] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [66] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [71] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [76] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [81] "三C"       "三C"       "三C"       "三C"       "醫藥/生技"
##  [86] "三C"       "三C"       "三C"       "三C"       "三C"      
##  [91] "三C"       "三C"       "通訊系統"  "三C"       "三C"      
##  [96] "三C"       "三C"       "三C"       "三C"       "三C"      
## [101] "三C"       "其他"      "三C"       "三C"       "三C"      
## [106] "三C"       "三C"       "三C"       "三C"       "三C"      
## [111] "三C"       "三C"       "三C"       "三C"       "三C"      
## [116] "三C"       "三C"       "三C"       "三C"       "三C"      
## [121] "三C"       "三C"       "三C"       "三C"       "三C"      
## [126] "三C"       "三C"       "三C"       "三C"       "三C"      
## [131] "三C"       "三C"
In2 = Rm %>%
  group_by(product_type__c) %>%
  summarise(
    number=n(),
    money=sum(Amount),
    mean=mean(Amount),
    max=max(Amount),
    min=min(Amount))  %>% as.data.frame()

##產業分析
In = Dc %>%
  group_by(product_type__c) %>%
  summarise(
    number=n(),
    money=sum(Amount),
    mean=mean(Amount),
    max=max(Amount),
    min=min(Amount))  %>% as.data.frame()


#### 繪圖 multiplot function 
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { 
  library(grid) 
  
  # Make a list from the ... arguments and plotlist 
  plots <- c(list(...), plotlist) 
  
  numPlots = length(plots) 
  
  # If layout is NULL, then use 'cols' to determine layout 
  if (is.null(layout)) { 
    # Make the panel 
    # ncol: Number of columns of plots 
    # nrow: Number of rows needed, calculated from # of cols 
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), 
                     ncol = cols, nrow = ceiling(numPlots/cols)) 
  } 
  
  if (numPlots==1) { 
    print(plots[[1]]) 
    
  } else { 
    # Set up the page 
    grid.newpage() 
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) 
    
    # Make each plot, in the correct location 
    for (i in 1:numPlots) { 
      # Get the i,j matrix positions of the regions that contain this subplot 
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) 
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, 
                                      layout.pos.col = matchidx$col)) 
    } 
  } 
}  

##########
In$product_type__c <- as.factor(In$product_type__c)
In$type <- "展設"
In2$type <- "裝修"
rbind(In,In2) -> In3


In3[c(8,12,16,13),] -> xx
xx$number <- 0
xx$money <- 0
xx$mean <- 0
xx$max <- 0
xx$min <- 0
xx$type <- "展設"
rbind(In3,xx) -> In3
In3[3,] -> xx
xx$number <- 0
xx$money <- 0
xx$mean <- 0
xx$max <- 0
xx$min <- 0
xx$type <- "裝修"
rbind(In3,xx) -> In3

In3 %>%  ggplot(aes(fill=type)) + geom_bar(aes(x=product_type__c,y=mean),stat="identity",position = position_dodge())+theme(text=element_text(family="黑體-繁 中黑", size=14),axis.text.x= element_text(angle=90)) + 
  geom_hline(yintercept=c(250000,500000),colour="red") + xlab("品項") + ylab("平均金額")-> p1

In3 %>%  ggplot(aes(fill=type)) + geom_bar(aes(x=product_type__c,y=number),stat="identity",position = position_dodge())+theme(text=element_text(family="黑體-繁 中黑", size=14),axis.text.x= element_text(angle=90)) + xlab("品項") + ylab("次數") -> p2

p1

p2