R 在ggplot2中创建组平滑打印
我目前正在绘制一张10年内有100个人参与的图表,图中的每一行代表一个人。100名个体也被分为4个不同的分层组。我试图使用stat_smooth函数为每个组创建一个平滑图。但是,它目前正在为每个人绘制平滑的绘图。ggplot2是否有办法绘制此平滑函数 另外,对于平滑函数,我希望使用gam函数指定权重和相关类型。在stat_smooth函数中是否有这样做的方法 以下是问题的一个例子:R 在ggplot2中创建组平滑打印,r,plot,ggplot2,stat,smoothing,R,Plot,Ggplot2,Stat,Smoothing,我目前正在绘制一张10年内有100个人参与的图表,图中的每一行代表一个人。100名个体也被分为4个不同的分层组。我试图使用stat_smooth函数为每个组创建一个平滑图。但是,它目前正在为每个人绘制平滑的绘图。ggplot2是否有办法绘制此平滑函数 另外,对于平滑函数,我希望使用gam函数指定权重和相关类型。在stat_smooth函数中是否有这样做的方法 以下是问题的一个例子: set.seed(1) D = data.table(id = rep((1:100),10), value =
set.seed(1)
D = data.table(id = rep((1:100),10), value = rnorm(1000), stratification = rep(c("A","B","C","D"), 25))
setkey(D, id)
D = D[, time := 1:10, by = id]
plot = ggplot(data = D, aes(x = time, y = value, group = id, color = stratification) )+
geom_line()+
theme_classic() +
xlab("Time from index (years)") +
ylab("value")
我希望分别为A、B、C和D组创建四个平滑函数。在ggplot中有没有这样做的方法?如果您只需删除
group=id
,它将使用颜色作为id
,提供您想要的:
ggplot(data = D, aes(x = time, y = value, color = stratification) )+
geom_smooth()+
theme_classic() +
xlab("Time from index (years)") +
ylab("value")