#Encircling points automagically Circle the dots
library(ggplot2)
library(gridExtra)
library(ggalt)
## Registered S3 methods overwritten by 'ggalt':
##   method                  from   
##   grid.draw.absoluteGrob  ggplot2
##   grobHeight.absoluteGrob ggplot2
##   grobWidth.absoluteGrob  ggplot2
##   grobX.absoluteGrob      ggplot2
##   grobY.absoluteGrob      ggplot2
#install.packages("ggalt")
d <- data.frame(x=c(1,1,2),y=c(1,2,2)*100)
d
##   x   y
## 1 1 100
## 2 1 200
## 3 2 200
gg <- ggplot(d,aes(x,y))
gg <- gg + scale_x_continuous(expand=c(0.5,1))
gg <- gg + scale_y_continuous(expand=c(0.5,1))
gg

gg + geom_encircle(s_shape=1, expand=0) + geom_point()

########################
gg + geom_encircle(s_shape=1, expand=0.1, colour="red") + geom_point()

gg + geom_encircle(s_shape=0.5, expand=0.1, colour="purple") + geom_point()

#
d; subset(d, x==1)
##   x   y
## 1 1 100
## 2 1 200
## 3 2 200
##   x   y
## 1 1 100
## 2 1 200
gg + geom_encircle(data=subset(d, x==1), colour="blue", spread=0.02) +
  geom_point()

gg +geom_encircle(data=subset(d, x==2), colour="cyan", spread=0.04) + 
  geom_point()

gg <- ggplot(mpg, aes(displ, hwy))
gg

gg + geom_encircle(data=subset(mpg, hwy>40)) + geom_point()

######################
ss <- subset(mpg,hwy>31 & displ<2)

gg + geom_encircle(data=ss, colour="blue", s_shape=0.9, expand=0.07) +
  geom_point() + geom_point(data=ss, colour="blue")

#######################
gg <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = manufacturer)) + 
  geom_encircle(aes(group = manufacturer, fill = manufacturer), 
                alpha = 0.2, show.legend = F) +
  theme_classic()
gg