Reputation: 981
I'm have a dataframe as like below. I need to graph based on region, date as x Axis and AveElapsedTime as y axis.
>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
If I rearrage the above one based on region, it will be as like below. It should not plot if there is no value(NA) for particular date.
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
I tried using the below ggplot command, I'm getting geom_path error.
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 ...
Please advise on this.
Upvotes: 3
Views: 3822
Reputation: 179398
As the error message indicates, you need to specify the group
. Like this:
ggplot(avg_data, aes(date, AveElapsedTime, colour=region, group=region)) +
geom_point() + geom_line()
Upvotes: 9