2015-03-25 79 views
0

我有一個困惑的問題,試圖在ggplot中使用facet_wrap,同時使用我的真實數據集和簡化的虛擬數據集。我試圖爲多個個體繪製整個基因組的雜合性,每個染色體單獨顯示。facet在qplot中工作,但facet_wrap在ggplot中產生錯誤

我的虛擬數據:

hetshoms <- read.table("fakedata.txt", header=F) 
    chrom <- hetshoms$V1 
    start.pos <- hetshoms$V2 
    end.pos <- hetshoms$V3 
    hets <- hetshoms$V4 
    het_stat <- hetshoms$V5 
    homs <- hetshoms$V6 
    hom_stat <- hetshoms$V7 
    indiv <- hetshoms$V8 
    HetRatio <- hets/(hets+homs) 

當我嘗試在qplot分別繪製染色體,它工作正常:

testplot <- qplot(start.pos, HetRatio, facets = chrom ~ ., colour=chrom) 

chr1 123000 124000 2 0.00002 26 0.00026 indiv1 
chr1 124000 125000 3 0.00003 12 0.00012 indiv1 
chr1 125000 126000 1 0.00001 6 0.00006 indiv1 
chr1 126000 126000 2 0.00002 14 0.00014 indiv1 
chr2 123000 124000 6 0.00006 20 0.00020 indiv1 
chr2 124000 125000 0 0.00000 12 0.00012 indiv1 
chr1 123000 124000 2 0.00002 26 0.00026 indiv2 
chr1 124000 125000 3 0.00003 12 0.00012 indiv2 
chr1 125000 126000 1 0.00001 6 0.00006 indiv2 
chr1 126000 126000 2 0.00002 14 0.00014 indiv2 
chr2 123000 124000 6 0.00006 20 0.00020 indiv2 
chr2 124000 125000 0 0.00000 12 0.00012 indiv2 

我的代碼在數據讀取

但是當我在ggplot中嘗試類似的東西時,它不起作用。 第一部分工作正常:

testplot <- ggplot(hetshoms, aes(x=start.pos, y=HetRatio)) + geom_point(aes(color=chrom)) 

,但是當我嘗試添加facet_wrap:

testplot + facet_wrap(~chrom) 

這將產生以下錯誤

「錯誤連接layout_base(數據,增值經銷商, drop = drop):至少有一個圖層 必須包含所有用於刻面的變量「

我已經嘗試添加(as.formula(paste))到facet_wrap()並直接調用hetshoms $ V1但既不能解決問題。

我將不勝感激任何關於如何更正我的代碼的建議。

回答

0

要複製qplot輸出我們需要facet_grid(chrom~.)

#data 
hetshoms <- read.table(text=" 
         chr1 123000 124000 2 0.00002 26 0.00026 indiv1 
chr1 124000 125000 3 0.00003 12 0.00012 indiv1 
         chr1 125000 126000 1 0.00001 6 0.00006 indiv1 
         chr1 126000 126000 2 0.00002 14 0.00014 indiv1 
         chr2 123000 124000 6 0.00006 20 0.00020 indiv1 
         chr2 124000 125000 0 0.00000 12 0.00012 indiv1 
         chr1 123000 124000 2 0.00002 26 0.00026 indiv2 
         chr1 124000 125000 3 0.00003 12 0.00012 indiv2 
         chr1 125000 126000 1 0.00001 6 0.00006 indiv2 
         chr1 126000 126000 2 0.00002 14 0.00014 indiv2 
         chr2 123000 124000 6 0.00006 20 0.00020 indiv2 
         chr2 124000 125000 0 0.00000 12 0.00012 indiv2 
         ",header=FALSE) 
#calculate HetRatio 
colnames(hetshoms) <- c("chrom","start.pos","end.pos","hets","hets_stat","homs","homs_stat","indiv") 
hetshoms$HetRatio <- hetshoms$hets/(hetshoms$hets+hetshoms$homs) 

#plot 
ggplot(hetshoms, aes(x=start.pos, y=HetRatio)) + 
    geom_point(aes(color=chrom)) + 
    facet_grid(chrom~.) 

enter image description here

+0

我真的不希望複製qplot,因爲在我的真實數據集我有30條染色體,我想單獨繪製。但看來這個解決方案也適用於facet_wrap。謝謝! – Loren 2015-03-27 16:55:56