【问题标题】:facets work in qplot but facet_wrap produces an error in ggplotfacets 在 qplot 中工作,但 facet_wrap 在 ggplot 中产生错误
【发布时间】:2015-03-25 01:45:53
【问题描述】:

我在尝试在 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)

这会产生以下错误

"错误 en layout_base(data, vars, drop = drop) : 至少一层 必须包含用于分面的所有变量"

我尝试在 facet_wrap() 中添加一个 (as.formula(paste)) 并直接调用 hetshoms$V1 但都没有解决问题。

对于如何更正我的代码的任何建议,我将不胜感激。

【问题讨论】:

    标签: r ggplot2 facet-wrap


    【解决方案1】:

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

    【讨论】:

    • 我真的不想复制 qplot,因为在我的真实数据集中我有 30 条染色体我想单独绘制。但似乎这个解决方案也适用于 facet_wrap。谢谢!
    猜你喜欢
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    相关资源
    最近更新 更多