-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtop10pie.R
More file actions
105 lines (86 loc) · 3.69 KB
/
top10pie.R
File metadata and controls
105 lines (86 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
library('ggplot2')
library('phyloseq')
source('tax_glom_fast.R')
#source('run_this.R')
taxpal = read.csv('Genus.taxa.pal.csv', header=TRUE, row.names=1, stringsAsFactors=FALSE)
physeq = snp_physeq
## Toy with the taxonomy so that the names are a little more consistent.
newtax = gsub('^D_.__', '', unlist(tax_table(physeq)[,6]))
newtax[newtax == ''] = 'unclassified'
newtax[newtax == 'uncultured'] = 'unclassified'
newtax[newtax == 'uncultured bacterium'] = 'unclassified'
subsetted = with(sample_data(physeq), {Time %in% c(0,3) & (Group %in% c('Old','New'))})
## Tax glom (faster, cause it's slow otherwise.)
taxglom = do.call('rbind', tapply(1:nrow(otu_table(physeq)), newtax,
function(x) colSums(otu_table(physeq)[x,])))[,subsetted]
taxglom = tax_glom_fast(physeqp, 'Genus')
pieglom = do.call('cbind', tapply(1:ncol(taxglom), with(sample_data(physeq)[subsetted,], {paste(Group, Time, Location)}),
function(x) if(length(x) > 1) rowSums(taxglom[,x]) else taxglom[,x]))
## Correct by dividing by sample size.
pieglomfixed = apply(pieglom, 2, function(x) x/sum(x))
## Sort by abundance.
pieglomfixed = pieglomfixed[rev(order(rowSums(pieglomfixed))),]
pieglomhead = pieglomfixed[1:10,]
pieglomhead = rbind(pieglomhead, 1 - colSums(pieglomhead))
rownames(pieglomhead)[nrow(pieglomhead)] = 'ZZ'
rownames(pieglomhead)[rownames(pieglomhead) == 'unclassified'] = 'ZY'
taxa = sort(rownames(pieglomhead))
taxa_sub = gsub('ZY','Unclassified', taxa)
taxa_sub = gsub('ZZ','<5%', taxa_sub)
pieglomheadpal = setNames(taxpal[rownames(pieglomhead),], rownames(pieglomhead))
pieglomheadpal["ZZ"] = '#BDBDBD'
pieglomheadpal["ZY"] = '#CDCDCD'
library(ggplot2)
# Barplot
pieplots = apply(pieglomhead, 2,
function(dat)
{
df = data.frame(group=rownames(pieglomhead), value = dat)
bp = ggplot(df, aes(x="", y=value, fill = group)) +
geom_bar(width = 1, stat = "identity", color='black') +
coord_polar("y", start=0) +
theme_bw() +
theme(
axis.text.x = element_blank(),
axis.text.y = element_blank(),
legend.text=element_text(family='Arial', colour = 'black'),
legend.title=element_text(family='Arial', colour = 'black',face='bold'),
legend.background = element_blank(),
legend.key.size = unit(0.6,"line"),
legend.position = 'none',
axis.ticks = element_blank(),
axis.title.y = element_blank(), #element_text(family='serif',size=10, colour = 'black', face='bold'),
axis.title.x = element_blank(),
plot.background = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank(),
panel.grid.major = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
) +
scale_fill_manual(values = pieglomheadpal, labels = taxa_sub)
return(bp)
})
dir.create('piecharts')
lapply(1:length(pieplots),
function(n)
{
fname = paste0('piecharts/', names(pieplots)[n], '.svg')
svglite::svglite(fname)
print(pieplots[[n]])
dev.off()
})
g_legend <- function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
legend
}
plotlegend = g_legend(pieplots[[1]] + theme(legend.position = 'right'))
svglite::svglite('piecharts/legend.svg')
grid::grid.draw(plotlegend)
dev.off()