简体   繁体   中英

facet_wrap fill by column

As far as I can tell facet_wrap fills by row. In the same way you can specify how to fill a matrix with byrow I was hoping you could do the same with facet_wrap. I know I could reorder the levels of a factor to plot in this maner but this seems like a bit of work if there's a shorter method that I'm overlooking.

library(ggplot2)

ggplot(mtcars, aes(x=gear,  y=mpg, fill=vs)) +
    geom_bar(position="dodge", stat="identity") + 
    facet_wrap(~ carb, ncol=2)  #fills by row

How can we fill by column?

This feature is implemented in the current development version of ggplot2 on github. This commit implements a new parameter dir of facet_wrap , so you'd simply do

## "v" for vertical or "h" for horizontal (the default)    
ggplot(...) + facet_wrap(~ carb, ncol=2, dir="v")

Note that this feature is currently not available in the version on CRAN.

It can be done by converting faceting variable into factor and then re-leveling it. In function relevel.byrow I used matrix(..., byrow=T) for level ordering, then converted this matrix into a vector using c() function and then re-leveled factor.

#number of columns
nc <- 2
level.byrow <- function(vec, nc){
  fac <- factor(vec) #if it is not a factor
  mlev <- matrix(levels(fac), nrow=nc, byrow=T)
  factor(fac, levels= c(mlev))
}

library(plyr)
ggplot(transform(mtcars, rcarb=level.byrow(carb, nc)), aes(x=gear,  y=mpg, fill=vs)) +
  geom_bar(position="dodge", stat="identity") + 
  facet_wrap(~ rcarb, ncol=nc)

I used plyr for convenience, you can just simply write

mtcars$rcarb <- level.byrow(mtcars$carb, nc)

This also works when we don't have full facet structure, but gives couple warnings.

mtcars2 <- subset(mtcars, carb!=3)
ggplot(transform(mtcars2, rcarb=level.byrow(carb, nc)), aes(x=gear,  y=mpg, fill=vs)) +
  geom_bar(position="dodge", stat="identity") + 
  facet_wrap(~ rcarb, ncol=nc)

Result with carb==3 excluded:

在此输入图像描述

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