try commenting out [[
[don.git] / posts / plotting_ethnic_regions.mdwn
1 [[!meta title="Plotting Ethnic Regions"]]
2
3 I was asked over the weekend how to plot SNPs which are associated
4 with specific ethnicities; the following code is a really quick stab
5 at the problem using the grid graphics engine in R.
6
7 [[!sweavealike fig=1 echo=1 results="hide" code="""
8 require(grid)
9 snp.position <- 1:10
10 snp.integers <- sample.int(5,size=length(snp.position),replace=TRUE)
11 ### the position is really the midpoint of the range
12 snp.midpoint <- snp.position
13 ### start of the SNP; we're assuming it should start at 0.
14 snp.start <- c(0,snp.position[-1]-diff(snp.position)/2)
15 ### stop of the snp; we're assuming it should stop at the last snp position
16 snp.stop <- c(snp.position[-length(snp.position)]+diff(snp.position)/2,
17         snp.position[length(snp.position)])
18 snp.width <- snp.stop - snp.start
19
20 ### these are the colors
21 possible.colors <- c("red","blue","yellow","green","purple")
22 snp.colors <- possible.colors[snp.integers]
23
24 ### this sets up the viewport that we'll plot into
25 pushViewport(viewport(height=unit(1,"npc")-unit(7,"lines"),
26 width=unit(1,"npc")-unit(7,"lines")
27         ))
28 pushViewport(dataViewport(xscale=range(c(0,snp.stop)),yscale=c(0,1)))
29
30 ### this draws a rectangle around the graph
31 grid.rect()
32 ### this sets up the x axis
33 grid.xaxis()
34 ### this labels the X axis
35 grid.text("Position on Chromosome",y=unit(-2.5,"lines"))
36
37 ### this draws all of the boxes corresponding to each SNP in the
38 ### appropriate color
39 grid.rect(x=unit(snp.start,"native"),
40         width=unit(snp.width,"native"),
41         y=unit(0.5,"native"),
42         height=unit(0.25,"native"),just=c("left","center"),
43         gp=gpar(col=snp.colors,fill=snp.colors))
44
45 ### this pops the viewport
46 popViewport(2)
47 """]]
48
49
50 [[!tag r genetics biology]]