26

I'd like to plot multiple lines in R for this dataset: (x = Year, y = Value)

School_ID   Year    Value
A           1998    5
B           1998    10
C           1999    15
A           2000    7
B           2005    15

Each school has data for different years. I'd like to have one line for each school.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
dani
  • 4,880
  • 8
  • 55
  • 95

3 Answers3

34

Let's create some data:

dd = data.frame(School_ID = c("A", "B", "C", "A", "B"),
  Year = c(1998, 1998, 1999, 2000, 2005),
  Value = c(5, 10, 15, 7, 15))

Then to create a plot in base graphics, we create an initial plot of one group:

plot(dd$Year[dd$School_ID=="A"], dd$Value[dd$School_ID=="A"], type="b",
     xlim=range(dd$Year), ylim=range(dd$Value))

then iteratively add on the lines:

lines(dd$Year[dd$School_ID=="B"], dd$Value[dd$School_ID=="B"], col=2, type="b")
lines(dd$Year[dd$School_ID=="C"], dd$Value[dd$School_ID=="C"], col=3, type="b")

I've used type="b" to show the points and the lines.

Alternatively, using ggplot2:

require(ggplot2)
##The values Year, Value, School_ID are
##inherited by the geoms
ggplot(dd, aes(Year, Value,colour=School_ID)) + 
    geom_line() + 
    geom_point()
csgillespie
  • 59,189
  • 14
  • 150
  • 185
16

Is this what you want? You need group = School_id to tell ggplot2 to plot separate lines for each school. If you want the horizontal axis to incluude all years between 1998 and 2005, then remove factor in x = factor(year)

  library(ggplot2)

df = read.table(text = "School_id Year Value 
 A           1998    5
 B           1998    10
 C           1999    15
 A           2000    7
 B           2005    15", sep = "", header = TRUE)

ggplot(data = df, aes(x = factor(Year), y = Value, color = School_id)) +       
  geom_line(aes(group = School_id)) + geom_point()
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
  • I only seem to get one school plotted? – dani Apr 27 '12 at 11:05
  • I get two school line - for schools A and B. There's no line for school C because there's only one data point for school C. – Sandy Muspratt Apr 27 '12 at 11:12
  • Sorry, my fault. I meant I get one line - see image: http://i.imgur.com/s6K6e.png - when I try it on my full dataset. Any idea why? – dani Apr 27 '12 at 11:20
  • OK? I still get very odd output - see image in prev. comment. Has it got to do with interpolation between years? – dani Apr 27 '12 at 11:24
  • Sorry - my fault!! I had accidentally replaced all school IDs with a constant ... Strange I only got one line ... :P Thanks for your help. – dani Apr 27 '12 at 11:30
  • @Sandy is there a way to have palette with more than 15 colors in group ? – alily Sep 23 '16 at 12:45
4

The plot function in base R does not support grouping so you need to display your groups one by one. GGPLOT handles grouping well. I also suggest looking at Trellis XYPLOT which allows you to plot separate groups.

This is how you can create a basic grouped line plot using Trellis:

library(lattice)
rm(list = ls())     # clear objects  
graphics.off()      # close graphics windows   

test = data.frame(x =  rep(1:3, each = 2),
                  group =  rep(c("Group 1","Group 2"),3),
                  y=   c(22,8,11,4,7,5)
                 )
xyplot(y~x,
       type="b",
       group=group,
       data=test,
       auto.key =list(
         points = FALSE, 
         columns=2,
         lines = TRUE)
)
Max C
  • 2,573
  • 4
  • 23
  • 28