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.

2
  • 2
    boxplot(y ~ x) will make side by side boxplots for a variable y, grouped by the unique values for x but, since this is purely a programming question, this is off topic here, so I've voted close and migrate to stackoverflow. Commented Sep 14, 2012 at 15:27
  • It is a bit more complicated. It is not only a variable y (energy) compared by x (route) but also packets (?). Commented Sep 14, 2012 at 15:45

1 Answer 1

8

If you're using ggplot2, you'll be able to do this quite nicely with facet_wrap, which can create multiple boxplots next to each other. For example:

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

enter image description here

In the case of your code, you look like you might actually want to divide by two variables (it's a little unclear). If you want to divide it by route and then by packet (as your example seems to suggest) you can use facet_grid:

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

However, note that since you have 99 packets this would end up being 99 graphs wide, so you probably want to try a different approach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.