Group data and plot multiple lines

前端 未结 3 369
孤街浪徒
孤街浪徒 2020-12-01 08:13

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                


        
3条回答
  •  悲哀的现实
    2020-12-01 08:50

    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)
    )
    

提交回复
热议问题