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.
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 removefactor
inx = factor(year)
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:
Let's create some data:
Then to create a plot in base graphics, we create an initial plot of one group:
then iteratively add on the lines:
I've used
type="b"
to show the points and the lines.Alternatively, using ggplot2: