fill_by_column: Fill facet_wrap By Column

Description Usage Arguments Author(s) References Examples

Description

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

Usage

1
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

Author(s)

jem77bfp (stackoverflow.com) and Tyler Rinker <tyler.rinker@gmail.com>

References

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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()

trinker/plotflow documentation built on May 31, 2019, 9:42 p.m.