plotflow (version 0.2.1)

fill_by_column: Fill facet_wrap By Column

Description

Alter the data set to fill ggplot2's facet_wrap by column rather than row.

Usage

fill_by_column(dataframe, fact, ncol = 2)

Arguments

dataframe

A data.frame object.

fact

The factor to be reordered (re-leveled) to fill by column.

ncol

Number of columns to make the plot

References

http://stackoverflow.com/a/12893273/1000343

Examples

Run this code
# NOT RUN {
library(ggplot2); library(reshape2)
dat <- aggregate(cbind(vs, am, gear, cyl) ~ carb, mtcars, sum)
dat$carb <- factor(dat$carb, rev(dat$carb[order(rowSums(dat[, -1]))]))
mdat <- melt(dat)

ggplot(mdat, aes(x = variable)) +
  geom_bar(stat="count", aes(fill=variable, weight = value)) +
  facet_wrap(~carb, ncol = 2) + coord_flip()

ggplot(fill_by_column(mdat, "carb"), aes(x = variable)) +
  geom_bar(stat="count", aes(fill=variable, weight = value)) +
  facet_wrap(~carb, ncol = 2) + coord_flip()

ggplot(fill_by_column(mdat, "carb", 3), aes(x = variable)) +
  geom_bar(stat="count", aes(fill=variable, weight = value)) +
  facet_wrap(~carb, ncol = 3) + coord_flip()
# }

Run the code above in your browser using DataLab