rno*_*ian 4 plot r ggplot2 dataframe tidyverse
我想知道是否有一种方法ggplot2可以圈出同组点,如下图所示?
注意:数据是动态的,因此标准也应该是动态的。
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()
Run Code Online (Sandbox Code Playgroud)
使用stat_ellipse:
library(ggplot2)
ggplot(dat) +
aes(x, y, color = groups, shape = groups)+
geom_point() +
stat_ellipse()
Run Code Online (Sandbox Code Playgroud)