rm(list=ls())

library(ggplot2)
#Idea: simulate 3 groups  each of 20 points from a uniform
# the first group in a range x:2~8, y:4-10,
# the second group in a range x:7~11, y:8-14,
# the third group in a range x: 10~15, y:12-18,
n=30
n1=15
g1x<-matrix(runif(n=n,min=2,max=8))
g1y<-matrix(runif(n=n,min=4,max=10))
g1<-matrix(rep(1,n),ncol=1)

g2x<-matrix(runif(n=n1,min=7,max=11))
g2y<-matrix(runif(n=n1,min=8,max=14))
g2<-matrix(rep(2,n1),ncol=1)

g3x<-matrix(runif(n=n,min=10,max=15))
g3y<-matrix(runif(n=n,min=12,max=18))
g3<-matrix(rep(3,n),ncol=1)

df<-data.frame(
x=rbind(g1x,g2x,g3x),
y=rbind(g1y,g2y,g3y),
group=rbind(g1,g2,g3)
)

head(df)
##          x        y group
## 1 7.664540 7.681091     1
## 2 7.257342 4.311132     1
## 3 4.342835 7.878021     1
## 4 2.659666 7.061637     1
## 5 6.005720 8.148243     1
## 6 5.391477 4.835509     1
df$group<-as.factor(df$group)
str(df)
## 'data.frame':    75 obs. of  3 variables:
##  $ x    : num  7.66 7.26 4.34 2.66 6.01 ...
##  $ y    : num  7.68 4.31 7.88 7.06 8.15 ...
##  $ group: Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
# make a dataframe with group index


# work on ggplot three groups.




p1=ggplot(df, aes(x=x, y=y)) +geom_point() +xlab("Trait 1") + ylab("Trait 2")+theme(legend.position="none")+theme_bw()+stat_ellipse()
p1

p2=ggplot(df, aes(x=x, y=y,color=group,size=1.25)) +geom_point(aes(shape=group, color=group)) +xlab("Trait 1") + ylab("Trait 2")+theme_bw() +  theme(legend.position="none")+  stat_ellipse() +annotate(geom="text", x=4.5, y=10, label="A,B", color="black",size =8)+annotate(geom="text", x=8, y=12.5, label="C", color="black",size =8)+annotate(geom="text", x=12.5, y=18, label="D,E", color="black",size =8)+ylim(0,20)+ theme(plot.title = element_text(hjust = 0.5))+ ggtitle("Tree Dependence Data")
p2<-p2 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))


p3=ggplot(df, aes(x=x, y=y,size=1.25)) +geom_point() +xlab("Trait 1") + ylab("Trait 2")+theme_bw() +  theme(legend.position="none")+ylim(0,20)+ theme(plot.title = element_text(hjust = 0.5))+ ggtitle("Independent Data")
p3

library(gridExtra)

grid.arrange(p3,p2,ncol=2)