从字符中获取匹配行并绘制 ts

Get matching lines from a character and plot ts

我有一个包含日期、数量和公司等列的数据框。 我想知道如何为每个公司保留一行?

grep returns行数,请问如何才能得到完整的行?

此外,如何在一个时间序列图上绘制每个公司的这些交易量?

我找到了 plot.ts 但我做不到,因为我没有每个公司的数量,就好像我 plot.ts 我的完整数据集一样,它不会在公司之间产生差异,然后时间序列错误(单个日期有很多点)

我想要这样的情节: time series plot 但是 "websites" 有 "volumes" 而不是 "shoes,socks,lace" 有我的名字 companies/subjects

或者像那样但是有 svolumes time series plot 2

我的数据是这样的:

> head(data)
            Date Time Subject  Sscore  Smean Svscore Sdispersion Svolume  Sbuzz    Last close
1 2015-07-08 09:10:00     MMM -0.2280 0.2593 -0.2795       0.375       8 0.6026 155.430000000
2 2015-07-08 09:10:00     ACE -0.4415 0.3521 -0.0374       0.500       4 0.7200 104.460000000
3 2015-07-07 09:10:00     AES  1.9821 0.0233  1.1743       1.000       1 1.9445  13.200000000
4 2015-07-04 09:10:00     AFL -2.9335 0.0035 -0.2975       1.000       1 0.8321  61.960000000
5 2015-07-07 09:10:00     MMM  0.2977 0.2713 -0.7436       0.400       5 0.4895 155.080000000
6 2015-07-07 09:10:00     ACE -0.2331 0.3519 -0.1118       1.000       3 0.7196 103.330000000
         Company name       Date
1          3M Company 2015-07-08
2         ACE Limited 2015-07-08
3 The AES Corporation 2015-07-07
4          AFLAC Inc. 2015-07-04
5          3M Company 2015-07-07
6         ACE Limited 2015-07-07

非常感谢!

我还不是很清楚你有什么,你想要什么。没有可重现的例子是非常困难的!

我假设您想按公司和日期汇总您的数据。您可以使用 data.table 包来实现此目的:

library(data.table)
setDT(data)
newdata<-data[,.(volume=sum(Svolume)),by=.("Company name",Date)]
# Notice that you can use any other function instead of sum. mean, to mention one

一旦你有了 newdata 对象,你可以尝试用 ggplot2:

绘制它
library(ggplot2)
library(scales)
pl<-ggplot(newdata,aes(x=Date,y=volume,group=`Company name`))+geom_line()+scale_x_date(format="%d-%m-%y")
pl

这个解决方案是通用的(因为你的问题在你的问题中没有很好地定义你想要的输出),所以它可能需要一些小的调整。

我找到了怎么做,如果它可以帮助别人:

library(ggplot2)
ggplot(data)+geom_line(aes(x=data$Date, y=data$Svolume, group=data$Subject, color=data$Subject))