简体   繁体   中英

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.

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.

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

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.

resulting boxplot graph

Here's an example using the built-in iris data frame. 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. Species is analogous to stafftype and variable is analogous to the three evaluation variables (degree, weighted.degree, eccentricity).

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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