Group data and plot multiple lines

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

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

提交回复
热议问题