使用 R 和 xts 绘制 tshark 数据

graphing tshark data using R and xts

我是 R 和 xts 的新手,不知道如何编写我想要的代码。 我有一个数据集,我希望使用 R 和 xts

绘制图表

使用 tshark 和 io stat 我可以生成每个间隔的 bytes/mbps 以非常详细地分析突发周期,唯一缺少的是捕获周期的图形表示。

编辑 我注意到 mbps 列中缺少一个小数点,我现在添加了这个

数据集如下(截取);

      ms    bytes    mbps
1     0.00   1880     1.504
2     0.01      0        0
3     0.02      0        0
4     0.03      0        0
5     0.04      0        0
6     0.05      0        0
7     0.06      0        0
8     0.07      0        0
9     0.08      0        0
10    0.09      0        0
11    0.10      0        0
12    0.11      0        0
13    0.12      0        0
14    0.13      0        0
15    0.14      0        0
16    0.15      0        0
17    0.16      0        0
18    0.17      0        0
19    0.18      0        0
20    0.19      0        0
21    0.20      0        0
22    0.21   3566    2.8528
23    0.22  54160    43.328
24    0.23  15954   12.7632
25    0.24      0        0

ms 是以 10ms 为步长的间隔时间段以及该时间段的相关字节数和 mbps

我已将文件读入数据框;

mydata <- read.table("d:/pcaps/tx-stats.txt",sep=",", header=FALSE, col.names=c("ms","bytes","mbps"))

xts 似乎需要我的 ms 列是一个时间序列,但我不确定如何转换间隔周期以满足此需求。

如有任何帮助,我们将不胜感激 我想生成一系列图表,如图所示 on this site,折线图显示 mbps,条形图显示字节数。

我知道使用 chartSeries、addMACD 和 addBBand 将实现我想要的,但我终生无法弄清楚如何让 xts 读取我的数据。

** 编辑 **

这是我最后使用的代码

library(ggplot2)
library(scales)
library(gridExtra)

#dat <- head(read.table("d:/pcaps/tx-stats-1sec.txt",sep=",", header=FALSE, col.names=c("sec","bytes","mbps")),10000)
dat <- head(read.table("d:/pcaps/tx-stats-10ms.txt",sep=",", header=FALSE, col.names=c("sec","bytes","mbps")),100000)
#dat <- read.table("d:/pcaps/tx-stats.txt",sep=",", header=FALSE, col.names=c("ms","bytes","mbps"))

gg <- ggplot()
gg <- gg + ggtitle("******** WAN Mbps Transmitted") + theme(plot.title = element_text(face="bold"))
gg <- gg + geom_line(data=dat, aes(x=sec, y=mbps, colour='mbps'))
gg <- gg + geom_line(data=dat, aes(x=sec, y=mean(dat$mbps), colour='avg'))
gg <- gg + geom_line(data=dat, aes(x=sec, y=1, colour='max'))
gg <- gg + scale_x_continuous(expand=c(0,0.005))
#gg <- gg + scale_y_continuous(label=comma, limits=c(0, 1))
gg <- gg + scale_y_continuous(label=comma)
gg <- gg + theme(axis.text.y = element_text(colour='red'))
gg1 <- gg

gg <- ggplot()
gg <- gg + ggtitle("******** WAN Bytes Transferred") + theme(plot.title = element_text(face="bold"))
gg <- gg + geom_bar(data=dat, aes(x=sec, y=bytes, colour='bytes'),stat="identity")
gg <- gg + geom_line(data=dat, aes(x=sec, y=mean(dat$bytes), colour="avg"))
gg <- gg + scale_x_continuous(expand=c(0,0))
gg <- gg + scale_y_continuous(label=comma)
gg <- gg + theme(axis.text.y = element_text(colour='red'))
gg2 <- gg

grid.arrange(gg1, gg2, ncol=1, heights=c(0.65, 0.35))

mean(dat$mbps)
summary(dat$bytes)
summary(dat$mbps)

你可以做这样的事情而不用担心 xts 最初:

library(ggplot2)
library(scales)
library(gridExtra)

gg <- ggplot()
gg <- gg + geom_line(data=dat, aes(x=ms, y=mbps))
gg <- gg + scale_x_continuous(expand=c(0,0.005))
gg <- gg + scale_y_continuous(label=comma, limits=c(0, 150000))
gg1 <- gg

gg <- ggplot()
gg <- gg + geom_bar(data=dat, aes(x=ms, y=bytes), stat="identity")
gg <- gg + scale_x_continuous(expand=c(0,0))
gg <- gg + scale_y_continuous(label=comma, limits=c(0, 100000))
gg2 <- gg

grid.arrange(gg1, gg2, ncol=1, heights=c(0.7, 0.3))

我放弃了相当多的 theme 格式化,以降低复杂性,并且如果这不是您要找的东西,也不会浪费周期。可以调整大量这些组合图表,以在您的某些链接图表中获得折线图和条形视图。