gpt4 book ai didi

r - 可视化每组每天的汇总统计数据

转载 作者:行者123 更新时间:2023-12-03 18:19:16 25 4
gpt4 key购买 nike

假设以下数据框,

head(df, 9)
Day variable value
1 2015-10-18 Number_Flows.minimum 401.0000
2 2015-10-18 Number_Flows.maximum 2068.0000
3 2015-10-18 Number_Flows.average 1578.9474
4 2015-10-18 Number_srcaddr.minimum 95.0000
5 2015-10-18 Number_srcaddr.maximum 292.0000
6 2015-10-18 Number_srcaddr.average 222.6316
7 2015-10-18 Number_dstaddr.minimum 65.0000
8 2015-10-18 Number_dstaddr.maximum 411.0000
9 2015-10-18 Number_dstaddr.average 202.5789

我想做的是剧情 minimum , maximum , average每个 Number_Flows , Number_srcaddr等。我宁愿让条形图显示值,但我也可以使用其他方法,只要我得到(例如,对于下面发布的可重现示例)总共 22 个图表(每天 11 个)。

我尝试了各种事情,但没有运气。
library(dplyr)
library(ggplot2)


ggplot(df %>% mutate(group = paste(Day, gsub('\\..*', '', variable), sep = '-')), aes(x = Day, y = value))+geom_bar(stat = 'identity')+facet_wrap(~group)
ggplot(df %>% mutate(group = paste(Day, gsub('\\..*', '', variable), sep = '-')), aes(x = Day, y = value))+geom_bar(stat = 'identity')+facet_wrap(~group)
ggplot(df %>% mutate(group = paste(Day, gsub('\\..*', '', variable), sep = '-')), aes(x = Day, y = value))+geom_line()+facet_wrap(~group)

数据
dput(df)
structure(list(Day = structure(c(1445115600, 1445115600, 1445115600,
1445115600, 1445115600, 1445115600, 1445115600, 1445115600, 1445115600,
1445115600, 1445115600, 1445115600, 1445115600, 1445115600, 1445115600,
1445115600, 1445115600, 1445115600, 1445115600, 1445115600, 1445115600,
1445115600, 1445115600, 1445115600, 1445115600, 1445115600, 1445115600,
1445115600, 1445115600, 1445115600, 1445115600, 1445115600, 1445115600,
1445202000, 1445202000, 1445202000, 1445202000, 1445202000, 1445202000,
1445202000, 1445202000, 1445202000, 1445202000, 1445202000, 1445202000,
1445202000, 1445202000, 1445202000, 1445202000, 1445202000, 1445202000,
1445202000, 1445202000, 1445202000, 1445202000, 1445202000, 1445202000,
1445202000, 1445202000, 1445202000, 1445202000, 1445202000, 1445202000,
1445202000, 1445202000, 1445202000), class = c("POSIXct", "POSIXt"
), tzone = ""), variable = c("Number_Flows.minimum", "Number_Flows.maximum",
"Number_Flows.average", "Number_srcaddr.minimum", "Number_srcaddr.maximum",
"Number_srcaddr.average", "Number_dstaddr.minimum", "Number_dstaddr.maximum",
"Number_dstaddr.average", "Sum_packets.minimum", "Sum_packets.maximum",
"Sum_packets.average", "Sum_duration_nannosecs.minimum", "Sum_duration_nannosecs.maximum",
"Sum_duration_nannosecs.average", "Average_Duration.minimum",
"Average_Duration.maximum", "Average_Duration.average", "Average_Bytes.minimum",
"Average_Bytes.maximum", "Average_Bytes.average", "Bytes_per_packet.minimum",
"Bytes_per_packet.maximum", "Bytes_per_packet.average", "Sum_of_Bytes.minimum",
"Sum_of_Bytes.maximum", "Sum_of_Bytes.average", "Actual_Batch_Duration_secs.minimum",
"Actual_Batch_Duration_secs.maximum", "Actual_Batch_Duration_secs.average",
"packets_per_second.minimum", "packets_per_second.maximum", "packets_per_second.average",
"Number_Flows.minimum", "Number_Flows.maximum", "Number_Flows.average",
"Number_srcaddr.minimum", "Number_srcaddr.maximum", "Number_srcaddr.average",
"Number_dstaddr.minimum", "Number_dstaddr.maximum", "Number_dstaddr.average",
"Sum_packets.minimum", "Sum_packets.maximum", "Sum_packets.average",
"Sum_duration_nannosecs.minimum", "Sum_duration_nannosecs.maximum",
"Sum_duration_nannosecs.average", "Average_Duration.minimum",
"Average_Duration.maximum", "Average_Duration.average", "Average_Bytes.minimum",
"Average_Bytes.maximum", "Average_Bytes.average", "Bytes_per_packet.minimum",
"Bytes_per_packet.maximum", "Bytes_per_packet.average", "Sum_of_Bytes.minimum",
"Sum_of_Bytes.maximum", "Sum_of_Bytes.average", "Actual_Batch_Duration_secs.minimum",
"Actual_Batch_Duration_secs.maximum", "Actual_Batch_Duration_secs.average",
"packets_per_second.minimum", "packets_per_second.maximum", "packets_per_second.average"
), value = c(401, 2068, 1578.94736842105, 95, 292, 222.631578947368,
65, 411, 202.578947368421, 4181, 130567, 33860.2631578947, 2647278,
10876533, 5438303.63157895, 1543.937984, 20335.58603, 4202.062837,
692.4193548, 77207.90476, 14689.4305788105, 231.6654261, 943.7592654,
465.315475931579, 1244970, 123223816, 19865244, 9, 30, 27.1578947368421,
179, 4352, 1265.94736842105, 609, 2352, 1578.94736842105, 89,
299, 219.105263157895, 92, 402, 193.578947368421, 1124, 60473,
19022.6842105263, 944317, 20088618, 5254959.84210526, 1550.602627,
9749.356239, 3236.99523905263, 258.9441708, 17451.96293, 5789.86937011053,
140.2998221, 717.4807734, 424.926870810526, 157697, 33505216,
9510806.21052632, 5, 30, 24.9473684210526, 114, 2179, 772.947368421053
)), .Names = c("Day", "variable", "value"), row.names = c(NA,
66L), class = "data.frame")

最佳答案

我喜欢用线条来表示一段时间的趋势,用丝带来显示值的范围。

类似于@docendo 我会 separate首先,但我会spread后:

library(tidyverse)

df %>%
separate(variable, c("type", "var"), sep = "\\.") %>%
spread(var, value) %>%
ggplot(aes(Day)) +
geom_line(aes(y = average), size = 1) +
geom_ribbon(aes(ymin = minimum, ymax = maximum), alpha = 0.2) +
facet_wrap(~type, scales = 'free_y') +
theme(axis.text.x=element_text(angle = 90, vjust = 0.5))

enter image description here

如果你有更多的日子,这看起来会更好。

关于r - 可视化每组每天的汇总统计数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42269382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com