plotflow (version 0.2.1)

reorder_by: Order a Factor by Numeric Variable(s)

Description

Create a new dataframe with a factor reordered (re-leveled) by numeric variable(s).

Usage

reorder_by(fact, by, data, FUN = NULL, df = TRUE)

Arguments

fact

The factor to be reordered (re-leveled).

by

A formula to order the factor by.

data

A data.frame object.

FUN

A function to compute the summary statistics which can be applied to all data subsets.

df

logical. If TRUE a dataframe is returned. If FALSE a factor vector is returned.

Value

Returns a re-ordered (re-leveled) dataframe, factor vector, or levels.

References

The majority of this code is taken directly from Thomas Wutzler's blog post that has since been removed/redirected.

Examples

Run this code
# NOT RUN {
## EXAMPLE 1 - no aggregation ##

## Make a fake data set
dat <- aggregate(cbind(mpg, hp, disp)~carb, mtcars, mean)
dat$carb <- factor(dat$carb)

## compare levels (data set looks the same though)
dat$carb
reorder_by(carb, ~-hp + -mpg, data = dat)$carb

library(ggplot2)
## Unordered bars
ggplot(dat, aes(x=carb, y=mpg)) + 
    geom_bar(stat="identity") + 
    coord_flip()

## Ordered bars
ggplot(reorder_by(carb, ~mpg, dat), aes(x=carb, y=mpg)) + 
    geom_bar(stat="identity") + 
    coord_flip()
    
## Return just the vector with new levels
reorder_by(carb, ~-hp + -mpg, dat, df=FALSE)

## EXAMPLE 2 - with aggregation ##

mtcars2 <- reorder_by(gear, ~hp + -carb, mtcars, mean)

## Without re-leveling gear
ggplot(mtcars, aes(mpg, hp)) + 
    geom_point(aes(color=factor(cyl))) + 
    facet_grid(gear~.)

## After re-leveling gear
ggplot(mtcars2, aes(mpg, hp)) + 
    geom_point(aes(color=factor(cyl))) + 
    facet_grid(gear~.)
# }

Run the code above in your browser using DataLab