data going to wrong facet

21 views
Skip to first unread message

squid

unread,
Apr 29, 2009, 1:04:21 PM4/29/09
to ggplot2
I am having a very strange problem where datapoints that belong in a
particular facet are being placed in the wrong facet. What am I doing
wrong?

Here is a small sample of the data:

file FETpvalueM_MSmin2 arm start
AGAP005564 0.001053788 2L 17101445
AGAP005917 0.011319261 2L 23651967
AGAP806034 0.030680402 2R 51229099
AGAP805412 0.032756982 2R 41180953
AGAP804604 0.036707636 2R 29819156
AGAP806746 0.036877991 2R 7610594
AGAP803378 0.038069482 2R 12744400
AGAP805600 0.040740741 2R 43763624
AGAP804582 0.04233871 2R 29692288
AGAP804331 0.043159066 2R 26150873
AGAP806732 0.045796309 2R 7252700
AGAP806795 0.047619048 2R 8997836
AGAP012296 0.005437915 3L 39836452
AGAP011219 0.00619195 3L 19474993
AGAP012218 0.018181818 3L 38976455
AGAP011764 0.020979021 3L 32993728
AGAP012311 0.028490028 3L 40079675
AGAP011177 0.03642232 3L 18246092
AGAP012230 0.037384979 3L 39159193
AGAP011249 0.046472143 3L 19999848
AGAP009774 0.003393665 3R 42791948
AGAP007833 0.038494226 3R 1524610
AGAP009029 0.045377198 3R 24311514
AGAP008535 0.046749042 3R 12029046
AGAP007852 0.05 3R 2122554
AGAP008345 0.052631579 3R 9510131
AGAP807715 0.047619048 X 4983591
AGAP807296 0.054945055 X 15861007

here's what I did:

mk<-read.table("mk.txt", header=T)
attach(mk)

qplot(start, FETpvalueM_MSmin2, facets=arm~., data=subset(mk,
FETpvalueM_MSmin2<0.05))+geom_text(label=file, size = 4, hjust = 1,
vjust = 1)

I know that any 'file" that starts with "agap8" belongs in arm 2R or
X, but this is not happening here. Instead, 'files' are being placed
in incorrect facets. Also, the "start" is totally wrong. I'm obviously
mis-using facets somehow but can't figure out how. If I do nearly the
same thing but use colour instead of facets, everything is right.

qplot(start, FETpvalueM_MSmin2, colour=arm, data=subset
(mk,FETpvalueM_MSmin2<0.05))+geom_text(label=file, size = 4, hjust =
1, vjust = 1)

No matter what I do, I am getting some warning messages that say
Warning messages:
1: In `[<-.data.frame`(`*tmp*`, gp, value = list(size = 4, hjust =
1, :
replacement element 4 has 28 rows to replace 2 rows

Thanks, again.

hadley wickham

unread,
Apr 29, 2009, 4:30:50 PM4/29/09
to squid, ggplot2
Hi squid,

Would you mind making your example fully reproducible by including the
data in a way that easy for other people to access? It makes trouble
shooting much easier.

Hadley
--
http://had.co.nz/

Winston Chang

unread,
Apr 29, 2009, 4:45:06 PM4/29/09
to hadley wickham, squid, ggplot2
This is probably very basic information for many out there, but just in case...

To write out an object in a format that can be easily cut and pasted:
> dput(mk)
  [copy the output and paste into email message]

This generates output that the other person can just copy and paste into their R console to generate the correct data structure.


On the receiving end, even if the data has been pasted in a plain text format (as squid did earlier) rather than a nice R command format from dput(), it's still possible to read it in without too much trouble.
> mk <- read.table(file=stdin(), header=T)
 [paste table text here]

However, there's no guarantee that the types of each column will be correct; if the sender wants, for example, a column of numbers to be treated as a factor, that conversion needs to be done by hand.

-Winston

squid

unread,
Apr 29, 2009, 6:05:45 PM4/29/09
to ggplot2
Hi, sorry, I didn't realize it was a hassle to cut and paste the data.
I've put a small subset of it in the 'files' section (mktestex.txt)
and here is the output that Winston suggested. I've done both because
the output looks really strange to me...maybe that's the source of my
problems.
Many thanks.


> dput(mk)
structure(list(file = structure(c(1L, 2L, 23L, 21L, 20L, 25L,
17L, 22L, 19L, 18L, 24L, 26L, 15L, 10L, 13L, 12L, 16L, 9L, 14L,
11L, 8L, 3L, 7L, 6L, 4L, 5L, 28L, 27L), .Label = c("AGAP005564",
"AGAP005917", "AGAP007833", "AGAP007852", "AGAP008345", "AGAP008535",
"AGAP009029", "AGAP009774", "AGAP011177", "AGAP011219", "AGAP011249",
"AGAP011764", "AGAP012218", "AGAP012230", "AGAP012296", "AGAP012311",
"AGAP803378", "AGAP804331", "AGAP804582", "AGAP804604", "AGAP805412",
"AGAP805600", "AGAP806034", "AGAP806732", "AGAP806746", "AGAP806795",
"AGAP807296", "AGAP807715"), class = "factor"), FETpvalueM_MSmin2 = c
(0.001053788,
0.011319261, 0.030680402, 0.032756982, 0.036707636, 0.036877991,
0.038069482, 0.040740741, 0.04233871, 0.043159066, 0.045796309,
0.047619048, 0.005437915, 0.00619195, 0.018181818, 0.020979021,
0.028490028, 0.03642232, 0.037384979, 0.046472143, 0.003393665,
0.038494226, 0.045377198, 0.046749042, 0.05, 0.052631579,
0.047619048,
0.054945055), arm = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L), .Label = c("2L", "2R", "3L", "3R", "X"), class =
"factor"),
start = c(17101445L, 23651967L, 51229099L, 41180953L, 29819156L,
7610594L, 12744400L, 43763624L, 29692288L, 26150873L, 7252700L,
8997836L, 39836452L, 19474993L, 38976455L, 32993728L, 40079675L,
18246092L, 39159193L, 19999848L, 42791948L, 1524610L, 24311514L,
12029046L, 2122554L, 9510131L, 4983591L, 15861007L)), .Names = c
("file",
"FETpvalueM_MSmin2", "arm", "start"), class = "data.frame", row.names
= c(NA,
-28L))


