【问题标题】:Create group smooth plot in ggplot2在 ggplot2 中创建组平滑图
【发布时间】:2017-01-31 01:26:40
【问题描述】:

我目前正在绘制一个包含 100 个人超过 10 年的图表,其中图表中的每一行代表一个人。这 100 个人也分为 4 个不同的分层组。我正在尝试使用 stat_smooth 函数为每个组创建平滑图。但是,它目前正在为每个人绘制一个平滑的图。 ggplot2 有没有办法绘制这个平滑函数?

另外,对于平滑函数,我希望使用 gam 函数并指定权重和相关类型。有没有办法在 stat_smooth 函数中做到这一点?

这是一个问题的例子:

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 中做到这一点?

【问题讨论】:

    标签: r plot ggplot2 stat smoothing


    【解决方案1】:

    如果您只是删除group = id,它将使用颜色作为id,从而满足您的需求:

    ggplot(data = D, aes(x = time, y = value, color = stratification) )+
      geom_smooth()+ 
      theme_classic()  +
      xlab("Time from index (years)") +
      ylab("value") 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      相关资源
      最近更新 更多