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

Loren sackettl at colorado.edu
Tue Mar 24 23:40:11 CET 2015


Hello all,

I am having a perplexing problem trying to use facet_wrap in ggplot, with
both my real dataset and a simplified dummy dataset.  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:
testplot <- qplot(start.pos, HetRatio, facets = chrom ~ ., colour=chrom)

But when I try an analogous thing in ggplot, it does not work.
The first part works fine:
testplot <- ggplot(hetshoms, aes(x=start.pos, y=HetRatio)) +
geom_point(aes(color=chrom, alpha=1/4)) 

but when I try to add the facet_wrap:
testplot + facet_wrap(~chrom)

This produces the following error (and no plot)
"Error en layout_base(data, vars, drop = drop) : 
  At least one layer must contain all variables used for facetting"

I have tried adding an (as.formula(paste)) and directly calling hetshoms$V1
but neither solves the problem.

Can anyone please point out where I have gone wrong and how to fix my code?  

Much appreciated,
Loren






--
View this message in context: http://r.789695.n4.nabble.com/facets-work-in-qplot-but-facet-wrap-produces-an-error-in-ggplot-tp4705058.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list