On Apr 29, 9:45 pm, Winston Chang <wins...@stdout.org> wrote:
> This is probably very basic information for many out there, but just in
> case...
>
> To write out an object in a format that can be easily cut and pasted:> dput(mk)
>
>   [copy the output and paste into email message]
>
> This generates output that the other person can just copy and paste into
> their R console to generate the correct data structure.
>
> On the receiving end, even if the data has been pasted in a plain text
> format (as squid did earlier) rather than a nice R command format from
> dput(), it's still possible to read it in without too much trouble.> mk <- read.table(file=stdin(), header=T)
>
>  [paste table text here]
>
> However, there's no guarantee that the types of each column will be correct;
> if the sender wants, for example, a column of numbers to be treated as a
> factor, that conversion needs to be done by hand.
>
> -Winston
>
> On Wed, Apr 29, 2009 at 3:30 PM, hadley wickham <h.wick...@gmail.com> wrote:
>
> > Hi squid,
>
> > Would you mind making your example fully reproducible by including the
> > data in a way that easy for other people to access?  It makes trouble
> > shooting much easier.
>
> > Hadley
>

Kasper Daniel Hansen

unread,
Apr 30, 2009, 12:02:44 AM4/30/09
to squid, ggplot2
SessionInfo? Are you running the latest ggplot2 version, a facet_wrap
bug was recently fixed.

Kasper

squid

unread,
Apr 30, 2009, 4:31:05 AM4/30/09
to ggplot2
Hi. I was actually using the previous ggplot2 but have now updated
both ggplot2 and plyer. Here's my info
> sessionInfo()
R version 2.9.0 Under development (unstable) (2009-02-17 r47950)
i386-apple-darwin9.6.0

locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid stats graphics grDevices utils datasets
methods
[8] base

other attached packages:
[1] ggplot2_0.8.3 reshape_0.8.2 plyr_0.1.8 proto_0.3-8

loaded via a namespace (and not attached):
[1] tools_2.9.0


I notice that R 2.9 for macs now has a stable release, so I will
update that today although I can't imagine that is the problem. In
the updated ggplot2, I am still having the bizarre problem I report
above. Again, the example file is in the files section (mktest.txt)
and when I type the following command, the data go to the wrong facets
and have the wrong "start"

qplot(start, FETpvalueM_MSmin2, data=subset(mk,
FETpvalueM_MSmin2<0.05), facets=arm~.)+geom_text(label=file, size = 4,
hjust = 1, vjust = 1,data=subset(mk, FETpvalueM_MSmin2<0.05))

but when I type this command (and identify 'arm' using colour rather
than facets) everything seems ok.

qplot(start, FETpvalueM_MSmin2, colour=arm, data=subset(mk,
FETpvalueM_MSmin2<0.05))+geom_text(label=file, size = 4, hjust = 1,
vjust = 1)

