绘制属于数据框中特定列的数据的多条回归线

Plotting multiple regression lines that belong to data from a specific column in a dataframe

我有几个植物的数据框,其中包含对它们特征的三个测量值。

    plant_id  stem_id  stem_hei  inf_len
1       1       1       410      92
2       1       2       520     130
3       1       3       440      98
4       2       1       480     109
5       2       2       490     115
6       2       3       500     125
7       3       1       457     105
8       3       2       425      83
9       3       3       412      93
10      4       1       385     100
11      4       2       375      78
12      4       3       380      66

我为所有这些绘制了一个简单的茎高 (x=stem_hei) vs 花序长度 (y=inf_len) 图并完成了没有麻烦。 regression line plot

我想要的是为同一关系绘制三个独立的回归线。但是每个包含词干 1,2 和 3 的数据:(第 1 行:来自词干 1 的数据,第 2 行:来自词干 2 的数据,第 3 行相同)

我以为使用这个函数会 select 数据 stem_id 的值,但它没有:

  plot(tr_correl$tall_stem_hei, tr_correl$inf_len, "stem_id" == "1")

这看起来很明显,但我真的不知道如何从这里开始。预先感谢您的帮助!

您可以使用 ggplot2 包轻松完成此操作,使用 geom_smooth。您可以更改回归类型、是否显示 SE 等。使用美学 col (and/or linetype) 你可以为不同的组绘制不同的线条。

tr_correll <- data.table(stem_id=c("a","b","c"),tall_stem_hei=rnorm(9),inf_len=rnorm(9))

ggplot(tr_correll,aes(y=inf_len,x=tall_stem_hei,col=stem_id))+geom_point()+
  geom_smooth(method="lm",se=F)