如何显示总价值

时间:2013-11-26 02:52:11

标签: r ggplot2

我是R和ggplot2的新手,用于显示从ndnSim获取的数据。当我运行流动代码时,它分别在10秒时显示HopCount 4和2,在11秒时显示4和4。但我希望它在10秒内显示为6(HopCount的总和),并且在11秒内同样显示HopCount 8的总和......依此类推。怎么做,请帮帮我。

Time    Node    AppId SeqNo Type      HopCount
10.0057 leaf-1  1     0     LastDelay        4
10.0057 leaf-1  1     0     FullDelay        4
10.0066 leaf-1  1     1     LastDelay        2
10.0066 leaf-1  1     1     FullDelay        2
11.0029 leaf-2  2     0     LastDelay        2
11.0029 leaf-2  2     0     FullDelay        2
11.0057 leaf-1  1     0     LastDelay        4
11.0057 leaf-1  1     0     FullDelay        4
11.0057 leaf-1  1     0     LastDelay        4
11.0057 leaf-1  1     0     FullDelay        4

我的原始代码是

data = read.table ("app-delays-trace.txt", header=T)
data$Node = factor (data$Node)
data$AppId <- factor(data$AppId)
data$HopCount <- data$HopCount
data$Type = factor (data$Type)

# exlude irrelevant types

data = subset (data, Type %in% c("FullDelay"))
data = subset (data, Node %in% c("leaf-1"))
data = subset (data, AppId %in% c("1"))

# combine stats from all faces

data.combined = summaryBy (. ~ Time + Node + AppId + Type, data=data, FUN=sum)
g.root <- ggplot (data.combined) +
geom_point (aes (x=Time, y=HopCount.sum, color=Type), size=0.5) +
geom_line (aes (x=Time, y=HopCount.sum, color=Type), size=0.5) +
ylab ("Hop Count")
print (g.root)
png ("app-delay-1.png", width=500, height=250)
print (g.root)
dev.off ()

1 个答案:

答案 0 :(得分:0)

你可能正在寻找aggregate。您尝试的summaryby可能与data.combined = aggregate(. ~ Time + Node + AppId + Type, data=data, FUN=sum)类似,并且基于您的口头描述,我认为您想要做

data$TimeFloor <-  floor(data$Time)
data.combined = aggregate(. ~ TimeFloor + Node + AppId + Type, data=data, FUN=sum)

输出:

> data.combined
  TimeFloor   Node    Time AppId SeqNo Type HopCount
1        10 leaf-1 20.0123     2     1    2        6
2        11 leaf-1 22.0114     2     0    2        8