Skip to content

facets work in qplot but facet_wrap produces an error in ggplot

4 messages · Loren, Jeff Newmiller, Loren Cassin Sackett

#
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.
#
You MUST put all data you plan to refer to into a data frame when using ggplot. There are a couple of ways you could do this... the easiest is to put a header line in the data file with column names. Or, you can assign a vector of new names to the names of the data frame.

names( hetshoms ) <- c( "chrom", "start.pos", "end.pos", "hets", "het_stat", "homs", "hom_stat", "indiv" )
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On March 24, 2015 3:40:11 PM PDT, Loren <sackettl at colorado.edu> wrote:
#
Thanks, Jeff.  I tried this previously by using a header in my data file
(and 'header=TRUE'), but for some reason, that did not seem to work.

Creating a 'names' vector as you suggested did solve the problem, though.

Thank you!
Loren


2015-03-24 23:19 GMT-04:00 Jeff Newmiller <jdnewmil at dcn.davis.ca.us>:

  
  
#
Perhaps you had forgotten to use header=TRUE when you read the data in.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On March 25, 2015 6:49:07 AM PDT, Loren Cassin Sackett <sackettl at colorado.edu> wrote: