[Bioc-sig-seq] Of Mice and Lattice: twenty chromosomes and 7 automatic colours.

Martin Morgan mtmorgan at fhcrc.org
Sat Mar 13 01:00:46 CET 2010


On 03/12/2010 12:37 PM, Ivan Gregoretti wrote:
> Hello everybody,
> 
> How do you get the lattice package to use more than its 7 standard colours?
> 
> 
> Now in little more detail:
> 
> I use lattice for plotting, well, everything. Above all, I find it
> particularly useful to plot simultaneously a property segregated by
> chromosome. One such example of multi-panelled plot would be this
> 
> http://dsarkar.fhcrc.org/lattice/book/images/Figure_01_02_stdBW.png
> 
> or in a single panel like this
> 
> http://dsarkar.fhcrc.org/lattice/book/images/Figure_01_03_stdBW.png
> 
> In mouse there are 20 canonical chromosomes. Because lattice has been
> set to default to 7 standard colours, each colour is shared by three
> different chromosomes. People get confused by that. It seems that
> people follow colours rather than legends.
> 
> How do I 'reset' lattice to, say, 20 colours?

I think you're looking for col=rainbow(20) (or col.line; see
?panel.xyplot), as in

> library("BSgenome.Mmusculus.UCSC.mm9")
> chrlen <- seqlengths(Mmusculus)
> chrlen <- chrlen[!grepl("([XYM]|_rand)", names(chrlen))]
> pts <- lapply(lapply(chrlen, sample, size=1000, replace=TRUE), sort)
> data <- data.frame(Chromosome=factor(rep(names(pts),
+                                          sapply(pts, length)),
+                                      levels=names(pts)),
+                    Location=unlist(pts, use.names=FALSE),
+                    Value=rnorm(sum(sapply(pts, length))))
> xyplot(Value~Location, group=Chromosome, data,  col=rainbow(20))

I was going to suggest library(RColorBrewer) as a good source for
palettes, but most of these are restricted to at most 12 colors. Which
makes one think about why that is, and I guess it is because it becomes
difficult to distinguish twenty colors. Here's the way I usually display
genome summary data in lattice

czomeplot <-
    function(x, data=NULL, ..., plot.type=xyplot, layout, scales=list(
             x=list(at=TRUE, relation="free", axs="i")),
             strip=FALSE, strip.left=TRUE)
{
    formula <- latticeParseFormula(x, data, subscripts=FALSE)
    lvls <- levels(formula$condition[[1]])
    ## layout
    if (missing(layout))
        layout <- c(2, ceiling(length(lvls)/2))
    nCol <- layout[[1]]
    nRow <- layout[[2]]
    nPanelsPerCol <- c(nRow, nRow - (prod(layout) - length(lvls)))

    ## ordering of packets, by condition level
    if (length(lvls) < prod(layout))
        lvls <- c(lvls, rep(NA_character_, prod(layout) - length(lvls)))
    idx <- as.integer(factor(t(matrix(lvls, ncol=layout[[1]])),
                             levels=lvls))
    index.cond <- list(idx[!is.na(idx)])

    panelColMax <- with(formula, tapply(right, condition, max))
    xmax <- apply(matrix(panelColMax[idx], 2), 1, max, na.rm=TRUE)
    xrange <- mapply(c, 0, xmax, SIMPLIFY=FALSE)

    ## x-axis lengths
    scales$x$relation <- "free"
    scales$x$axs <- "i"
    scales$x$limits <- rep(xrange, nPanelsPerCol)
    at <- rep(c(TRUE, rep(list(NULL), nRow-1)), nCol)[!is.na(lvls)]
    scales$x$at <- at

    par.settings <- list(layout.widths=list(), layout.heights=list())
    xr <- sapply(xrange, get("["), 2)
    par.settings$layout.widths$panel <- xr / xr[1]
    par.settings$layout.heights$axis.panel <-
        c(1, rep(0, nRow-1))
    plot.type(x, data, layout=layout, index.cond=index.cond,
              scales=scales,
              par.settings=par.settings,
              strip=strip, strip.left=strip.left, ...)
}

and then

czomeplot(Value~Location|Chromosome, data, pch=".")

Martin

> 
> One example would be highly appreciated. John Steinbeck is excused.
> 
> Thank you,
> 
> Ivan
> 
> 
> Ivan Gregoretti, PhD
> National Institute of Diabetes and Digestive and Kidney Diseases
> National Institutes of Health
> 5 Memorial Dr, Building 5, Room 205.
> Bethesda, MD 20892. USA.
> Phone: 1-301-496-1016
> Fax: 1-301-496-9878
> 
> _______________________________________________
> Bioc-sig-sequencing mailing list
> Bioc-sig-sequencing at r-project.org
> https://stat.ethz.ch/mailman/listinfo/bioc-sig-sequencing


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



More information about the Bioc-sig-sequencing mailing list