2015-03-25 80 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