首页 > 解决方案 > 从输入 csv 绘制延迟

问题描述

我已经对数据进行了预处理,在这些数据中我从许多 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),我会看到:

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

我目前的做法是这样的:

> 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 多年前的东西)。

标签: rplot

解决方案


有什么问题boxplot

您可以使用stackby 过滤的子集来"turn"使用grep.

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

在此处输入图像描述

编辑

subset=实际上,使用 的功能可能是明智的boxplot。正如您自己发现的那样,我们可能会使用add=TRUE过度绘制多个图。为了使盒子更容易区分,我们可以使用at=参数并使用 custom 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))

推荐阅读