【问题标题】:Plot latencies from input csv从输入 csv 绘制延迟
【发布时间】:2020-12-21 12:38:22
【问题描述】:

我对数据进行了预处理,从许多 pcap 文件中“提取”了一些信息,结果如下:

device, command type, latency, device id
Nokia 3310,          turn on,  22, 1
Nokia 3310,          turn off, 12, 1
Nokia 3310,          turn on,  20, 2
Nokia 3310,          turn off, 14, 2
Nokia 3310,          turn on,  21, 3
Nokia 3310,          turn off, 19, 3
Nokia 3310,          turn on,   2, 4
Nokia 3310,          send sms, 12, 4
candle,              turn on,   5, 1
candle,              turn off,  1, 1
Nuclear power plant, turn on,  64, 1
Nuclear power plant, turn off, 32, 1
Car,                 turn on,   7, 1
Car,                 turn off,  2, 1
Car,                 fuel,     42, 1

我想做箱线图(我的意思是这样的https://www.statmethods.net/graphs/images/boxplot1.jpg),我会在哪里看到:

  • X 轴,设备类型(设备列)
  • Y 轴,延迟
  • “开启”延迟应该是一种颜色”
  • “关闭”延迟应该是一种颜色”
  • 不应显示其他命令类型
  • 请注意,并非所有设备都具有所有操作,有些设备有不止一个观察结果(即诺基亚 3310 有更多“开启”样本...

我认为我有更多的任务/问题。 IE。我可能需要先对数据进行分类。

我目前的做法是这样的:

> library(readr)
> stack <- read_csv("stack.csv")

── Column specification ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
cols(
  device = col_character(),
  `command type` = col_character(),
  latency = col_double(),
  `device id` = col_double()
)

> plot(stack$device, stack$latency)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

PS:我对 R-project 几乎一无所知(只是一些 10 多年前的其他工作)。

【问题讨论】:

  • 也许你想要一个直方图...首先使用aggregate(latency ~ device, FUN = mean, data = stack)计算频率。
  • @RomanLuštrik,您的命令有效并打印了所有操作的每个设备的平均延迟。我只需要将输入限制为两个特定操作并将其放入单个图表中。我已经尝试过hist(stack$latency[startsWith(stack$device, 'Nokia 3310')]),但这样一来,我将需要每个设备两个(两个操作)直方图,并且我有大约 10 个设备。

标签: r plot


【解决方案1】:

boxplot 有什么问题?

您可以通过使用grep 过滤"turn" 来使用stack 的子集。

stack <- read.csv("test2.csv", strip.white=TRUE)
boxplot(latency ~ device, stack[grep("^turn", stack$command.type), ],
        main="This could be your title", col="#fac194")

编辑

实际上,使用boxplotsubset= 功能可能是明智的。正如您自己发现的那样,我们可以使用add=TRUE 来覆盖多个图。为了更好地区分这些框,我们可以使用at= 参数并使用自定义axes

boxplot(latency ~ device, stack, subset=command.type == "turn on", 
        xlim=c(0.5, 4.5), boxwex=0.25, main="This could be your title", 
        col="#fac194", at=1:4 - 0.2, xaxt="n", border="#F48024")
boxplot(latency ~ device, stack, subset=command.type == "turn off", 
        boxwex=0.25, col="#ff9d9d", add=T, 
        at=1:4 + 0.2, xaxt="n", border="red")
axis(1, 1:4, labels=sort(unique(stack$device)))
legend("topleft", c("turn on", "turn off"), pt.bg=c("#fac194", "#ff9d9d"), 
       col=c("#F48024", "red"), pch=22)


数据:

stack <- structure(list(device = c("Nokia 3310", "Nokia 3310", "Nokia 3310", 
"Nokia 3310", "Nokia 3310", "Nokia 3310", "Nokia 3310", "Nokia 3310", 
"candle", "candle", "Nuclear power plant", "Nuclear power plant", 
"Car", "Car", "Car"), command.type = c("turn on", "turn off", 
"turn on", "turn off", "turn on", "turn off", "turn on", "send sms", 
"turn on", "turn off", "turn on", "turn off", "turn on", "turn off", 
"fuel"), latency = c(22L, 12L, 20L, 14L, 21L, 19L, 2L, 12L, 5L, 
1L, 64L, 32L, 7L, 2L, 42L), device.id = c(1L, 1L, 2L, 2L, 3L, 
3L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-15L))

【讨论】:

  • 嗯,我只尝试了plot(...) 命令。这似乎产生了与我预期相同的结果。要接受答案,我只需要使用“打开”命令和“关闭”过滤行并使它们出现在同一个图表中......此外,我有一些数据“N/A”,即延迟没有价值.我想忽略(过滤掉)它们。
  • 您的编辑中可能有一个逗号(或缺少过滤参数)。当我删除给定的逗号时,这会跳到我身上:Error in eval(predvars, data, env) : object 'latency' not found。 PS:看来基本上是我需要的。旁注,用startsWith代替grep可以吗?
  • @jay.st 谢谢,我几乎最终的解决方案是:boxplot(latency ~ device, stack[grep("^turn on", stack$command.type), ], main="This could be your title", col="#fac194")boxplot(latency ~ device, stack[grep("^turn off", stack$command.type), ], main="This could be your title", col="red", add=TRUE)
  • @Lubo 我喜欢您的尝试并对其进行了一些改进。
  • @Lubo 哦,是的,很抱歉没有注意到。这些框按字母顺序是sorted,所以我们使用标签。见编辑。
猜你喜欢
  • 2017-07-26
  • 1970-01-01
  • 2010-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
相关资源
最近更新 更多