
setwd("C:\\Users\\Administrator.GEOGDEPPGLAPTOP\\Documents\\New_SQL_R\\mock_data\\output_Fig")
#############################################################################
##  Figure 1
############################################################################
Ntest <- st_Dounat_all(orgmock_poly,orgmock_k1_5_k2_12_tmp,orgmock_k1_5_k2_12_tmp,5,1,25000,1,TRUE,500,1,5)


##############################################################################
## Figure 2.  a) r_min calculated for different values of k1 and 
## b) r_max calculated for different values of k2 when k1 considered as six 
##############################################################################

#import excel sheet 'k1_rmin'
pk1_rmin <- read_excel("~/New_SQL_R/Mock_data/summary_mockdata.xlsx",sheet = "k1_rmin")
pkrmin1  <- ggplot(pk1_rmin, aes(x = k1, y = rmin)) +geom_line(size = 1)
pkrmin  <- ggplot(pk1_rmin, aes(x = k1, y = rmin)) +geom_line(size = 1)
pkrmin <- pkrmin + geom_point(size = 2) +theme_light() +
  theme( text = element_text(size= 20,color = "black"),legend.title = element_blank(),
         axis.text.y = element_text(angle= 90, vjust= 1 , hjust = 1)) +
  xlab('K1')  +ggtitle('a)')+
  scale_y_continuous(name = "rmin(m)",labels = function(x) paste0(x /1000, "k"))

## import excel sheet 'k2_rmax'
pk2_rmax <- read_excel("~/New_SQL_R/Mock_data/summary_mockdata.xlsx", sheet = "k2_rmax")
pkrmax  <- ggplot(pk2_rmax, aes(x = K2, y = rmax_k1_5)) +geom_line(size = 1)
pkrmax <- pkrmax + geom_point(size = 2) +theme_light() +
  theme( text = element_text(size= 20,color = "black"),legend.title = element_blank(),
         axis.text.y = element_text(angle= 90, vjust= 1 , hjust = 1)) +
  xlab('K2')  +ggtitle('b)   k1= 5' )+
  scale_y_continuous(name = "rmax(m)",labels = function(x) paste0(x /1000, "k"))


ggarrange( pkrmin,pkrmax, ncol = 2)
ggsave("Fig2.png", units="in", width=12, height=6, dpi =600)

#############################################################################################
##  Figure 3. Rank of performance of different obfuscation method based on distribution 
##  preservation using 2D KS-test and combination of 1D KS-test with correlation 
##############################################################################################
## import excel sheet 'Peacock' 
pks_peacock <- read_excel("~/New_SQL_R/Mock_data/summary_mockdata.xlsx",sheet = "Peacock")
## rank methods based on sum of ks-test and cooretaltion alteration
pks_peacock$Rank <- NA
pks_peacock$Rank [pks_peacock$sum_abs <= 0.009] <- '1'
pks_peacock$Rank[pks_peacock$sum_abs > 0.009 & pks_peacock$sum_abs <= 0.02] <- '2'
pks_peacock$Rank[pks_peacock$sum_abs > 0.020 & pks_peacock$sum_abs <= 0.05] <- '3'
pks_peacock$Rank[pks_peacock$sum_abs > 0.05 & pks_peacock$sum_abs <= 0.1] <- '4'
pks_peacock$Rank[pks_peacock$sum_abs > 0.1] <- '5'

## rand methods based on peacock test 
pks_peacock$Rank1 <- NA
pks_peacock$Rank1 [pks_peacock$Peacock2 <= 0.009] <- '1'
pks_peacock$Rank1[pks_peacock$Peacock2 > 0.009 & pks_peacock$Peacock2 <= 0.02] <- '2'
pks_peacock$Rank1[pks_peacock$Peacock2 > 0.02 & pks_peacock$Peacock2 <= 0.05] <- '3'
pks_peacock$Rank1[pks_peacock$Peacock2 > 0.05 & pks_peacock$Peacock2 <= 0.07] <- '4'
pks_peacock$Rank1[pks_peacock$Peacock2 > 0.07] <- '5'

poly_ks_peacock <- ggplot(data= pks_peacock , 
                          aes(x= factor(newM, 
                                        levels = newM[order(Peacock2)]) , y=Peacock2,fill = Rank1)) +
  geom_bar(colour="black", stat="identity", width = 0.4, position = position_dodge() ) +
  labs(x = 'Method', y = "Peacocok") + theme_bw() +  scale_fill_grey(start = 0,end = 0.97) 

