3

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.

3
  • 1
    I strongly suspect that your date got turned into a factor, and that ggplot2 is now complaining because each point on the x axis is being treated as a separate group. What are the classes of the columns (i.e. results of str(avg_data))? Can you use avg_data <- transform(avg_data,date=as.Date(date)) ? Or, brute force solution, add group=region to your geom_line mapping. Commented Jul 23, 2012 at 13:58
  • It is working fine, If I transform as date. But is there a option to display all the dates which is there in the date column ? Commented Jul 23, 2012 at 14:06
  • you can probably use +scale_x_date(breaks=...) to specify this (see ?scale_x_date), or you can use group=region as suggested by me (above) and @Andrie (in his answer). Commented Jul 23, 2012 at 14:08

1 Answer 1

9

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

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

The message indicates almost nothing, as always in R.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.