如何在一个阴谋中绘制“多个箱子的阴谋”? [英] How to plot 'multiple box plots' in one plot?
问题描述
我有以下格式的数据:
#重复,包,路线,能量水平
1, 1,1,10.0 $ b $ 1,1,2,12.3 $ b $ 1,1,3,13.8
1,2,1,2.9
1,2,2,10.1
1,2,3,11.2
...
50,99,3,0.01
现在,我想创建一个图表,显示每个数据包的每个路线在所有重复中的箱形图。因此,例如,x轴将描绘数据包和y轴的能量水平。 x轴上的第一个勾号将显示三个箱形图,其中包含三个子集的数据
subset(data,data $ packet == 1& data $ route == 1)
subset(data,data $ packet == 1& data $ route == 2)
subset(data,data $ packet == 1& amp; ; data $ route == 3)
等等。我正在使用ggplot2,我想知道是否每次都要创建boxplot并尝试将它们添加到一个或者有一个聪明的方法来做到这一点?
提前致谢!
M。
如果您使用 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))
< img src =https://i.stack.imgur.com/2nRqP.pngalt =在这里输入图片描述>
在代码的情况下,你看起来像你可能实际上想要除以两个变量(这是有点不清楚)。如果你想通过路线,然后通过数据包分割它(如你的例子似乎建议),你可以使用 facet_grid
:
print(ggplot(data,aes(repetition,energy.level))+ geom_boxplot()+ facet_grid(route〜packet))
然而,请注意,由于您有99个数据包,因此它最终会成为99个宽度的图表,因此您可能需要尝试不同的方法。
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.
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))
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.
这篇关于如何在一个阴谋中绘制“多个箱子的阴谋”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!