0

I have the following data.

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

I try following commends to draw regression line for each HEIs.

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

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

I want to draw to draw regression line for each college in one graph and I do not how.can you help me.

Tyler Rinker
  • 99,090
  • 56
  • 292
  • 477
Safiya Kkmha
  • 55
  • 1
  • 5
  • 1
    Make your code snippet reproducible. Use `dput(HEIrank1)`. Follow the guidelines [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – smci Mar 10 '14 at 05:25
  • If your problem is only how to plot multiple `abline` plots on one graph, see http://stackoverflow.com/questions/2564258/plot-2-graphs-in-same-plot-in-r – smci Mar 10 '14 at 05:28

1 Answers1

0

Here's my approach with ggplot2 but the graph is uninterpretable with that many lines.

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

Faceting may be a better way to got:

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

enter image description here

Tyler Rinker
  • 99,090
  • 56
  • 292
  • 477
  • 2
    Nice answer. You simultaneously showed how to do it and how pointless it is to do it. :-) – thelatemail Mar 10 '14 at 06:17
  • When I try to run commend. error appears:Error in rename(x, .base_to_ggplot, warn_missing = FALSE) : could not find function "revalue" and I install library(plyr) but it still ..I do not what mistake.Thank you very much Tyler – Safiya Kkmha Mar 10 '14 at 06:30
  • This is likely because you are using the `attach` function. Stop doing that. – Tyler Rinker Mar 10 '14 at 06:48
  • Hi Tyler..Thank you for your help. ..and can you tell me how to change the colour. ..because some colour look similar and I can't distinguished which line o which college? ?, – Safiya Kkmha Mar 12 '14 at 11:52
  • Yes you can use `http://docs.ggplot2.org/0.9.3.1/scale_manual.html` but the problem of looking similar is because you have too many lines on one graph for your purposes. Faceting is likely a much better route; see my edits. – Tyler Rinker Mar 12 '14 at 12:36