poly_ks_peacock <- poly_ks_peacock + 
  theme(axis.title.x = element_text(vjust = 2) ,
        axis.title.y = element_text(vjust = -3),
        text = element_text(size= 14, color = "black",face = 'bold'),
        axis.text.x = element_text(angle= 45, vjust= 1 , hjust = 1,
                                   color = 'black', face = 'bold', size = 14),
        axis.text.y = element_text( color = 'black', face = 'bold', size = 12),
        legend.position = "right")

poly_ks_peacock

poly_ks_sum <- ggplot(data= pks_peacock , 
                      aes(x= factor(newM, 
                                    levels = newM[order(Peacock2)]) , y= sum_abs, fill= Rank) ) +
  geom_bar(colour="black", stat="identity", width = 0.4, position = position_dodge() ) +
  labs(x = 'Method', y = "KS x,y and correlation") + 
  theme_bw() + scale_fill_grey(start = 0,end = 0.97) 

poly_ks_sum <- poly_ks_sum + 
  theme(axis.title.x = element_text(vjust = 2) ,
        axis.title.y = element_text(vjust = 0),
        text = element_text(size= 14, color = "black",face = 'bold'),
        axis.text.x = element_text(angle= 45, vjust= 1 , hjust = 1,
                                   color = 'black', face = 'bold', size = 14),
        axis.text.y = element_text( color = 'black', face = 'bold', size = 12),
        legend.position = "none")

poly_ks_sum
poly_ks_peacock

ggarrange( poly_ks_sum,poly_ks_peacock, ncol = 2,labels = c("a)", "b)")) 
ggsave("Fig3.png", units="in", width=12, height=6, dpi =600)

#######################################################################################
## Figure 4. comparison of statistical summary centroid of polygon displacement 
## for different obfuscation method
########################################################################################
## import excel sheet 'Summary'
p_st_summary <- read_excel("~/New_SQL_R/Mock_data/summary_mockdata.xlsx",sheet = "Summary")
p_st_summary <- p_st_summary[,-(1)]
p_st_summary_melt <- melt(p_st_summary,id = "NewM")
p_st_summary_melt$value <- as.numeric(p_st_summary_melt$value)

poly_st <- ggplot(data= p_st_summary_melt[p_st_summary_melt$variable != 'Q1' & p_st_summary_melt$variable != 'Q3' ,],
                  aes(x= variable, y=value ,fill = NewM ,order = NewM)) +
  geom_bar(colour="black", stat="identity", position = position_dodge() ) +
  labs( y = "replacement",x = 'statstic', fill = "Method")  + theme_bw() + 
  scale_fill_grey(start = 0, end = 1)
poly_st <- poly_st+
  theme(text = element_text(size= 14, color = "black",face = 'bold'),
        axis.text.x = element_text(),
        axis.text.y = element_text( color = 'black', face = 'bold', size = 14)
  ) 

poly_st
ggsave("Fig4.png", units="in", width=12, height=6, dpi =600)

###########################################################################################
## Figure 5. Comparison of polygon-based obfuscation methods based on a) TCA 
## ########################################################################################
## import excel sheet 'TCAf'
poly_tca <- read_excel("~/New_SQL_R/Mock_data/summary_mockdata.xlsx", sheet = "TCAf")
pl_tca <- ggplot(data= poly_tca , 
                 aes(x= factor(NewM,levels = NewM[order(Sum)]), y= Sum )) +
  geom_bar(colour="black", stat="identity", width = 0.4, position = position_dodge() ) +
  labs( y = "%") + theme_bw()

pl_tca <- ggplot(data= poly_tca , 
                 aes(x= NewM, y= Sum )) +
  geom_bar(colour="black", stat="identity", width = 0.4, position = position_dodge() ) +
  labs( y = "%") + theme_bw()

pl_tca <- pl_tca + 
  theme(#axis.title.x = element_text(vjust = 2) ,
    axis.title.y = element_text(vjust = -3),
    text = element_text(size= 14, color = "black",face = 'bold'),
    axis.text.x = element_text(angle= 25, vjust= 1 , hjust = 1,
                               color = 'black', face = 'bold', size = 14),
    axis.text.y = element_text( color = 'black', face = 'bold', size = 14),
    #axis.text.x = element_blank(),
    axis.title.x=element_blank(),
    legend.position = "none") +  ggtitle("TCA")

pl_tca

