簡體   English   中英

facet在qplot中工作,但是facet_wrap在ggplot中產生錯誤

[英]facets work in qplot but facet_wrap produces an error in ggplot

嘗試在ggplot中使用facet_wrap以及我的真實數據集和簡化的虛擬數據集時,我遇到了一個困惑的問題。 我試圖繪制多個個體在基因組中的雜合性,每個染色體分別顯示。

我的虛擬數據:

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

我的代碼讀取數據:

    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)

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

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

但是當我嘗試添加facet_wrap時:

testplot + facet_wrap(~chrom)

這會產生以下錯誤

“ Error en layout_base(data,vars,drop = drop):至少一層必須包含用於構面的所有變量”

我嘗試將一個(as.formula(paste))添加到facet_wrap()並直接調用hetshoms $ V1,但都不能解決問題。

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

要復制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~.)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM