如何在R中绘制2个分组变量?

时间:2017-05-31 15:52:02

标签: r plot

如何在R中绘制此图,其中Department和Year分组,Time为x轴,Counts为y轴?并有一条线连接并着色相同的组

  Department  Year Counts  Time
1        CPD  2011     24   0
2        CPD  2011     28   1
3        CPD  2011     31   2
4        APD  2012     20   0
5        APD  2012     25   2
6        APD  2012     21   3
7        CPD  2012     30   2
8        CPD  2012     26   3
9        CPD  2012     11   5

1 个答案:

答案 0 :(得分:0)

你的意思是这样......

library(ggplot2) 
df$depYr <- paste(df$Department,df$Year,sep="_") #set a combined dept_year variable
ggplot(df,aes(x=Time,y=Counts,colour=depYr,group=depYr))+geom_line()

enter image description here