How to build "multiple cells" in one plot?

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 graph showing the field diagrams for one route for each packet for all repetitions. So, for example, the x axis will display the packets and the y axis will represent the energy level. The first checkmark on the x axis will display three rectangles that contain data from 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) 

etc. I use ggplot2 and I wonder if I need to create a box every time and try to add them to one or if there is a smart way to do this?

Thanks in advance! M.

+6
source share
1 answer

If you use ggplot2 , you can do it pretty well with facet_wrap , which can create multiple boxes next to each other. For instance:

 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 as if you can really split into two variables (this is a bit unclear). If you want to split it by route and then by package (as your example seems to you), 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 packages, this will have 99 columns in width, so you probably want to try a different approach.

+8
source

Source: https://habr.com/ru/post/925426/


All Articles