Skip to content

Instantly share code, notes, and snippets.

@indapa
Created March 9, 2012 19:31
Show Gist options
  • Save indapa/2008237 to your computer and use it in GitHub Desktop.
Save indapa/2008237 to your computer and use it in GitHub Desktop.
Using the reshape module on R
library(reshape)
#suppose I read a file with the header lines of sample and 13 target ids
# the values represent median target coverage from an exome capture experiment
median <- read.table("median.txt", header=T)
> median
sample t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13
1 s1 10 34 31 66 34 61 40 46 117 29 55 96 33
2 s2 20 29 25 36 27 53 34 26 89 28 40 70 26
3 s3 8 31 31 66 33 57 31 36 93 36 40 74 27
4 s4 12 35 40 63 32 70 36 42 149 36 50 98 30
5 s5 12 28 23 31 31 36 36 30 66 33 45 60 26
6 s6 17 33 39 66 30 70 39 44 128 32 53 91 32
#I want to plot on the x-axis the target ids and the y axis the median coverage for each of samples
# Now the table I read has 78 elements. I want to melt the table into a data structure
# with sample, target, median. This can be down easily with the melt function
median.reshape = melt(median)
> median.reshape
sample target median
1 s1 t1 10
2 s2 t1 20
3 s3 t1 8
4 s4 t1 12
(truncated to save space)
#now plot it
xyplot(median~target, groups=sample, data=median.reshape, type="b", xlab="targets", main="my plot")
#here is a nice tutorial http://www.statmethods.net/management/reshape.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment