comparison bin/plot_migration_rates.R @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 #!/usr/bin/env Rscript
2
3 library(argparse)
4 library(ggplot2)
5 library(gtable)
6
7 parser <- ArgumentParser()
8 parser$add_argument('-b', '--brewer', help="Specify a color brewer pallete")
9 parser$add_argument('-c', '--color-spec', help="Specify a deme -> color CSV mapping")
10 parser$add_argument('-d', '--demes', help="For help with making colors consistent, always know what all the demes are")
11 parser$add_argument('common')
12 parser$add_argument('stats')
13 parser$add_argument('output')
14 args <- parser$parse_args()
15
16 # Load shared library
17 source(args$common)
18
19
20 stats <- read.delim(args$stats,
21 stringsAsFactors=F,
22 colClasses=c(mean='numeric', lower='numeric', upper='numeric'))
23
24
25 # Filter out just the paired migration statistics
26 stats <- stats[grepl("mig", stats$statistic),]
27 stats <- subset(stats, statistic != "mig_all")
28
29
30 # Now let's split this up a bit into mig_from and mig_to
31 mig.split <- strsplit(stats$statistic, "_")
32 stats$mig_from <- as.character(lapply(mig.split, function(x) x[2]))
33 stats$mig_to <- as.character(lapply(mig.split, function(x) x[3]))
34
35 deme.factor <- factorify.deme(stats, label='mig_from', args=args)
36 stats <- deme.factor$data
37 deme.colors <- deme.factor$colors
38
39
40 # Now for let there be plots
41 gg <- ggplot(stats)
42 gg <- gg + geom_crossbar(aes(x=mig_to, y=mean, ymin=lower, ymax=upper, fill=mig_from))
43 gg <- gg + facet_grid(mig_from ~ .)
44 gg <- gg + scale_fill_manual(values=deme.colors, name="source deme")
45 #gg <- gg + scale_y_sqrt()
46 gg <- gg + labs(y="")
47 gg <- gg + theme_bw()
48 gg <- gg + xlab("target deme")
49 gg <- gg + labs(title="Migration rate comparison")
50
51
52 # Render and save output; with the right hand facet labels on the left instead...
53 g <- ggplotGrob(gg)
54 g$layout[g$layout$name == "strip-right",c("l", "r")] <- 2
55 svg(args$output, width=5, height=4.3)
56 grid.newpage()
57 grid.draw(g)
58 dev.off()
59
60