我的数据框如下所示。我需要根据区域绘制图形,日期为x轴,AveElapsedTime为y轴。
>avg_data
date region AveElapsedTime
1 2012-05-19 betasol 1372
2 2012-05-22 atpTax 1652
3 2012-06-02 betasol 1630
4 2012-06-02 atpTax 1552
5 2012-06-02 Tax 1552
6 2012-06-07 betasol 1408
7 2012-06-12 betasol 1471
8 2012-06-15 betasol 1384
9 2012-06-21 betasol 1390
10 2012-06-22 atpTax 1252
11 2012-06-23 betasol 1442
如果我基于区域重新武装上面的那个,它将如下所示。如果特定日期没有值(NA),则不应绘制。
date atpTax betasol Tax
1 2012-05-19 NA 1372 NA
2 2012-05-22 1652 NA NA
3 2012-06-02 1552 1630 1552
4 2012-06-07 NA 1408 NA
5 2012-06-12 NA 1471 NA
6 2012-06-15 NA 1384 NA
7 2012-06-21 NA 1390 NA
8 2012-06-22 1252 NA NA
9 2012-06-23 NA 1442 NA
我尝试使用下面的ggplot命令,我收到geom_path错误。
ggplot(avg_data, aes(date, AveElapsedTime)) + geom_line(aes(col=region)) + opts(axis.text.x = theme_text(angle=90, hjust=1))
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
> str(avg_data)
'data.frame': 11 obs. of 3 variables:
$ date : Factor w/ 9 levels "2012-05-19","2012-05-22",..: 1 2 3 3 3 4 5 6 7 8 ...
$ region : Factor w/ 3 levels "atpTax","betasol",..: 2 1 2 1 3 2 2 2 2 1 ...
$ AveElapsedTime: int 1372 1652 1630 1552 1552 1408 1471 1384 1390 1252 ...
请就此提出建议。
答案 0 :(得分:8)
如错误消息所示,您需要指定group
。像这样:
ggplot(avg_data, aes(date, AveElapsedTime, colour=region, group=region)) +
geom_point() + geom_line()