#' Visulize a partition on 2 dimensional space with convex hull #' #' This function plots a partition on 2 dimensional reduced space with convex hull. #' @param X Cordinates matrix of data. #' @param K Cluster labels of data. #' @return none #' @examples #' # set OTA package directory and compile C code. #' SetOTA(directory) #' set.seed(2070) #' dat = GenData(n=5000,p=2,C=4) #' # set parameters for 'kmeans' method in GenBsSamps. #' C=4 #' bs.samps = GenBsSamps(dat$X,100,"kmeans") #' # find mean partition and uncertainty statistics. #' idx = align2(bs.samps$Z,100) #' res = MeanPart(bs.samps$Z, 100, idx) #' # plot the result on two dimensional space. #' plot.part(dat$X,dat$z) # ground truth #' plot.part(dat$X,kmeans(dat$X,C)$cluster) # baseline method #' plot.part(dat$X,res$repre) # OTA #' # distance between ground truth and each partition #' WassDist(c(dat$z,kmeans(dat$X,C)$cluster,res$repre),3) #' @export plot_part2 <- function(X,K,title="",xlab="",ylab="",legend.title=""){ if(dim(X)[2]==2){ Y = data.frame(PC1=X[,1],PC2=X[,2]) } else if(dim(X)[2]>2){ Y = as.data.frame(prcomp(X)$x) } A = as.character(K) s = cbind(Y,A) %>% split(K) ch = s %>% lapply(., function(el) chull(el$PC1, el$PC2)) ch = lapply(names(ch), function(x) s[[x]][ch[[x]],]) %>% do.call(rbind, .) mu_x = sapply(1:max(K),function(z){mean(X[which(K==z),1])}) mu_y = sapply(1:max(K),function(z){mean(X[which(K==z),2])}) ggplot2::ggplot(as.data.frame(Y[,1:2]), ggplot2::aes(x=PC1, y=PC2, color = A)) + ggplot2::geom_point(shape=19, size=1, alpha=0.5) + ggplot2::geom_polygon(data = ch, ggplot2::aes(fill = A), alpha = 0.2) + ggplot2::theme_classic() + ggplot2::labs(x=xlab,y=ylab) + ggplot2::ggtitle(title) + ggplot2::theme(plot.title = ggplot2::element_text(face="bold", hjust = 0.5)) + ggplot2::guides(fill=ggplot2::guide_legend(title=legend.title), color=ggplot2::guide_legend(title=legend.title)) + ggplot2::annotate("text", x=mu_x, y=mu_y, label=1:max(K), size=4, fontface = "bold") }