facets work in qplot but facet_wrap does produces error in ggplot

15 views
Skip to first unread message

Loren Sackett

unread,
Mar 24, 2015, 6:01:51 PM3/24/15
to ggp...@googlegroups.com
I am having a perplexing problem with ggplot with both my real dataset and a simplified dummy dataset.  I am trying to plot heterozygosity across a genome, and have each chromosome on a separate plot.

For simplicity, my fake dataset:
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 file looks like this:
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)

If I run qplot, there is the expected output:
testplot <- qplot(start.pos, HetRatio, facets = chrom ~ ., ylim=c(0,1))

But if I try something analogous in ggplot:
testplot <- ggplot(hetshoms, aes(x=start.pos, y=HetRatio)) + geom_point()
testplot + facet_wrap(~chrom)

I receive the error:
"Error en layout_base(data, vars, drop = drop) :
  At least one layer must contain all variables used for facetting"

Can anyone explain what I have done wrong, and how to fix it?

Many thanks in advance,
Loren

Gray, Wayne

unread,
Mar 24, 2015, 7:24:05 PM3/24/15
to Loren Sackett, ggp...@googlegroups.com
Loren,

Try assigning the labels to the column names as:

names(hetshoms)[1:8]<-c("chrom", "start.pos", "end.pos", "hets", "het_stat", "homs", "hom_stat", "indiv")


And then switch:

facet_wrap(~chrom)

to

facet_grid(chrom~.)

Wayne


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Loren Cassin Sackett

unread,
Mar 24, 2015, 10:46:57 PM3/24/15
to Gray, Wayne, ggp...@googlegroups.com
Hi Wayne,

Assigning the column names as you suggested does seem to work with facet_grid, and it actually works with facet_wrap as well. Any idea why my assignment method (chrom <- hetshoms$V1) did not work, or why including a header in the file and "header=TRUE" also did not work?   

Thank you so much for this suggestion-- you are a lifesaver!

Loren


--
Loren Cassin Sackett, Ph.D.
Center for Conservation & Evolutionary Genetics
Smithsonian Institution
P.O. Box 37012, MRC 5503 
Washington, DC 20013-7012
www.colorado.edu/ebio/sackett

Doug Mitarotonda

unread,
Mar 24, 2015, 11:27:42 PM3/24/15
to Loren Cassin Sackett, Gray, Wayne, ggp...@googlegroups.com
Hey Loren,
The reason is because ggplot looks for the column names of the data frame you pass into it. In your example, `hetshoms` has columns V1-8. Wayne’s suggestion was to give the data frame legit column names so that it would successfully find the column names of `start`, `HetRatio` and `chrom`. 

As for why it works with qplot, read the ?qplot’s `data` argument and that will explain why it worked.

Cheers,
Doug

Loren Cassin Sackett

unread,
Mar 25, 2015, 8:40:26 AM3/25/15
to Doug Mitarotonda, Gray, Wayne, ggp...@googlegroups.com
But wouldn't using
hetshoms <- read.table("fakedata.txt", header=TRUE)

also give the dataframe legit column names?

--
Loren Cassin Sackett, Ph.D.
Center for Conservation & Evolutionary Genetics
Smithsonian Institution
P.O. Box 37012, MRC 5503 
Washington, DC 20013-7012
www.colorado.edu/ebio/sackett

Doug Mitarotonda

unread,
Mar 25, 2015, 11:30:14 AM3/25/15
to ggp...@googlegroups.com
Sure, as long as the txt file has a row of headers, but that does not seem to be the case from what you sent originally. If you can provide a MWE that shows how this is not working in that case, it would be easier to help you. 
Reply all
Reply to author
Forward
0 new messages