2016-03-03 66 views
-1

我想爲以下代碼片段構建一個R函數,該函數繪製一張圖。在R中構建函數

ERO1B <- plotCounts(dds, gene="ERO1B", intgroup="treat", transform=TRUE, returnData=TRUE) 
ERO1B$gene="ERO1B" 

SLC4A10 <- plotCounts(dds, gene="SLC4A10", intgroup="treat", transform=TRUE, returnData=TRUE) 
SLC4A10$gene="SLC4A10" 

ggplot(rbind(ERO1B,SLC4A10), aes(x=factor(treat), y=count))+geom_boxplot(aes(fill=gene))+facet_wrap(~gene, scales="free") 

如果我需要添加更多的元素,我目前做的,

ERO1B <- plotCounts(dds, gene="ERO1B", intgroup="treat", transform=TRUE, returnData=TRUE) 
ERO1B$gene="ERO1B" 

SLC4A10 <- plotCounts(dds, gene="SLC4A10", intgroup="treat", transform=TRUE, returnData=TRUE) 
SLC4A10$gene="SLC4A10" 

G6PC2 <- plotCounts(dds, gene="G6PC2", intgroup="treat", transform=TRUE, returnData=TRUE) 
G6PC2$gene="G6PC2" 

ggplot(rbind(ERO1B,SLC4A10,G6PC2), aes(x=factor(treat), y=count))+geom_boxplot(aes(fill=gene))+facet_wrap(~gene, scales="free") 

我想有一個函數,向量(任意長度的)c("ERO1B","SLC4A10","G6PC2")和迭代雖然矢量和情節:

plotGeneCounts <- function (dds, genes) { 

    for (i in 1:length(genes)) { 

    i <- plotCounts(dds, gene=genes[i], intgroup="treat", transform=TRUE, returnData=TRUE) 
    i$gene=genes[i] 
    gene_vec <- c("") 
    gene_vec <- gene_vec+i 

    } 
ggplot(rbind(gene_vec), aes(x=factor(treat), y=count))+geom_boxplot(aes(fill=gene))+facet_wrap(~gene, scales="free") 
} 

回答

1

首先:這是很難猜測出了什麼問題沒有一條錯誤信息和重複的例子。這就是爲什麼你得到了零頁的答覆迄今...

但是如果我不得不猜的話,你在做什麼錯:

plotGeneCounts <- function (dds, genes) { 

    for (i in 1:length(genes)) { 
    i <- plotCounts(dds, gene=genes[i], intgroup="treat", transform=TRUE, returnData=TRUE) 
    i$gene=genes[i] 
    if (exist("gene_vec")){ 
     gene_vec <- rbind(gene_vec,i) 
    } else { 
     gene_vec <- i 
    } 
    } 
    ggplot(gene_vec, aes(x=factor(treat), y=count))+geom_boxplot(aes(fill=gene))+facet_wrap(~gene, scales="free") 
} 

注意這並不是高效的代碼越來越多的對象(大罪在那裏)。但它可能會讓你去。