I have data that consists of several data points for a number of individuals, and each of these individuals is from a particular study site. I'd like to plot all points, draw 95% ellipses for each individual, but then color the ellipses by study site. Unfortunately it seems that when I specify to color by site, the ellipse is drawn for the aggregated group.
The data look like this:
dat1 <- data.frame(X=rnorm(21),Y=rnorm(21),indiv_id=rep(c(1,2,3),7),group_id=rep(1,21))
dat2 <- data.frame(X=rnorm(21,5),Y=rnorm(21,5),indiv_id=rep(c(4,5,6),7),group_id=rep(2,21))
dat3 <- data.frame(X=rnorm(21,10),Y=rnorm(21,10),indiv_id=rep(c(7,8,9),7),group_id=rep(3,21))
ggdat <- rbind(dat1,dat2,dat3)
ggdat$indiv_id <- as.factor(ggdat$indiv_id)
ggdat$group_id <- as.factor(ggdat$group_id)
If I draw ellipses by individual, I can see all of the ellipses separately:
ggplot(ggdat) +
geom_point(aes(x=X, y=Y,color=indiv_id),size=1) + #
stat_ellipse(aes(x=X, y=Y,color=indiv_id),type = "norm")
but if I draw by the group, it makes just one ellipse per group:
ggplot(ggdat) +
geom_point(aes(x=X, y=Y,color=indiv_id),size=1) + #
stat_ellipse(aes(x=X, y=Y,color=group_id),type = "norm") + #, linetype = 2
theme(legend.position='none')
How can I draw all 9 ellipses but color them by group? Thanks for the help!
Explicitly define the groups:
ggplot(ggdat) +
geom_point(aes(x=X, y=Y,color=indiv_id),size=1) + #
stat_ellipse(aes(x=X, y=Y,color=group_id, group=indiv_id),type = "norm") +
theme(legend.position='none')
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