Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circling points of the same group in plot in R [duplicate]

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()

enter image description here

like image 633
rnorouzian Avatar asked Nov 15 '25 22:11

rnorouzian


1 Answers

Use stat_ellipse :

library(ggplot2)

ggplot(dat) +
  aes(x, y, color = groups, shape = groups)+
  geom_point() + 
  stat_ellipse()

enter image description here

like image 168
Ronak Shah Avatar answered Nov 17 '25 16:11

Ronak Shah



Donate For Us

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