简体   繁体   English

R中的并排和垂直堆叠的箱形图

[英]Side-by-side and vertically stacked boxplots in R

I have 3 different populations (staff types 1, 2, and 3) that are being evaluated along 3 variables (degree, weighted.degree, eccentricity) and am looking to view these in a single set of boxplots. 我有3个不同的总体(人员类型1、2和3),正在按照3个变量(度,权重,度,偏心率)进行评估,并且希望在一组盒图中查看这些变量。

These boxplots should be placed side-by-side for each staff type for a given variable, and with 3 vertical stacked graphs by variable: something like a 3 x 3 matrix of boxplots. 对于每个人员类型,对于给定的变量,应将这些箱形图并排放置,并按变量放置3个垂直堆叠图:类似于3 x 3的箱形图矩阵。

My current code is producing the desired vertical stacking for the variables, but within each vertically stacked graph I have 1 aggregate boxplot where there should be 3 side-by-side (1 for each of the 3 staff types). 我当前的代码正在为变量生成所需的垂直堆叠,但是在每个垂直堆叠图中,我有1个聚合箱形图,其中应该有3个并排(3种职员类型中的1种)。

qp <- ggplot(data, aes(stafftype., value, fill=stafftype.))
+ geom_boxplot() + facet_grid(variable~., scales = "free_y")

Also note that each of the 3 side-be-side boxplots should be colored by stafftype, which I've been unsuccessful with. 另请注意,这3个并排的箱形图都应按人员类型上色,但我一直没有成功。

resulting boxplot graph 结果箱图

Here's an example using the built-in iris data frame. 这是使用内置iris数据帧的示例。 We remove one of the measure columns so that we'll have three instead of four and then melt the data frame so that the three measure columns are stacked into "long" format. 我们删除其中一个度量值列,以便只有三个度量值列,而不是四个度量值列,然后melt数据框,以便将三个度量值列堆叠为“长”格式。 Species is analogous to stafftype and variable is analogous to the three evaluation variables (degree, weighted.degree, eccentricity). Species类似于人员stafftypevariable类似于三个评估变量(度,加权度,偏心率)。

library(reshape2)

ggplot(melt(iris[,-1], id.var="Species")) +
  geom_boxplot(aes(Species, value, colour=Species), show.legend=FALSE) +
  facet_grid(variable ~ .) +
  theme_bw()

在此处输入图片说明

If you want each plot in a separate panel, you can do this: 如果要将每个图放在单独的面板中,可以执行以下操作:

ggplot(melt(iris[,-1], id.var="Species")) +
  geom_boxplot(aes("", value, colour=Species), width=0.5, show.legend=FALSE) +
  facet_grid(variable ~ Species) +
  theme_bw() +
  theme(axis.ticks.x=element_blank()) +
  labs(x="")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM