r - Transform data by-group -


i want know if possible apply ggplot2 transformations (of data) after grouping has been performed.

example:

here qqplot of iris species:

ggplot(iris, aes(sample=sepal.width, col=species)) +     stat_qq() +     ggtitle('qqnorm of sepal width') 

qqnorm raw sepal width

i want transform sepal.widths (x - mean(x))/sd(x):

normalize = function (x) (x - mean(x))/sd(x) ggplot(iris, aes(sample=normalize(sepal.width), col=species)) +     stat_qq() +     ggtitle('qqnorm of sepal width, normalized globally') 

qqnorm of sepal width, shifted/scaled global mean/sd

note has used global mean/sd in normalizing, not per-group mean/sd (same happens if write aes(sample=(sepal.width - mean(sepal.width))/sd(sepal.width)) rather hiding away in normalize.

question: there way apply normalize within each group (species)?

i can ddply, wondered if there elegant way apply affine transformation data in ggplot call, transformation parameters per-group.

ggplot(ddply(iris, .(species), mutate, y=normalize(sepal.width)),              aes(sample=y, col=species)) +     stat_qq() +     ggtitle('qqnorm of sepal.width, normalized within-group') 

qqnorm i'm after

you change function normalize take argent by. makes normalize function more complicated, simplifies ggplot call (compared plyr solution). see below suggestion on how define normalize.

# new normalize command normalize <- function(x, by='none'){   unsplit(lapply(unique(by), function(id) scale(x[by==id])), by) }  # global normalization ggplot(iris, aes(sample=normalize(sepal.width), col=species)) +   stat_qq() +   ggtitle('qqnorm of sepal width, normalized globally') # groupe-wise normalization ggplot(iris, aes(sample=normalize(sepal.width, by=species), col=species)) +   stat_qq() +   ggtitle('qqnorm of sepal width, normalized species') 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -