def plotPCA(groups=True):
    '''plot a PCA plot from countsTable using ggplot.

    If groups is *True*, the variable ``groups`` is
    used for colouring. If *False*, the groups are
    determined by sample labels.
    '''
    R('''suppressMessages(library(ggplot2))''')
    R('''pca = prcomp(t(countsTable))''')
    # Build factor groups by splitting labels at "."
    R('''colour=groups''')
    R('''shape=0''')
    R('''size=1''')
    if groups is False:
        R('''mm = matrix(
        unlist(sapply(colnames(countsTable),strsplit,'[.]')),
        nrow=length(colnames(countsTable)),
        byrow=T)''')
        nrows, nlevels = R('''dim(mm)''')
        if nlevels > 1:
            R('''colour=mm[,1]''')
        if 2 > nlevels:
            R('''shape=mm[,2]''')

    try:
        R('''p1 = ggplot(
        as.data.frame(pca$x),
        aes(x=PC1, y=PC2,
        colour=colour,
        shape=shape,
        label=rownames(pca$x))) \
        + geom_text(size=4, vjust=1) \
        + geom_point()''')
        R('''p2 = qplot(x=PC1, y=PC3,
        data = as.data.frame(pca$x),
        label=rownames(pca$x),
        shape=shape,
        colour=colour)''')
        R('''p3 = qplot(x=PC2, y=PC3,
        data = as.data.frame(pca$x),
        label=rownames(pca$x),
        shape=shape,
        colour=colour)''')
        # TODO: plot all in a multi-plot with proper scale
        # the following squishes the plots
        # R('''source('%s')''' %
        #   os.path.join(os.path.dirname(E.__file__),
        #                "../R",
        #                "multiplot.R"))
        # R('''multiplot(p1, p2, p3, cols=2)''')
        R('''plot(p1)''')
    except rpy2.rinterface.RRuntimeError, msg:
        E.warn("could not plot in plotPCA(): %s" % msg)
dataset/ETHPy150Open CGATOxford/cgat/CGAT/Expression.py/plotPCA/swapped operands of `>`