I was wondering if there is a way in ggplot2 to circle same-group points as shown in the picture below?
Note: The data is dynamic and so should be the cricle.
library(tidyverse)
pop_mean<-60
n_groups<-3
groups<-gl(n_groups, 5)
x <-rnorm(length(groups), 55, 15)
Z <-model.matrix(~groups-1)
group_means <-rnorm(n_groups, 0, 2.5)
y <- pop_mean + -.1*x + Z%*%group_means + rnorm(length(groups), 0, 0.2)
dat <- data.frame(y, groups, x)
dat %>% group_by(groups) %>% ggplot() +
aes(x, y, color = groups, shape = groups)+
geom_point()

Use stat_ellipse :
library(ggplot2)
ggplot(dat) +
aes(x, y, color = groups, shape = groups)+
geom_point() +
stat_ellipse()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With