Group data and plot multiple lines

前端 未结 3 370
孤街浪徒
孤街浪徒 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:58

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

提交回复
热议问题