[英]facets work in qplot but facet_wrap produces an error in ggplot
I am having a perplexing problem trying to use facet_wrap in ggplot, with both my real dataset and a simplified dummy dataset. 尝试在ggplot中使用facet_wrap以及我的真实数据集和简化的虚拟数据集时,我遇到了一个困惑的问题。 I am trying to plot heterozygosity across the genome for multiple individuals, with each chromosome shown separately. 我试图绘制多个个体在基因组中的杂合性,每个染色体分别显示。
My dummy data: 我的虚拟数据:
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
My code to read in the data: 我的代码读取数据:
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)
When I try to plot the chromosomes separately in qplot, it works fine: 当我尝试在qplot中分别绘制染色体时,它可以正常工作:
testplot <- qplot(start.pos, HetRatio, facets = chrom ~ ., colour=chrom)
But when I try an analogous thing in ggplot, it does not work. 但是当我在ggplot中尝试类似的东西时,它不起作用。 The first part works fine: 第一部分工作正常:
testplot <- ggplot(hetshoms, aes(x=start.pos, y=HetRatio)) + geom_point(aes(color=chrom))
but when I try to add the facet_wrap: 但是当我尝试添加facet_wrap时:
testplot + facet_wrap(~chrom)
This produces the following error 这会产生以下错误
"Error en layout_base(data, vars, drop = drop) : At least one layer must contain all variables used for facetting" “ Error en layout_base(data,vars,drop = drop):至少一层必须包含用于构面的所有变量”
I have tried adding an (as.formula(paste)) to facet_wrap() and directly calling hetshoms$V1 but neither solves the problem. 我尝试将一个(as.formula(paste))添加到facet_wrap()并直接调用hetshoms $ V1,但都不能解决问题。
I would appreciate any suggestions for how to correct my code. 我将不胜感激任何有关如何更正我的代码的建议。
To replicate qplot
output we need facet_grid(chrom~.)
要复制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.