####################################################################################
##  Figure 5. Comparison of polygon-based obfuscation methods based on b) MCE
####################################################################################
## import excel sheet 'MCE'
MCE <- read_excel("~/New_SQL_R/Mock_data/summary_mockdata.xlsx",sheet = "MCE")
poly_mce <- ggplot(data=MCE, aes(x= NewM, y= MCE)) +
  geom_bar(colour="black", stat="identity", position = position_dodge(), width = 0.4 ) +
  labs( y = "%") + ylim(0,35) + theme_light()

poly_mce <- poly_mce +
  theme(axis.text.x = element_text(angle= 25, vjust= 1 , hjust = 1,
                                   color = 'black', face = 'bold', size = 14),
        axis.text.y = element_text( color = 'black', face = 'bold', size = 14),
        text = element_text(size= 14,color = "black", face = 'bold'),
        legend.position = "none",
        axis.title.x=element_blank())
poly_mce <- poly_mce +  ggtitle("MCE")

poly_mce

#########################################################################################
## Figure 5. Comparison of polygon-based obfuscation methods based on c) percentage of 
## obfuscated polygons that satisfies k-anonymity for different values of k
##########################################################################################

## import excel sheet 'PSatisfy_k'
k_anonymity <- read_excel("~/New_SQL_R/Mock_data/summary_mockdata.xlsx", sheet = "PSatisfy_k")
k_anonymity <- k_anonymity[,-(1)]
poly_K_melt <- melt(k_anonymity,id = "NewM")
poly_K_melt$value <- as.numeric(poly_K_melt$value)

poly_k <- ggplot(data= poly_K_melt , aes(x=  NewM, y=value ,fill = variable  )) +
  geom_bar(colour="black", stat="identity", position = position_dodge() ) +
  labs( y = "%") + ylim(0,100) + theme_cowplot()
poly_k <- poly_k+
  theme(text = element_text(size= 14, color = "black",face = 'bold'),
        # axis.text.x = element_text(angle= 25, vjust= 1 , hjust = 1),
        axis.text.y = element_text( color = 'black', face = 'bold', size = 14),
        axis.text.x = element_blank(),
        axis.title.x=element_blank()
  ) 
poly_k <- poly_k + ggtitle("k-anonymity ") +
  scale_fill_grey(start = 0, end = 1, 
                  labels= c('5','6','7','8','9','10','15','20','25'), name = "") + 
  theme(legend.position = "right" , legend.text = element_text(size = 14))


poly_k

#######################################################################################
## Figure 5. Comparison of polygon-based obfuscation methods based on d) percentage of 
## obfuscated polygons with displacement less than different values of d.
#########################################################################################
## import excel sheet 'Distance' 
p_dist <- read_excel("~/New_SQL_R/Mock_data/summary_mockdata.xlsx",sheet = "Distance")

p_dist_melt <- melt(p_dist,id = "NewM")

poly_d <- ggplot(data= p_dist_melt, aes(x=NewM, y=value ,fill = variable )) +
  geom_bar(colour="black", stat="identity", position = position_dodge() ) +
  labs(x = 'Method', y = "%") + ylim(0,100)  + theme_cowplot()
poly_d <- poly_d+ 
  theme(axis.title.x = element_text(vjust = 2) ,
        axis.title.y = element_text(vjust = -3),
        text = element_text(size= 14, color = "black",face = 'bold'),
        axis.text.x = element_text(angle= 15, vjust= 1 , hjust = 1,
                                   color = 'black', face = 'bold', size = 16),
        axis.text.y = element_text( color = 'black', face = 'bold', size = 16)
        #axis.text.x = element_blank(),
        #axis.title.x=element_blank(),
        #legend.position = "right")
  ) 

poly_d <- poly_d + ggtitle("Distance(m)") +
  scale_fill_grey (start = 0, end = 1,labels=c('500','1K','2k','3k','4k','5K','10K','15K',
                                               '20K','25K'), name = "") +
  theme(legend.position = "right", legend.text = element_text(size = 14))

poly_d

##########################################################################################
##   Figure 5 all together
ggarrange( ggarrange(pl_tca,poly_mce, 
                     ncol = 2, nrow = 1,labels = c("a)","b)"), 
                     common.legend = TRUE, legend = "right",
                     heights = c(1.3,1)
),
ggarrange( poly_k,poly_d, 
           nrow = 2, ncol = 1, labels = c("c)","d)"), 
           heights = c(1,1.3))
,ncol = 1,nrow= 2, heights = c(1,2.1))

ggsave("Fig5.png", units="in", width=12, height=10, dpi =600)