In both cases though, I get several warnings of the form:
Warning messages:
1: In `[<-.data.frame`(`*tmp*`, gp, value = list(size = 4, hjust =
1, :
replacement element 4 has 28 rows to replace 25 rows

Many thanks, once again.
Mara





On Apr 30, 5:02 am, Kasper Daniel Hansen <khan...@stat.berkeley.edu>
wrote:

hadley wickham

unread,
Apr 30, 2009, 10:01:56 AM4/30/09
to squid, ggplot2
> I notice that R 2.9 for macs now has a stable release, so I will
> update that today although I can't imagine that is the problem.  In
> the updated ggplot2, I am still having the bizarre problem I report
> above.  Again, the example file is in the files section (mktest.txt)
> and when I type the following command, the data go to the wrong facets
> and have the wrong "start"
>
> qplot(start, FETpvalueM_MSmin2, data=subset(mk,
> FETpvalueM_MSmin2<0.05), facets=arm~.)+geom_text(label=file, size = 4,
> hjust = 1, vjust = 1,data=subset(mk, FETpvalueM_MSmin2<0.05))

Try this code:

qplot(start, FETpvalueM_MSmin2, facets = arm ~ ., data = mk) +
geom_text(aes(label=file), size = 4, hjust = 1, vjust = 1) +
ylim(0, 0.05)

The problem is that you were missing the aes call in geom_text and you
had previously used attach, so ggplot2 used the file variable from the
global environment, causing the issue you saw.


Hadley


--
http://had.co.nz/

squid

unread,
Apr 30, 2009, 1:19:41 PM4/30/09
to ggplot2
Hi Hadley,

Thanks very much for this. Naturally it leads to several follow up
questions...

1. Is it generally inappropriate and/or unnecessary to "attach"? Are
there any rules of thumb for this?

2. I'm now working with this dataset using melt which going really
well for the most part. Great function! But now that I'm working with
this modified dataset, I have cases where I'd like to plot subsets of
subsets of data. The "melted" dataset I'm working with has 854701 rows
so I'm not going to paste it here but I can put it up in files if
necessary. I think the question might be straightforward enough
without having to work through it though.
In this melted dataset, there are 2 relevant descriptor terms
(measure and inSpecies) that are connected to values:

> summary(rm$measure)
analyzedCodons FETpvalue Nfix Npoly
NpolyShare
61050 146520 73260 146520
73260
Sfix Spoly SpolyShare totalCodons
73260 146520 73260 61050
> summary(rm$inSpecies)
M MP MS P S SP
146520 146520 122100 146520 146520 146520


I can plot the subset of the data that only looks at the measure
FETpvalue as you suggested above by

qplot(start, value, data=subset(rm, measure="FETpvalue"),
colour=inSpecies, facets=arm~.) +
geom_text(aes(label=file), size = 4, hjust = 1, vjust = 1) +
ylim(0, 0.05)


but how do I look at only the values of that variable that fall within
a certain range? FETpvalue is a pvalue, so for example, only things
less than 0.01. Is there a way to do it internally rather than by
setting a ylim as you do? Previously, I was using

qplot(start, FETpvalueM_MSmin2, data=subset(mk,
FETpvalueM_MSmin2<0.05)) but I can't see how to do this now that I'm
using the melted dataset.

3. For a more complex scenario, I've been able to plot several subsets
of the data as follows:
data=subset(rm, measure==c("Nfix","FETpvalue")) but is it possible to
also put restrictions on the values of each these variables as above
for pvalue? For example, how would I plot only data points where
measure=FETpvalue <0.05 and measure=Nfix>5?

4. Even more complex, is it possible to put multiple restrictions on
both variables? e.g., plot only the subset where measure=
FETpvalue<0.01 AND inSpecies=M?

5. And finally, is it possible to plot different relationships of the
variables, for example Nfix/Sfix?

Many thanks, once again.
Mara




hadley wickham

unread,
Apr 30, 2009, 1:28:23 PM4/30/09
to squid, ggplot2
> Thanks very much for this. Naturally it leads to several follow up
> questions...
>
> 1. Is it generally inappropriate and/or unnecessary to "attach"?  Are
> there any rules of thumb for this?

Yes, never use attach ;)

You'd just do subset(rm, measure == "FETpvalue" & value < 0.05)

> 3. For a more complex scenario, I've been able to plot several subsets
> of the data as follows:
> data=subset(rm, measure==c("Nfix","FETpvalue"))  but is it possible to
> also put restrictions on the values of each these variables as above
> for pvalue? For example, how would I plot only data points where
> measure=FETpvalue <0.05 and measure=Nfix>5?

(measure == "FETpvalue" & value < 0.05) | (measure == "Nfix" & value > 5)

But it may be better to just stick with the raw data.

> 4. Even more complex, is it possible to put multiple restrictions on
> both variables? e.g.,  plot only the subset where measure=
> FETpvalue<0.01 AND inSpecies=M?

See above for the basic idea.

> 5. And finally, is it possible to  plot different relationships of the
> variables, for example Nfix/Sfix?

No - you'd want to use the non-molten data for that.

Hadley

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages