在R中每行绘制回归线

时间:2014-03-10 05:21:38

标签: r drawing regression

我有以下数据。

HEIrank1

     HEI.ID X2007 X2008 X2009 X2010 X2011 X2012
1        OP  41.8 147.6  90.3  82.9 106.8  63.0
2        MO  20.0  20.8  21.1  20.9  12.6  20.6
3        SD  21.2  32.3  25.7  23.9  25.0  40.1
4        UN  51.8  39.8  19.9  20.9  21.6  22.5
5        WS  18.0  19.9  15.3  13.6  15.7  15.2
6        BF  11.5  36.9  20.0  23.2  18.2  23.8
7        ME  34.2  30.3  28.4  30.1  31.5  25.6
8        IM   7.7  18.1  20.5  14.6  17.2  17.1
9        OM  11.4  11.2  12.2  11.1  13.4  19.2
10       DC  14.3  28.7  20.1  17.0  22.3  16.2
11       OC  28.6  44.0  24.9  27.9  34.0  30.7
12       TH   7.4  10.0   5.8   8.8   8.7   8.6
13       CC  12.1  11.0  12.2  12.1  14.9  15.0
14       MM  11.7  24.2  18.4  18.6  31.9  31.7
15       MC  19.0  13.7  17.0  20.4  20.5  12.1
16       SH  11.4  24.8  26.1  12.7  19.9  25.9
17       SB  13.0  22.8  15.9  17.6  17.2   9.6
18       SN  11.5  18.6  22.9  12.0  20.3  11.6
19       ER  10.8  13.2  20.0  11.0  14.9  14.2
20       SL  44.9  21.6  21.3  26.5  17.0   8.0

我尝试遵循纪念,为每个高等教育机构绘制回归线。

year <- c(2007 ,   2008  ,  2009  ,  2010 ,   2011, 2012)

op <- as.numeric(HEIrank1[1,])
lm.r <- lm(op~year)
plot(year, op)
abline(lm.r)

我想在一张图中绘制每个大学的回归线,但我不知道如何帮助我。

1 个答案:

答案 0 :(得分:0)

这是我使用ggplot2的方法,但图表无法用很多行解释。

library(ggplot2);library(reshape2)
mdat <- melt(HEIrank1, variable.name="year")
mdat$year <- as.numeric(substring(mdat$year, 2))
ggplot(mdat, aes(year, value, colour=HEI.ID, group=HEI.ID)) +
    geom_point() + stat_smooth(se = FALSE, method="lm")

enter image description here

分面可能是更好的方法:

ggplot(mdat, aes(year, value, group=HEI.ID)) +
    geom_point() + stat_smooth(se = FALSE, method="lm") +
    facet_wrap(~HEI.ID)

enter image description here

相关问题