R堆积线图与ggplot

时间:2014-11-30 01:51:31

标签: r graph ggplot2 dataframe

我有一个名为m1的数据框,类似于下面的数据框。我希望按toSL.share创建agebucket的折线图,每个time.period都有一个单独的行。我使用的是ggplot,但我一直收到错误,因为agebucket不是连续变量。

   agebucket time.period  toSL.share
1    (55,60]  X1968.2011 0.018442150
2    (60,65]  X1968.2011 0.020610489
3    (65,70]  X1968.2011 0.056670699
4    (70,75]  X1968.2011 0.065512286
5    (75,80]  X1968.2011 0.105771028
6    (80,85]  X1968.2011 0.134477048
7    (85,90]  X1968.2011 0.186838708
10   (55,60]  X1968.1984 0.000000000
11   (60,65]  X1968.1984 0.004602551
12   (65,70]  X1968.1984 0.003316970
13   (70,75]  X1968.1984 0.009582950
14   (75,80]  X1968.1984 0.024625690
15   (80,85]  X1968.1984 0.059762338
16   (85,90]  X1968.1984 0.139377908
19   (55,60]  X1985.1995 0.018040380
20   (60,65]  X1985.1995 0.009361666
21   (65,70]  X1985.1995 0.059075563
22   (70,75]  X1985.1995 0.059940681
23   (75,80]  X1985.1995 0.092601230 
24   (80,85]  X1985.1995 0.150035413
25   (85,90]  X1985.1995 0.153794013
28   (55,60]  X1996.2011 0.028367128
29   (60,65]  X1996.2011 0.038737821
30   (65,70]  X1996.2011 0.098084541
31   (70,75]  X1996.2011 0.111219309
32   (75,80]  X1996.2011 0.169499287
33   (80,85]  X1996.2011 0.164086942
34   (85,90]  X1996.2011 0.215390644

这是我创建情节的代码:

> m1$agebucket <- as.factor(m1$agebucket)
> ggplot(m1, aes(x=agebucket, y=toSL.share, col=time.period)) + geom_line()

这是错误:

 geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

我不明白这个错误。如何使用ggplot创建此图表?

1 个答案:

答案 0 :(得分:1)

我偏向qplot所以:

qplot(data=m1, agebucket, toSL.share, col=time.period, geom=c('point','line'), group=time.period)

或者作为ggplot:

ggplot(data=m1, aes(agebucket, toSL.share,group=time.period))+geom_line(aes(col=time.period))

有关详细信息,请参阅this link