r - 从输入 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),我会看到:
- X轴,设备类型(设备栏)
- Y 轴,延迟
- “开启”延迟应该是一种颜色”
- “关闭”延迟应该是一种颜色”
- 其他命令类型不应可视化
- 请注意,并非所有设备都具有所有操作,并且有些设备有不止一个观察结果(即诺基亚 3310 有更多“开启”样本......
我认为我有更多的任务/问题。即我可能需要先对数据进行分类。
我目前的做法是这样的:
> 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 多年前的东西)。
解决方案
有什么问题boxplot
?
您可以使用stack
by 过滤的子集来"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))
推荐阅读
- angular - Angular Material Stepper 的验证
- android - 在不破坏现有小部件的情况下重命名 AppWidgetProvider
- prolog - Prolog-仅使用“+”将代数表达式拆分为单独的原子和数字列表
- c# - 在选择时使用 ComboBox.DataSource 项
- c# - 如果我有一个重复次数的数字,我如何重复一个字符来组成一个字符串?
- awk - 根据 awk 中的字段值隔离行
- sql - 如何从 SQL 数据库中的多个父类别中检索多个子类别
- twitter-bootstrap - yii1 bootstrap 大屏 > 1200
- javascript - 在 OpenLayers 5.3.0 中加载外部 geojson 文件
- java - 从 hashmap 列表中消除重复项并计算他们的支持