List plot

Goal: density plots

Example data

## Toy names
sampleNames <- letters[1:25]

## Up
x <- runif(25)
names(x) <- sampleNames
y <- runif(25)
names(y) <- sampleNames
up <- list(x, y)
up
## [[1]]
##       a       b       c       d       e       f       g       h       i 
## 0.17877 0.74746 0.71735 0.63840 0.15473 0.22529 0.25960 0.23405 0.96519 
##       j       k       l       m       n       o       p       q       r 
## 0.08523 0.93595 0.07773 0.64966 0.32587 0.16515 0.04415 0.92307 0.59742 
##       s       t       u       v       w       x       y 
## 0.32062 0.76126 0.55135 0.51342 0.55463 0.18196 0.58405 
## 
## [[2]]
##        a        b        c        d        e        f        g        h 
## 0.479969 0.291134 0.271649 0.193868 0.326098 0.173215 0.523364 0.897946 
##        i        j        k        l        m        n        o        p 
## 0.102661 0.080840 0.127808 0.007419 0.425524 0.851470 0.007024 0.480347 
##        q        r        s        t        u        v        w        x 
## 0.225795 0.979052 0.111351 0.749865 0.069552 0.064184 0.554086 0.027425 
##        y 
## 0.531557

## Down
x <- runif(25)
names(x) <- sampleNames
y <- runif(25)
names(y) <- sampleNames
down <- list(x, y)
down
## [[1]]
##        a        b        c        d        e        f        g        h 
## 0.311357 0.583660 0.939151 0.039971 0.610499 0.970330 0.785706 0.482412 
##        i        j        k        l        m        n        o        p 
## 0.222829 0.386540 0.572870 0.892910 0.290019 0.216670 0.358813 0.238079 
##        q        r        s        t        u        v        w        x 
## 0.889711 0.390324 0.905736 0.579190 0.408578 0.773900 0.465613 0.004803 
##        y 
## 0.817985 
## 
## [[2]]
##        a        b        c        d        e        f        g        h 
## 0.642546 0.506210 0.047158 0.699680 0.874357 0.001941 0.014655 0.231821 
##        i        j        k        l        m        n        o        p 
## 0.577291 0.849496 0.070803 0.928538 0.859965 0.626584 0.264543 0.517166 
##        q        r        s        t        u        v        w        x 
## 0.483690 0.256340 0.235859 0.957545 0.138517 0.037839 0.028752 0.842220 
##        y 
## 0.952517

Process data

## Up
up.dfs <- lapply(seq_len(length(up)), function(x) {
    data.frame(value = up[[x]], sampleName = names(up[[x]]), peak = rep(x, length(up[[x]])))
})
upAll <- do.call(rbind, up.dfs)
upAll$type <- rep("up", nrow(upAll))

## Down
down.dfs <- lapply(seq_len(length(down)), function(x) {
    data.frame(value = down[[x]], sampleName = names(down[[x]]), peak = rep(x, 
        length(down[[x]])))
})
downAll <- do.call(rbind, down.dfs)
downAll$type <- rep("down", nrow(downAll))

## Explore
head(upAll)
##    value sampleName peak type
## a 0.1788          a    1   up
## b 0.7475          b    1   up
## c 0.7173          c    1   up
## d 0.6384          d    1   up
## e 0.1547          e    1   up
## f 0.2253          f    1   up
head(downAll)
##     value sampleName peak type
## a 0.31136          a    1 down
## b 0.58366          b    1 down
## c 0.93915          c    1 down
## d 0.03997          d    1 down
## e 0.61050          e    1 down
## f 0.97033          f    1 down

## Usable data
data <- rbind(upAll, downAll)
data$type <- factor(data$type)

Make the plot

library("ggplot2")

## Main plot
ggplot(data, aes(x = value, colour = type)) + geom_line(stat = "density") + 
    xlab("Methylation percent") + labs(title = "Woot!")

plot of chunk plot

## Playing around a bit
library(gridExtra)
## Loading required package: grid
p1 <- ggplot(subset(data, type = "up"), aes(x = value)) + geom_line(stat = "density") + 
    xlab("Methylation percent") + labs(title = "up") + facet_grid(. ~ sampleName) + 
    scale_x_continuous(breaks = c(0.5))
p2 <- ggplot(subset(data, type = "down"), aes(x = value)) + geom_line(stat = "density") + 
    xlab("Methylation percent") + labs(title = "down") + facet_grid(. ~ sampleName) + 
    scale_x_continuous(breaks = c(0.5))
grid.arrange(p1, p2)

plot of chunk plot2

This second plot could be useful if you do not have too many samples.

Reproducibility

Sys.time()
## [1] "2013-08-15 16:17:35 EDT"
proc.time()
##    user  system elapsed 
##   6.857   0.142   7.057
sessionInfo()
## R version 3.0.1 (2013-05-16)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] grid      stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] gridExtra_0.9.1 ggplot2_0.9.3.1 knitr_1.4.1    
## 
## loaded via a namespace (and not attached):
##  [1] colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3      
##  [4] evaluate_0.4.7     formatR_0.9        gtable_0.1.2      
##  [7] labeling_0.2       MASS_7.3-28        munsell_0.4.2     
## [10] plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5
## [13] reshape2_1.2.2     scales_0.2.3       stringr_0.6.2     
## [16] tools_3.0.1