6

I have data in the following format:

  # repetition, packet, route, energy level
  1, 1, 1, 10.0
  1, 1, 2, 12.3
  1, 1, 3, 13.8
  1, 2, 1, 9.2
  1, 2, 2, 10.1
  1, 2, 3, 11.2
  ...
  50,99,3, 0.01

Now, I want to create a plot showing box plots per route per packet over all repetitions. So, for example the x-axis would depict the packets and the y-axis the energy level. The first tick on the x-axis would show three box plots which contain data of three subsets

  subset(data, data$packet == 1 & data$route == 1)
  subset(data, data$packet == 1 & data$route == 2)
  subset(data, data$packet == 1 & data$route == 3)

and so on. I'm using ggplot2 and I'm wondering if I have to create each time a boxplot and try to add them into one or if there is a smart way to do this?

Thanks in advance! M.

4

1 回答 1

8

如果您正在使用ggplot2,您将能够很好地做到这一点facet_wrap,它可以创建多个彼此相邻的箱线图。例如:

library(ggplot2)
mydata = data.frame(x=as.factor(rep(1:2, 5, each=5)), y=rnorm(50),
        division=rep(letters[1:5], each=10))

print(ggplot(mydata, aes(x, y)) + geom_boxplot() + facet_wrap(~division))

在此处输入图像描述

就您的代码而言,您看起来实际上可能想要除以两个变量(这有点不清楚)。如果您想按路由然后按数据包划分它(正如您的示例似乎暗示的那样),您可以使用facet_grid

print(ggplot(data, aes(repetition, energy.level)) + geom_boxplot() + facet_grid(route ~ packet))

但是,请注意,由于您有 99 个数据包,因此最终将有99 个图形宽,因此您可能想尝试不同的方法。

于 2012-09-14T16:18:42.383 回答