【问题标题】:Plotting two longitudinal variables against time in r在 r 中绘制两个纵向变量与时间的关系
【发布时间】:2016-02-17 13:54:29
【问题描述】:

假设我有一个包含两个纵向变量的数据(x1, x2)t 是时间(年),type 是类:

set.seed(20)
x1 = rnorm(20,5,1) 
x2 = (x1 + rnorm(20))
t = rep(c(0,1,2,3), 5)
id = rep(1:5,each = 4)
type = as.factor(c(rep(0,8), rep(1,12)))
df = data.frame(id, t, x1, x2, type)

是否可以在一个图中绘制x1x2 agnist t?实际上,我试图通过修改相关矩阵来查看x1x1 之间的关系(但这里使用 rnorm 使其变得容易)。

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    不确定您想如何处理 ID 变量,但不妨试试这个?

    require(reshape)
    df <- reshape::melt(df, id.vars = c('id', 't', 'type'))
    ggplot(df, aes(x = t, y = value, color = variable)) +
        geom_line() +
        facet_wrap(~id)
    

    【讨论】:

      猜你喜欢
      • 2015-04-17
      • 2016-01-19
      • 2019-10-24
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      相关资源
      最近更新 更多