使用ggplot从单列中多行

Tam*_*lan 3 r ggplot2

我有一个如下所示的数据帧.我需要根据区域绘制图形,将日期作为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
Run Code Online (Sandbox Code Playgroud)

如果我根据区域重新组合上面的那个,它将如下所示.如果特定日期没有值(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
Run Code Online (Sandbox Code Playgroud)

我尝试使用下面的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 ...
Run Code Online (Sandbox Code Playgroud)

请告知此事.

And*_*rie 9

如错误消息所示,您需要指定group.像这样:

ggplot(avg_data, aes(date, AveElapsedTime, colour=region, group=region)) + 
  geom_point() + geom_line()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 消息几乎没有表示,就像在R中一样. (2认同)